Can any node.js experts tell me how I might configure node JS to autostart a server when my machine boots? I'm on Windows
Nodemon is a package for handling this restart process automatically when changes occur in the project file. It will show the version of nodemon as shown in the below screenshot. Now, when we make changes to our nodejs application, the server automatically restarts by nodemon as shown in the below screenshot.
js application locally after closing the terminal or Application, to run the nodeJS application permanently. We use NPM modules such as forever or PM2 to ensure that a given script runs continuously. NPM is a Default Package manager for Node.
This isn't something to configure in node.js at all, this is purely OS responsibility (Windows in your case). The most reliable way to achieve this is through a Windows Service.
There's this super easy module that installs a node script as a windows service, it's called node-windows (npm, github, documentation). I've used before and worked like a charm.
var Service = require('node-windows').Service; // Create a new service object var svc = new Service({ name:'Hello World', description: 'The nodejs.org example web server.', script: 'C:\\path\\to\\helloworld.js' }); // Listen for the "install" event, which indicates the // process is available as a service. svc.on('install',function(){ svc.start(); }); svc.install();
p.s.
I found the thing so useful that I built an even easier to use wrapper around it (npm, github).
Installing it:
npm install -g qckwinsvc
Installing your service:
> qckwinsvc prompt: Service name: [name for your service] prompt: Service description: [description for it] prompt: Node script path: [path of your node script] Service installed
Uninstalling your service:
> qckwinsvc --uninstall prompt: Service name: [name of your service] prompt: Node script path: [path of your node script] Service stopped Service uninstalled
If you are using Linux, macOS or Windows pm2 is your friend. It's a process manager that handle clusters very well.
You install it:
npm install -g pm2
Start a cluster of, for example, 3 processes:
pm2 start app.js -i 3
And make pm2 starts them at boot:
pm2 startup
It has an API, an even a monitor interface:
Go to github and read the instructions. It's easy to use and very handy. Best thing ever since forever.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With