System $ environmental variables empty result

Hi, i am running Node-RED on Ubuntu. In a bash script i export a variable like this:

export TEST=TEST

or

export TEST=TEST >> ~/.bashrc

when i typ printenv in de commandline i can see the variable is set

JOURNAL_STREAM=9:45470
XDG_DATA_DIRS=/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
GDMSESSION=ubuntu
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
TEST=TEST

The problem is that for some variables the payload results are empty. In Node-RED with the inject node i have set the
$TEST -> result empty
$JOURNAL_STREAM -> result not empty
$USER -> result not empty
$GDMSESSION -> result empty

Can someone explain how node-RED handles the system $ environmental variables?


How are you running Node-RED ? Is it in the same session you set that variable in ?

Yes, Node-RED is running locally the bash script and Node-RED installation is running in the same session/user. But it is not only the custom exported environment variabel that has a empty payload result but also for example the by default set "$GDMSESSION " variable is empty

How are you starting node-red after checking printenv in the command line?

i have set sudo systemctl enable nodered.service if that's what you mean

So node red is not running the same session where you are checking the environment. It is running in a systemd session. Those environment variables are probably not set in that environment. For example GDMSESSION will not be set because node-red does not have a display attached. Similarly .bashrc is probably not run for that session.

You can set environment variables in the systemd script if that is appropriate for your needs.

1 Like

so yes Node-RED is already running so it's environment has been set when it started. Any variables set after starting won't be updated. If you do a node-red-stop to stop that - then set your variable then just node-red (not node-red-start) - so it's started in the local environment it should then report correctly.

1 Like

Some good insights in this previous discussion.

1 Like

If you need to be able to specify environment variables for Node-RED, you can easily do so by tweaking the systemd service file. You will find examples in alternate-node-red-installer which I am, in fact, just updating.

You can provide an environment file as well which will be easier to amend if you need to make occasional changes. Of course, you still have to restart Node-RED each time you change them.

You can also change Node-RED's environment variables by setting them using node.js at the top of the settings.js file.

1 Like

Ah thank you that works indeed. now i have to look how to get it working in systemd, with the examples in this post below

I've just pushed some changes to GitHub so do have a look at the version there. The whole repo is a bit rough and ready but it at least points the way.