Using nssm to automatically start a node server on windows

July 04, 2019

Automatically starting a node process (e.g. your server) on windows (Windows Server or Windows 10) can by quite tricky. For *nix systems there are great alternatives (pm2 or forever), but I did not have any luck getting them to run on our windows machines.

Using nssm

NSSM short for non-sucking service manager however worked great for our other services. Luckily we can use it as our service manager for node processes, too.

NSSM is a small comand line tool that can be used to start, stop, install and uninstall services. This is how you would stop a service for example:

nssm stop service-name

Other helpful commands are nssm start service-name to start a server and nssm remove service-name to uninstall a stopped service. You can also edit (nssm edit service-name) the service information. Use nssm install service-name and follow the instructions, or use my script.

The script

This is my install_service.bat script to install and configure a service.

@echo off
cd /D "%~dp0"
:: Set the correct directory. Only %~dp0 does not work because of its trailing backslash
set HOME=%CD%
:: check for permissions first
:: we need admin priviliges for NSSM
goto check_Permissions
echo Administrative permissions required. Detecting permissions...
net session >nul 2>&1
if %errorLevel% == 0 (
echo Success: Administrative permissions confirmed. Starting config tool
goto startInstallation
) else (
echo Failure: Current permissions inadequate. Please right click bat and run as Administrator.
exit /B 1
:: only gets called when we have admin priviliges
echo Installing service
:: Set your service name and its description here
set SERVICE_NAME=node-script
:: replace with the absolute path where node.exe can be found
nssm install %SERVICE_NAME% "C:\Program Files\nodejs\node.exe"
nssm set %SERVICE_NAME% AppDirectory "%HOME%"
:: replace index.js with the name of your script
nssm set %SERVICE_NAME% AppParameters "index.js"
:: optionally set the out.log and error.log paths which will be used for stdouts and sterr messages
:: better use a logging framework like winston
nssm set %SERVICE_NAME% AppStdout "%HOME%\out.log"
nssm set %SERVICE_NAME% AppStderr "%HOME%\error.log"
:: Start the service
nssm start %SERVICE_NAME%
echo Successfully installed and started service %SERVICE_NAME%
view raw install_service.bat hosted with ❤ by GitHub

You have to replace up to 4 things when you copy this script.

  1. set SERVICE_NAME=node-script

    Replace node-script with your service name here. Must not have spaces. Use dashes instead of spaces!

  2. set SERVICE_DESCRIPTION=description

    Replace description with the service’s description. While being optional, I recommend to always set a description. It makes it easier for your colleagues and your future self to remember what the service is about.

  3. nssm set %SERVICE_NAME% AppParameters "index.js"

    Replace index.js with the name of your script. E.g. server.js or src\server.js

  4. (optional) nssm install %SERVICE_NAME% "C:\Program Files\nodejs\node.exe"

    If your node.exe cannot be found under that location, please set the correct path here.

You could also replace the paths for AppStdout and AppStderr. I recommend using a real logger like winston instead.

I write about React, JavaScript, web application development and other stuff. Sometimes in german and sometimes in english.