Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto start node.js server on boot

Tags:

node.js

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

like image 921
Bachalo Avatar asked Dec 07 '13 19:12

Bachalo


People also ask

How do I restart node server automatically?

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.

How do I permanently run NodeJS server?

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.


2 Answers

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 
like image 61
talles Avatar answered Oct 20 '22 23:10

talles


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:

AWESOME

Go to github and read the instructions. It's easy to use and very handy. Best thing ever since forever.

like image 42
durum Avatar answered Oct 20 '22 22:10

durum