How to install node.js as windows service?

node.jsWindows Services

node.js Problem Overview


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.

node.js Solutions


Solution 1 - node.js

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.

Solution 2 - node.js

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

Solution 3 - node.js

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

Solution 4 - node.js

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

Solution 5 - node.js

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

Solution 6 - node.js

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>

Solution 7 - node.js

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

Solution 8 - node.js

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTN.View Question on Stackoverflow
Solution 1 - node.jsCoreyView Answer on Stackoverflow
Solution 2 - node.jsHariram NandagopalView Answer on Stackoverflow
Solution 3 - node.jsPredrag StojadinovićView Answer on Stackoverflow
Solution 4 - node.jsKFLView Answer on Stackoverflow
Solution 5 - node.jsMichael HorojanskiView Answer on Stackoverflow
Solution 6 - node.jsKFLView Answer on Stackoverflow
Solution 7 - node.jsmilad shafieiView Answer on Stackoverflow
Solution 8 - node.jsBratislav B.View Answer on Stackoverflow