Multiple NodeRed Instances on different Ports - Linux/SystemD

I know this has been addressed because I've made it work in the past, so I apologize for rehashing an already discussed topic... but I can't seem to find the instructions again. I've tried multiple times using some of the other methods I've found to get this to work and have done nothing but break Node_Red repeatedly.

I have an AWS server and I need to run 2 instances of NodeRed on 2 different ports. I'm running Ubuntu and using SystemD to autostart Node-Red on boot.

The goal is to have 2 admin panels so that 2 different people can test and develop without having to boot up a second server with its own Node-Red install... and be able to point 2 different domains to the UI of those instances.

I'd like to avoid using PM2 if possible, as I used this method of autostart when I installed: https://nodered.org/docs/hardware/raspberrypi#adding-autostart-capability-using-systemd

I tried the following instructions, but only managed to get an error saying it can't find red.js and Node-Red failed to boot: https://nodesource.com/blog/running-your-node-js-app-with-systemd-part-2/

So I've rolled back... fresh install of Node-Red... What's the easiest way to get 2 instances running on 2 different ports?

Thanks in advance.

What I do, is create two different directories and do npm i node-red in each one and then edit the settings.js file in node_modules/node-red so that they use different ports.

And then, in each directory, I launch two node-reds using a command like node node_modules/node-red/red -s node_modules/node-red/settings.js -u ./

This tells node-red to use the settings.js file found in the node_modules/node-red directory and to create a flow.json file in the current directory.

Use the command line options.

Command-line usage

Usage: node-red [-v] [-?] [--settings settings.js] [--userDir DIR] [flows.json]

Options:
  -s, --settings FILE  use specified settings file
  -u, --userDir  DIR   use specified user directory
  -v                   enable verbose output
  -?, --help           show usage

You only need to install once then run a second, third, nth instance with different command line options.

1 Like

Typically I do ...

node-red -u /node-red-1881 -p 1881

Just noticed the -p option is missing from documentation (but it definitely works)

Perhaps documentation is out of date?

https://nodered.org/docs/getting-started/running

3 Likes

Hi @Steve-Mcl, good point. The part to be updated is the result of the node-red --help, which would be:

doc-01

I will fork the doc repository and try to submit a pull request.

Thanks for the reply. I've seen several posts regarding the CLI options...

I just can't seem to get those incorporated into the auto-startup option. I may not have been very clear in my post, sorry.

I am using systemd to auto-start the node-red service. Currently it starts 1 instance on port 1880.

I need it to also auto-start a second instance on port 1881.

I've tried copying the /lib/systemd/system/nodered.service, renaming it, modifying the internal settings to try and pull from different settings.js files... and then modifying those settings.js files to use different ports.

I've tried modifying the Environment line to include the options you've specified for the CLI.... but no matter what I try, it either tries to start a second instance on the same port, or throws some other error...like can't find "red.js".

Am I over-complicating this by trying to use SystemD to autostart both... and maybe I should just write a script?

Show us your modified systemd script that tries to specify the settings and flow file.

1 Like

As long as your settings.js file is right, you can do this via an environment variable. I posted my setup a few days ago that showed how to do this via an environment file.

settings.js:

uiPort: process.env.PORT || 1880,

/etc/systemd/system/multi-user.target.wants/nrlive.service

Obviously you can call this whatever you want.

# See https://gist.github.com/Belphemur/3f6d3bf211b0e8a18d93
#     http://www.freedesktop.org/wiki/Software/systemd/
#
# If changing this file, run:
# sudo systemctl daemon-reload && sudo systemctl enable nrlive

[Unit]
Description=Node-Red Home Automation production (live) service
Documentation=file:///home/pi/nrlive/README.md http://nodered.org/docs
After=syslog.target network.target
Wants=mosquitto.service influxdb.service

[Service]
Type=simple

#Environment="NODE_OPTIONS=--max-old-space-size=128" "NODE_RED_OPTIONS=--userDir /home/pi/nrlive/.data -v"
#Environment="NODE_RED_OPTIONS=-v"

# Environment variables file, overrides the Environment settings
# @See https://www.freedesktop.org/software/systemd/man/systemd.exec.html#EnvironmentFile=
EnvironmentFile=/home/pi/nrlive/.data/envFile.txt

# No path available here so make sure absolute folders specified
ExecStart=/usr/bin/node $NODE_OPTIONS /home/pi/nrlive/node_modules/node-red/red.js $NODE_RED_OPTIONS

WorkingDirectory=/home/pi/nrlive

# User/Group that launches node-RED (it's advised to create a new user for Node-RED)
# You can do : sudo useradd node-red then change the User=root by User=node-red
User=pi
Group=pi

Nice=10

SyslogIdentifier=NRlive
StandardOutput=syslog

# Make Node-RED restart if it fails
Restart=on-failure

#Restart=always
#RestartSec=100

# Node-RED need a SIGINT to be notified to stop
KillSignal=SIGINT

# Prevent this service changing the OS or /etc
# @See https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectSystem=
ProtectSystem=full

[Install]
WantedBy=multi-user.target

envFile.txt

# Environment variables file for running nrlive under SystemD
# Each line is a separate variable that will be available to the ExecStart
# and to the running process.
# @see https://www.freedesktop.org/software/systemd/man/systemd.exec.html#EnvironmentFile=
#
# Called from /etc/systemd/system/multi-user.target.wants/nrlive.service
# Which is linked from /home/pi/nrlive/systemd/nrlive.service

NODE_OPTIONS=--max_old_space_size=256
NODE_RED_OPTIONS=--userDir /home/pi/nrlive/.data -v
PORT=1880
1 Like

@Colin I mentioned in my first post that I wiped everything and started fresh, so my systemd file is currently a wget from: https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/nodered.service

as instructed here: https://nodered.org/docs/hardware/raspberrypi#adding-autostart-capability-using-systemd

I probably should have started with my modified file and asked for help, but nothing worked, and I wasn't even sure I was on the right track really.

@TotallyInformation Thanks for the post! I am pretty sure I can get that to work. I'll try first thing when I get back in the office tomorrow morning.

No problem. Just note that I don't use the standard installation of Node-RED. Instead I prefer to install to a "master" folder with a sub-folder containing the userDir. I'm sure you will be able to adjust according to your needs.

When running multiple instances (each on a different port and from different folders. e.g. ~/.node-red-1888, ~/.node-red-1880 etc), can they also be of different versions say v1.1.1, 1.0.4 etc ?

I have tried-
npm install --prefix .node-red-1880 node-red@1.0.4,
npm install -g --prefix .node-red-1880 node-red@1.0.4

from within the target directory (~./node-red-1880) and the parent but with no effect.

Any ideas anyone ?

P.S. I gave up with customising systemd for now. Just starting NR with-
./node-red-pi.sh

I don't know what the prefix parameter for npm does but maybe this gives you a clue:

cd ~/.node-red-1888
npm install node-red@1.0.4 # install to node_modules under current dir
npx node-red-pi -s settings.js -u . -p 1888 # run node-red-pi executable from ./node_modules/bin

Edit: just tested this and it should work

You can simply do local installs (without the -g switch) into separate directories.

I have all versions installed in an apps directory in my home dir. That's how I have managed it for some time now. The node-red symlink points to my currently preferred version.

~/apps
โ”œโ”€โ”€ node-red -> node-red_1.0
โ”œโ”€โ”€ node-red_0.19
โ”‚   โ”œโ”€โ”€ node_modules
โ”‚   โ””โ”€โ”€ package.json
โ”œโ”€โ”€ node-red_0.20
โ”‚   โ”œโ”€โ”€ node_modules
โ”‚   โ””โ”€โ”€ package.json
โ”œโ”€โ”€ node-red_1.0
โ”‚   โ”œโ”€โ”€ node_modules
โ”‚   โ”œโ”€โ”€ package.json
โ”‚   โ””โ”€โ”€ package-lock.json
โ””โ”€โ”€ node-red_1.1
    โ”œโ”€โ”€ node_modules
    โ”œโ”€โ”€ package.json
    โ””โ”€โ”€ package-lock.json
1 Like

Thank you. What is npx ?
Its alright I will google it.

Was it you @ristomatti that said you have seen many an old dog fall by the wayside with all this js malarky, well I am just on the edge of adding to that list.

Life is too short to spend much more time on transient stuff like this. I only came here to offer up my experience with NR and make a few suggestions.

I was pretty sure from the beginning that this task was beyond my capabilities and it is turning out that way.

Hope you all have a nice time.

Thank you but how are they installed in each folder

It's a helper utility that comes with node. The idea is that packages can define executable scripts in package.json that if installed globally, will be added to your PATH. When installing packages to local directory without the -g / --global flag, the executables are copied under ./node_modules/.bin. npx runs the scripts from this hidden dir but it also has a more powerful feature to transparently download a node module (to npm's cache directory under your home dir) with the given name and execute it.

Example of the transparent download and execute use case for those who are curious:

cat package.json | npx json2yaml
1 Like

Just create the folder, say node-red_1.1, change into it, and execute npm install node-red@1.1 in there.

You could do an npm init there first, to create a package.json to pin the dependencies to the desired version.

I'd generally advice against global npm installs. Gets you into more trouble than it's worth.

1 Like

It is only in desperation that I gave it a try based on some example on the web that clearly does not work.

Do you know what is the easiest way of removing all trace of a global install under linux ?

Or should I start another topic !

npm remove -g node-red

still reports v1.1.1 and that breaks my flow. I will try and work with npx.