node.js rerun script on specific event - node.js

I'm not looking for a file watcher like nodemon
I have the following scenario:
I'm running a script script.js in nodeJS, In some cases I have to switch to a different git branch (I know how to do this part).
After the git branch is switched, I would like to exit the current run and reload script.js again.
How do I cause nodeJS to reload and run the same script again?
Do I need to start another process and exit the current one?

Related

Git pre-commit hook isn't executed

I want to execute a node.js script in a pre-commit hook.
Expected result: node.js script is executed
Actual result: Nothing happens, no error, no logs, nothing
#!/usr/bin/env bash
echo "Run pre-commit hook"
node [project]/scripts/generate-social-media-preview/index.js
git add .
echo "Finished pre-commit hook"
exit 0
Of course, I also tried to run the node.js directly in the pre-commit, by changing the first line to: #!/usr/bin/env node. Same result.
The script executes playwright, manipulates an HTML template, takes a screenshot, and saves it. All of this lives in a nuxt.js project, so the script and the rest of the project are using one shared node-modules folder and package.json.
If I just run [project]/scripts/generate-social-media-preview/index.js it does exactly what it should do. I made the file executable.
I normally commit via Webstorm, but I also did it with the terminal and I never saw an output or that the images got generated accordingly.
Also, husky and pre-commit were on my list, but it behaved as with the native pre-commit hook.
I would be happy to solve this "within the code" to not have a plugin or local script which triggers this, also running this during npm run generate isn't working as my website is hosted at Netlify and of course, there isn't chromium available for playwright.
The whole code can be seen here (with all my test commits...): https://github.com/LukaHarambasic/harambasic.de/pull/46
So, there were two problems:
WebStorm doesn't handle pre-commit hooks very well and for me, somehow isn't working with husky. From now on, I'll do commands via the command line. If you want to know more, read this and this issue. -> let me know if you find a solution
Committing files in a pre-commit hook isn't very reliable - check this question to see the solution with a pre-commit and post-commit hook.

Autostart a node.js script using init.d in Debian

I have a little node application on a server (node mailer) that I run by going to its source folder and executing npm start. I figured the best way to run this automatically would be to create a my_script.sh file and drop it in the init.d directory of my debian box. Inside the file (below the !#/bin/bash line), the code to execute is
'/opt/mycode/source/npm start'
I save the line to the .sh file and restarted the machine, but so far haven't got it to work. My question is: is this even how you start a script like this (using that command and an .sh file)? It does start normally when I do it manually (when I navigate to it and run npm start in the terminal). I included the single quotes around it because of the space between npm start. Also, if I want to verify that it worked, which process would I look for other than just pinging my smtp mailer? Finally, I know I need to run:
update-rc.d my_script.sh defaults
but I was also confused at to whether I had done this correctly either (is it just the name of the file that goes there or the file plus the extension)?
The script that you leave on the init.d folder should not have any extension and should have functions to start, stop and get the status of the service (your application).
I'll leave a link with an example as well as with some basis in order to build the Linux service script.
I would suggest reloading the daemon with systemctl daemon-reload in order to refresh the Linux service files once you add a new one.

How to create git hooks in Windows with Node.js?

I have been following this guide on how to use Node.js to script git hooks. However, the guide is using a Unix based system whilst I am running on a Windows machine.
I have also found this guide on running git hooks on a Windows machine, but it is not using Node.js
I am running a pre-install script in my package.json file to set a custom git hooks location.
I am using VSCode as my editor and would like the git hooks to run when I use the UI for commits etc. However I am using command line initially to try and get the hooks to fire.
package.json excerpt
"scripts": {
"preinstall": "git config core.hooksPath ./git.hooks"
},
In my git.hooks folder I have a pre-commit.js file.
I have updated the first line to reflect the fact I'd like to execute the script running Node.js
pre-commit.js
#!C:/Program\ Files/nodejs/node.exe
console.log('Hello world!');
process.exit(1);
If I run this script directly I get a Microsoft JScript compilation error - Invalid character on line 1 char 1.
If I do a commit, I get no errors but nothing happens.
Can anyone guide me through the process of creating a Node.js hook in Windows. I would rather create one myself than use a package.
Name the hook exactly pre-commit, without .js.
Change the first line to #!/usr/bin/env node. But make sure that C:/Program\ Files/nodejs/node.exe has been added to the environment variable PATH.
Place it in <repo>/.git/hooks.
Make it executable. In git-bash, run chmod a+x <repo>/.git/hooks/pre-commit.
Now it should work as expected.

How can I run a node app from the command line and return control?

I run a angular app like this:
npm run server-java
From a terminal window.
The server starts, but I want it to give back the shell prompt.
I want the shell prompt back while it runs.
You can use tools like pm2 or forever to start your nodejs app as a background process. Usually used for production setup.
Another option is:
npm run server-java > /dev/null 2>&1 &
You will get back to the terminal, but then you have to kill process manually by id when you don't need it.

How to run a node.js file as a background process on a Windows server?

I was creating a node.js project and uploaded it to my Windows server to provide an API service for mobile application.
When I open command prompt and type
node app.js
It runs correctly, but when I close the command prompt my node.js server stopped running.
How to make it still running when I close the commend prompt?
For example on Ubuntu I use the command
nohup
How can I do this on Windows?
You can make the process run in background using pm2
pm2 start app.js --watch
This will start the process and will also look for changes in the file.
More about watch flag
Nodemon #ftw. On Windows, Forever doesn't really watch files so much as casually observe them, while pm2 restarts the server every time you load a page.
Each of these tools works similarly, and each installs just as in the accepted answer. E.g.:
npm install nodemon -g
This installs nodemon globally, and to use you can simply navigate to your project folder and enter:
nodemon
(Assuming your project has an app.js file). You can add the -w switch, just as in Forever and pm2, however if you're just wanting to watch all files, you can omit that. To run nodemon in the background with Forever, you would run this:
forever nodemon --exitcrash
Nodemon is good for development, especially on Windows, while Forever and pm2 are more for production, on Linux.
Here is a simpler answer that cuts right to the chase without any added libraries or overhead like in the other two answers described above. To run your Node.js application as a windowless startup program in the background (this would be analogous to "nohup" in Linux), modify this template to suit and copy it into a .VBS script file. Then copy that file to your Start Menu startup folder (for all users, that would be C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup) and it will automatically run. The techniques you are using here in Visual Basic are (1) preparing to run the Node.js application by first changing the working directory of the shell object and (2) informing the shell to run the Node.js application in a hidden window by adding a “, 0” immediately after the run function:
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.CurrentDirectory = "C:\path-to-your-node-js-app\"
objShell.Run("""node"" your-app.js"), 0
Set objShell = Nothing
References:
https://keestalkstech.com/2016/07/start-nodejs-app-windowless-windows/
https://devblogs.microsoft.com/scripting/how-can-i-change-the-working-folder-of-a-script/
No, you can't.
Even if you make a GUI program you'll need to run it via console.
And as soon as you close the command prompt. Your service would be stopped/ terminated that moment only. Because node creates a server itself while running : http.createServer().listen(port) or app.listen(port). So this this makes it independent in nature.
So, as soon as you close the command prompt on which server was running all the services would stop at that moment.

Resources