134

I have downloaded node.js executable. How can I run that executable as windows service? I cannot use standard node.js installer, since I need to run multiple version of node.js concurrently.

TN.
  • 17,602
  • 25
  • 84
  • 141

8 Answers8

207

Late to the party, but node-windows will do the trick too.

enter image description here

It also has system logging built in.

enter image description here

There is an API to create scripts from code, i.e.

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();

FD: I'm the author of this module.

cchamberlain
  • 14,693
  • 6
  • 52
  • 67
Corey
  • 4,266
  • 2
  • 21
  • 20
  • 4
    second that "awesome". I just followed your readme instructions and it worked straight out of the box - very rare! just one thing you might want to add to the readme: how to run the created js script at the CLI: i.e. > node set_up_win_service.js ... – mike rodent Sep 18 '14 at 17:15
  • @Corey any way I can run [JXCore](http://jxcore.com/docs/jxcore-feature-packaging-code-protection.html) package as a service using this module ? – Madhur Nov 11 '14 at 06:47
  • @Madhur - Theoretically, it might work, but I haven't and don't plan to test it. node-windows uses a wrapper.js file that's responsible for monitoring/restarts. This just launches the node script as a child process though. It's also possible to configure the executable path (i.e. jx instead of node). So, theoretically, you could probably do this, but I have no idea what kind of quirks you could potentially run into. – Corey Nov 12 '14 at 13:17
  • @Corey I have a simple Http Node app but it cant seem to stay running using this service. It installs and starts fine, but it stops immediatelly. Any help would be greatly appreciated sir! The event viewer shows no errors, but a warning: Child process [50732 - C:\Program Files\nodejs\node.exe --harmony "C:\Users\bmechkov\AppData\Roaming\npm\node_modules\node-windows\lib\wrapper.js" -f "C:\dev\Node\abs_tips\server.js" -l "NODE ABS TIPS" -g 0.25 -w 1 -r 3 -a n] terminated with 0 – Mechkov Jan 20 '15 at 20:18
  • @Mechkov - Terminating with a 0 means a successful exit. Can you run the script successfully without node-windows? – Corey Feb 21 '15 at 22:29
  • @Corey Yes, i can. The way i fixed this particular issue was to use Forever library from NPM which makes my NODE script run continously. Then i have a batch file that starts and stops the Forever deamon thread. The batch file is used with NSSM Windows Service tool. I havent tried to use Forever with node-windows yet. I am pretty new at this server-side JS config so out of curiousity do you know if this is something common (a NODE Http server app to run with Forever and/or as a service)? Do you have a better set up for such web app to reside on a server as a service and accept all requests? – Mechkov Feb 23 '15 at 14:09
  • @Mechkov - node-windows and Forever provide the same functionality, and so does nssm. You should only be using one of these. Running with Forever doesn't mean your script is functioning properly, because an error in your script will be handled the same by all of these. My question was whether you can run the script on it's own (i.e. node myscript.js). If it exits on it's own with a success code (repeatedly and immediately), node-windows will stop it from running so it doesn't run wild. – Corey Feb 24 '15 at 19:10
  • @Corey Yes, i can run it properly on the Node platform. Not using Forever and with NSSM, it almost looks like the server script runs and exits and It does NOT stay on. Picture running the script in command prompt - i dont get the prompt back back and the HTTPserver app is waiting on incoming requests right? Well, without Forever using either NSSM or node-windows it looks alsmost as if the script terminates after so many milliseconds and does not stay running to accept requests. I dont know what i am doing wrong. – Mechkov Feb 24 '15 at 19:29
  • @Mechkov - Again, you should not be using Forever with NSSM or node-windows. That's like running a daemon to run a daemon to run a script... i.e. use ONLY 1 of the 3. I think the issues you're having are the result of trying to run more than one of these at a time. If you are ONLY using node-windows and this still happens, then log an issue on the [node-windows Github issues page](https://github.com/coreybutler/node-windows/issues). – Corey Feb 25 '15 at 18:41
  • @Corey, I have my node application packaged into an exe using "pkg" module. I also set environment variable before launching exe i.e SET NODE_ENV=production&myApp.exe. Is is possible to launch the node app which is in for of exe as a service using "node-windows"? – pooja Aug 16 '19 at 07:25
  • @pooja - that's not really what node-windows does. Pkg generates an executable, while node-windows generates a wrapper executable and registers it with the OS. It'd be possible to modify the node-windows source to use a different executable if you like, but I think the use case is different enough to warrant a different module. – Corey Feb 19 '20 at 22:55
44

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
Deilan
  • 4,100
  • 3
  • 31
  • 47
Hariram Nandagopal
  • 671
  • 1
  • 7
  • 13
29

WinSer is a node.js friendly wrapper around the popular NSSM (Non-Sucking Service Manager)

Jake Hertenstein
  • 241
  • 1
  • 16
Predrag Stojadinović
  • 3,121
  • 5
  • 30
  • 49
  • don't like NSSM because it assumes it is Non-Sucking because it handles the the hosted application crash, so actually it is the hosted application sucking. Don't like in general blaming at Microsoft just because it is Microsoft. – Felice Pollano Dec 20 '17 at 08:58
  • 2
    @FelicePollano NSSM assumes it is Non-Sucking because it monitors the hosted application, in contrast to other solutions like srvany that leave the service in a running state, even if the wrapped process dies. – Jürgen Steinblock Jan 29 '18 at 11:04
  • @JürgenSteinblock this is exactly what I said: is the hosted application sucking, not the service manager itself – Felice Pollano Jan 29 '18 at 11:50
  • 2
    @FelicePollano a hosted application exit does not nessecarry mean something bad. The point is: NSSM reflects (or can reflect if configured properly) the real service state so the service can be monitored instead of just assuming a running state like other service managers (as a user I kann kill the hosted process and srvary would still show the service in a running state). – Jürgen Steinblock Jan 30 '18 at 13:47
  • In my experience, Nothing comes close to NSSM, handles Node, Python, Java(jar), PHP and PowerShell scripts just by a `nssm install` command. Install it via chocolatey `choco install nssm` and forget about all windows services quirks. – pouya Nov 30 '20 at 08:29
17

From this blog

Next up, I wanted to host node as a service, just like IIS. This way it’d start up with my machine, run in the background, restart automatically if it crashes and so forth.

This is where nssm, the non-sucking service manager, enters the picture. This tool lets you host a normal .exe as a Windows service.

Here are the commands I used to setup an instance of the your node application as a service, open your cmd like administrator and type following commands:

nssm.exe install service_name c:\your_nodejs_directory\node.exe c:\your_application_directory\server.js
net start service_name
The Red Pea
  • 12,346
  • 12
  • 77
  • 112
Michael Horojanski
  • 3,617
  • 4
  • 27
  • 32
16

I'm not addressing the question directly, but providing an alternative that might also meet your requirement in a more node.js fashion way.

Functionally the requirements are:

  1. Have the logic (app) running in the background
  2. Be able to start/stop the logic
  3. Automatically start the logic when system boots up

These requirements can be satisfied by using a process manager (PM) and making the process manager start on system startup. Two good PMs that are Windows-friendly are:

To make the PM start automatically, the most simple way is to create a scheduled task with a "At Startup" trigger:

enter image description here

KFL
  • 14,338
  • 12
  • 60
  • 80
  • If you try to start `pm2` using a batch script on startup, be sure to include the environment variables or it will not work. Discussed here: https://github.com/Unitech/pm2/issues/1079 – steampowered May 11 '16 at 15:02
  • @steampowered how give the PM2_Home path ? – charan tej Aug 23 '17 at 13:46
2

https://nssm.cc/ service helper good for create windows service by batch file i use from nssm & good working for any app & any file

milad shafiei
  • 148
  • 2
  • 2
  • 13
0

The process manager + task scheduler approach I posted a year ago works well with some one-off service installations. But recently I started to design system in a micro-service fashion, with many small services talking to each other via IPC. So manually configuring each service has become unbearable.

Towards the goal of installing services without manual configuration, I created serman, a command line tool (install with npm i -g serman) to install an executable as a service. All you need to write (and only write once) is a simple service configuration file along with your executable. Run

serman install <path_to_config_file>

will install the service. stdout and stderr are all logged. For more info, take a look at the project website.

A working configuration file is very simple, as demonstrated below. But it also has many useful features such as <env> and <persistent_env> below.

<service>
  <id>hello</id>
  <name>hello</name>
  <description>This service runs the hello application</description>

  <executable>node.exe</executable>

  <!-- 
       {{dir}} will be expanded to the containing directory of your 
       config file, which is normally where your executable locates 
   -->
  <arguments>"{{dir}}\hello.js"</arguments>

  <logmode>rotate</logmode>

  <!-- OPTIONAL FEATURE:
       NODE_ENV=production will be an environment variable 
       available to your application, but not visible outside 
       of your application
   -->
  <env name="NODE_ENV" value="production"/>

  <!-- OPTIONAL FEATURE:
       FOO_SERVICE_PORT=8989 will be persisted as an environment
       variable machine-wide.
   -->
  <persistent_env name="FOO_SERVICE_PORT" value="8989" />
</service>
Community
  • 1
  • 1
KFL
  • 14,338
  • 12
  • 60
  • 80
0

Since qckwinsvc has not been updated for a while there's a new version called qckwinsvc2 (npm, github)

It now supports args passed to the service. It also keeps a local cache so you don't have to provide a path every time you want to perform an action

Use the now arg to start the service as soon as it's installed

qckwinsvc2 install name="Hello" description="Hello World" path="C:\index.js" args="--y" now
qckwinsvc2 uninstall name="Hello"
qckwinsvc2 list
Dharman
  • 21,838
  • 18
  • 57
  • 107