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.
8 Answers
Late to the party, but node-windows will do the trick too.
It also has system logging built in.
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.
-
6second 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 ... Sep 18, 2014 at 17:15
-
-
@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.– CoreyNov 12, 2014 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– MechkovJan 20, 2015 at 20:18
-
@Mechkov - Terminating with a 0 means a successful exit. Can you run the script successfully without node-windows?– CoreyFeb 21, 2015 at 22:29
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
-
-
“npm ERR! asyncWrite is not a function” “npm ERR! pna.nextTick is not a function”– TimwiSep 12, 2018 at 20:35
-
1qckwinsvc is not maintained anymore. There is a new version qckwinsvc2 (npm i qckwinsvc2 -g) Apr 24, 2021 at 17:00
WinSer is a node.js friendly wrapper around the popular NSSM (Non-Sucking Service Manager)
-
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. Dec 20, 2017 at 8: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. Jan 29, 2018 at 11:04
-
1@JürgenSteinblock this is exactly what I said: is the hosted application sucking, not the service manager itself Jan 29, 2018 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). Jan 30, 2018 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 chocolateychoco install nssm
and forget about all windows services quirks.– pouyaNov 30, 2020 at 8:29
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
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:
- Have the logic (app) running in the background
- Be able to start/stop the logic
- 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:
-
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: github.com/Unitech/pm2/issues/1079 May 11, 2016 at 15:02 -
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>
https://nssm.cc/ service helper good for create windows service by batch file i use from nssm & good working for any app & any file
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
-
-
This works well. I wish there was a way to redirect the console.logs to a file. I tried adding it as a argument which did not work: args="> c:\log.txt" Dec 16, 2022 at 14:23