Running node.exe as a service in Windows

2,519 views
Skip to first unread message

rtweed

unread,
Dec 6, 2011, 8:51:23 AM12/6/11
to nodejs
Does anyone have any recommendations on how best to run node.exe as a
service in Windows?

I did come across this on Google:

http://blog.tatham.oddie.com.au/2011/03/16/node-js-on-windows/

..but interested in any other suggestions

Rob

Bradley Meck

unread,
Dec 6, 2011, 10:25:48 AM12/6/11
to nod...@googlegroups.com
Running applications as Windows Services requires that the app be built for and linked against Microsoft's C++ service entry points. http://stackoverflow.com/questions/1554047/convert-a-c-program-to-a-windows-service has a lot of good info. In order to make node able to do this it would be necessary to modify node's source to include those entry points and have them conditionally set when Node is not being used as a service and/or outside of Windows.

vincentcr

unread,
Dec 6, 2011, 10:58:33 AM12/6/11
to nod...@googlegroups.com
the easiest and most maintainable way i found is to create a thin shim that launches another process by getting command-line arguments from somewhere else (like a config file).

you can create this shim in c# as well as c/c++. in c#, it's pretty easy: you only need to override the onStart and onStop methods of the ServiceBase class: 

i would also suggest you use the sc.exe command-line tool to maintain the service: install/remove, start/stop, etc. you can do everything with it.

note that a windows service runs as a separate user, and in an isolated session ("session 0"). 

James Burton

unread,
May 19, 2015, 10:31:00 AM5/19/15
to nod...@googlegroups.com

Here is an example of running VorlonJS as a service under windows, which should help anyone with similar issues:

Running VorlonJS as a service on Windows.
=========================================

1. Ensure you have NPM installed.
2. Pick a folder to configure this from e.g.
mkdir c:\Development\vorlon\
cd c:\Development\vorlon
3. Install "vorlon" package with npm:
a) Globally (+ linking):
npm install -g vorlon
npm link vorlon
b) OR Locally (not recommended if you wish to be able to call vorlon manually)
npm install vorlon
4. Install "node-windows" package with npm:
a) Globally (+ linking):
npm install -g node-windows
npm link 
b) OR Locally (not recommended)
npm install node-windows
5. Create the following files:
VorlonService.install.js:

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

// Create a new service object
var svc = new Service({
name:'VorlonJSServer',
description:'Vorlon JS Server (port 1337)',
script: require('path').combine(__dirname, 'node_modules\\vorlon\\Server\\server.js')
});

// Listen for the "install" event, which indicates the process is available as a service.
svc.on('install',function(){
"VorlonJS Server Service Installed ... starting service ..."
svc.start();
console.log("... VorlonJS Server Service started.");
});

svc.install();

VorlonService.uninstall.js

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

// Create a new service object
var svc = new Service({
name:'VorlonJSServer',
description:'Vorlon JS Server (port 1337)',
script:require('path').combine(__dirname, 'node_modules\\vorlon\\Server\\server.js')
});

// Listen for the "install" event, which indicates the process is available as a service.
svc.on('uninstall',function(){
console.log("Uninstall complete.");
console.log("The service exists: " + svc.exists);
});

svc.uninstall();
6. Now you can install the service with "node VorlonService.install.js"
7. You can remove the service again with "node VorlonService.uninstall.js" 
Reply all
Reply to author
Forward
0 new messages