18

I created a simple http server in Node.js.

I wanted to make it run permanently on my Windows 2008 machine, so that, if the computer reboots, it automatically restarts.

So I made it a service with this command:

C:\Users\Administrator>sc create translate binPath= "node D:\Apps\translate\machine-learning-server\servertranslate.js" DisplayName= "Translation Server"

Then started it with:

C:\Users\Administrator>sc start translate

and got the following error message:

[SC] StartService FAILED 1053:

The service did not respond to the start or control request in a timely fashion.

The program works OK when I start it from the command line (not as a service).

What is the easiest way to have a node.js web server that restarts automatically when the computer reboots?

7 Answers 7

17

In the past, I've used NSSM for running Node.js applications as services on Windows. It works quite well, and can be configured to automatically restart your application in the event of a crash.

http://nssm.cc/usage

nssm install YourService "C:\Program Files\Node.js\node.exe" "C:\something\something.js"
Sign up to request clarification or add additional context in comments.

3 Comments

I put a copy of node.exe in the app directory, otherwise I had some path problems with nssm.
@Motti No issues like that here. Check your permissions.
If you just do nssm install YourService then a gui will open with all the options. It has 1 field for the executable and 1 field for the working directory. that was probably Motti's problem. Also, a note, nssm has an option to restart node if it stops unexpectedly.
7

As I recall, the Service runtime environment isn't the same as running something under the command shell. In particular, Services are required to respond to messages from the system to indicate their running status, as you've seen :-)

This must be a solved problem, though...

Sure enough: https://npmjs.org/package/windows-service

windows-service

Run Node.JS programs as native Windows Services.

npm install windows-service

2 Comments

OK, but what if I want to run the same program, both from the command line and as a Windows service - do I need two versions of the same program?
Read the docs for windows-service - you can implement command-line switches that tell the program to install itself as a service (or not).
4

Use this one, really simple https://github.com/coreybutler/node-windows

Create two js file on your project. And run those as

node your_service.js node your_service_remove.js

For install:

 /**
 * Created by sabbir on 08/18/2015.
 */
//ref: https://github.com/coreybutler/node-windows
var Service = require('node-windows').Service;

// Create a new service object
var svc = new Service({
  name:'nodeDemoApp',
  description: 'The nodejs.org example web server.',
  script: 'D:\\NodeJS\\demoWeb\\bin\\www'
});

// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
  svc.start();
});

svc.install();

For uninstall:

var Service = require('node-windows').Service;

// Create a new service object
var svc = new Service({
  name:'nodeDemoApp',
  script: require('path').join(__dirname,'bin\\www')
});

// Listen for the "uninstall" event so we know when it's done.
svc.on('uninstall',function(){
  console.log('Uninstall complete.');
  console.log('The service exists: ',svc.exists);
});

// Uninstall the service.
svc.uninstall();

4 Comments

Getting below error while running the application using node-windows. ENOENT: no such file or directory, open 'C:\Windows\system32\views\login\index.jade'
not sure why it's looking into system32 folder instead of my application folder
@jiterndra-pancholi can you paste how your JADE template configured on to project?
@JitendraPancholi do you remember how you solved it ? Was it related to __dirname as described in the node-windows wiki ?
2

At a guess, I'd say that the service doesn't know where to find the node binary. You've probably updated your profile's PATH variable. My recommendation is to ALWAYS hard code the full path in service scripts.

2 Comments

I must categorically disagree with the idea that you should "hard code the full path" of anything, ever.
@AJMansfield , I'm not a fan of absolute paths, but the node-windows package for running node.js as a Windows Service advises users to: "Per the instructions on the README, always use an absolute path."
2

As mentioned in others questions about it, I'd like to share here (because it wasn't referred yet) a node.js module called WinSer that wraps NSSM and its usage is very simple, maybe it helps someone someday.

: )

Comments

2

You could try the package qckwinsvc. First install it globally:

npm install -g qckwinsvc

And then from the cmd:

qckwinsvc
prompt: Service name: [...]
prompt: Service description: [...]
prompt: Node script path: [/path/to/.js file]

To uninstall:

qckwinsvc --uninstall

Comments

1

Always a good idea to look at the number of downloads something is getting.

PM2 seems to be winning and is very easy.

https://medium.com/@harshamw/deploying-a-node-js-application-in-iis-using-a-reverse-proxy-process-management-using-pm2-3d59b83d7f76

You'll need to then use https://www.npmjs.com/package/pm2-windows-service to start it as a windows service on reboot.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.