Skip to content

Instantly share code, notes, and snippets.

@dceejay
Last active September 17, 2024 21:54
Show Gist options
  • Save dceejay/576b4847f0a17dc066db to your computer and use it in GitHub Desktop.
Save dceejay/576b4847f0a17dc066db to your computer and use it in GitHub Desktop.
Running Node-RED as a service on Windows using NSSM

Running Node-RED as a service on Windows using NSSM

These instructions assume you installed Node-RED from an administrator enabled command prompt using the npm install -g node-red command to do so.

As an administrator, install nssm from https://nssm.cc/download

And then from the directory where you installed nssm, run the commands

mkdir c:\temp
nssm install Node-RED "c:\Users\\"%USERNAME%"\AppData\Roaming\npm\node-red.cmd"
nssm set Node-RED AppDirectory "c:\Users\\"%USERNAME%"\.node-red"
nssm set Node-RED AppParameters "-u c:\Users\\"%USERNAME%"\.node-red > c:\temp\node-red.log"
nssm set Node-RED Description "A wiring tool for the Internet of Things"

You can edit the service using the GUI to the configuration, add extra parameters, etc.

nssm edit Node-RED

The log will be written to c:\temp\node-red.log (see line 3)

For further options read the nssm usage page.

@aniston
Copy link

aniston commented Oct 28, 2020

pm2 start C:\Users\<USER-NAME>\AppData\Roaming\npm\node_modules\node-red\red.js --save

on W10 that won't work unless preceded by -- hence that becomes

pm2 start C:\Users\<USER-NAME>\AppData\Roaming\npm\node_modules\node-red\red.js -- --save

My flavor to running node-red as a service was the combination of pm2 and pm2-windows-service

  • pm2-windows-service actually handles only the pm2 as a visible windows service.
  • after that pm2 kicks in and handles it's own configuration.

Sorry to have jumped in on an thread that has to do with the NSSM manager !

@amilanov75
Copy link

Hey, I setup the PM2 service described above by @bloggie. Thank you BTW for your solution!

When I was running from the command prompt and I went to safely shut down ie.. ctrl+C, sometimes it would take a good few seconds before it shut down or I'd even have to hit ctrl+c a couple of times before it shut down.

When I stop the service using:
cmd /k pm2 stop C:\Users\ubiquitous\AppData\Roaming\npm\node_modules\node-red\red.js pause
It stops the service almost instantly every single time.
I'm just wondering if the way I am stopping the service is the right way for it to be "safe"

@ELGUEROUEL
Copy link

thanks it is working for me
As an administrator, install nssm from https://nssm.cc/download

And then from the directory where you installed nssm, run the commands

mkdir c:\temp
nssm install Node-RED "c:\Users\"%USERNAME%"\AppData\Roaming\npm\node-red.cmd"
nssm set Node-RED AppDirectory "c:\Users\"%USERNAME%".node-red"
nssm set Node-RED AppParameters "-u c:\Users\"%USERNAME%".node-red > c:\temp\node-red.log"
nssm set Node-RED Description "A wiring tool for the Internet of Things"

@Cappe73
Copy link

Cappe73 commented Apr 14, 2021

Another way for autostarting NodeRed in PM2 as a Serivce in Windows:

Everything in command-line as Admin:

Install node-red

npm install -g node-red

Install pm2

npm install pm2 -g

Install and config pm2-windows-startup package :

npm install pm2-windows-startup -g
pm2-startup install
pm2 save

Locate the Node-Red red.js File and add it to pm2:

pm2 start C:\Users\<USER-NAME>\AppData\Roaming\npm\node_modules\node-red\red.js --save

thank you so much

@matteoangrisano
Copy link

@bloigge does the PM2 method autorestart Node-RED if it crashes?

@bloigge
Copy link

bloigge commented Dec 9, 2021

@matteoangrisano - I am not using pm2 anymore - but I think there a several strategies regarding restarting an app: https://pm2.keymetrics.io/docs/usage/restart-strategies/

@AndreSchm
Copy link

AndreSchm commented Sep 15, 2022

Note: Running this from a Powershell prompt will not work. Escaping of quotes will fail and the commands will all get borked.

If you intend to use nssm and installation by Powershell you can execute something like:

$NssmDir = "<your_path_to_nssm>"
$NodeRedDir = "C:\Users\$($env:USERNAME)\AppData\Roaming\npm\node-red.cmd"
$ServiceName = "Node-RED"
Set-Location $NssmDir
.\nssm.exe install $($ServiceName) $NodeRedDir -Wait
.\nssm.exe set $($ServiceName) AppDirectory "C:\Users\$($env:USERNAME)\.node-red"
.\nssm.exe set $($ServiceName) AppParameters "-u c:\Users\$($env:USERNAME)\.node-red > c:\Users\$($env:USERNAME)\.node-red\log.log"
.\nssm.exe set $($ServiceName) Description "A wiring tool for the Internet of Things"
Start-Service $ServiceName

@slashinpdx
Copy link

Another way for autostarting NodeRed in PM2 as a Serivce in Windows:

Everything in command-line as Admin:

Install node-red

npm install -g node-red

Install pm2

npm install pm2 -g

Install and config pm2-windows-startup package :

npm install pm2-windows-startup -g pm2-startup install pm2 save

Locate the Node-Red red.js File and add it to pm2:

pm2 start C:\Users\<USER-NAME>\AppData\Roaming\npm\node_modules\node-red\red.js --save

Yes, thanks. 👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment