# System $ environmental variables empty result

**URL:** <https://discourse.nodered.org/t/system-environmental-variables-empty-result/38212>\
**Category:** General\
**Created:** [29 December 2020 14:59 UTC](https://discourse.nodered.org/t/system-environmental-variables-empty-result/38212 "2020-12-29T14:59:51Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![coen17st](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/coen17st/32/34027_2.png) [@coen17st](https://discourse.nodered.org/u/coen17st)\
**Post date:** [29 December 2020 14:59 UTC](https://discourse.nodered.org/t/system-environmental-variables-empty-result/38212/1 "2020-12-29T14:59:51Z")

</div>

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

```auto
export TEST=TEST

```

or

```auto
export TEST=TEST >> ~/.bashrc

```

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

```auto
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?

 ![test](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/8/381f7b547838f9749b33173e4c218b8a63523b7c.png)  
 ![js](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/3/a39e433643e5c39009bc8d751b85e4a5bc5e0df5.png)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [29 December 2020 15:27 UTC](https://discourse.nodered.org/t/system-environmental-variables-empty-result/38212/2 "2020-12-29T15:27:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![coen17st](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/coen17st/32/34027_2.png) [@coen17st](https://discourse.nodered.org/u/coen17st)\
**Post date:** [29 December 2020 15:28 UTC](https://discourse.nodered.org/t/system-environmental-variables-empty-result/38212/3 "2020-12-29T15:28:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 December 2020 15:53 UTC](https://discourse.nodered.org/t/system-environmental-variables-empty-result/38212/4 "2020-12-29T15:53:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![coen17st](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/coen17st/32/34027_2.png) [@coen17st](https://discourse.nodered.org/u/coen17st)\
**Post date:** [29 December 2020 16:18 UTC](https://discourse.nodered.org/t/system-environmental-variables-empty-result/38212/5 "2020-12-29T16:18:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 December 2020 16:25 UTC](https://discourse.nodered.org/t/system-environmental-variables-empty-result/38212/6 "2020-12-29T16:25:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [29 December 2020 16:27 UTC](https://discourse.nodered.org/t/system-environmental-variables-empty-result/38212/7 "2020-12-29T16:27:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [29 December 2020 16:31 UTC](https://discourse.nodered.org/t/system-environmental-variables-empty-result/38212/8 "2020-12-29T16:31:32Z")

</div>

> [@coen17st](#):
>
> Can someone explain how node-RED handles the system $ environmental variables?

Some good insights in this previous discussion.

> [@How to set env variable](https://discourse.nodered.org/t/how-to-set-env-variable/18612/15):
>
> @DemonKing environment variables are only evaluated when flows are started. The runtime gets their values before passing them to individual nodes. The nodes don't know the value came from an env var. If you change an env var it does not magically cause the node to change its configuration. You'd need to restart Node-RED to pickup the changes. I understand you have a requirement to dynamically change the mssql connection details. Unless there is a node that already provides that ability, then y…

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [29 December 2020 16:42 UTC](https://discourse.nodered.org/t/system-environmental-variables-empty-result/38212/9 "2020-12-29T16:42:27Z")

</div>

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](https://github.com/TotallyInformation/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.

---

<div class="post-metadata">

**Author:** ![coen17st](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/coen17st/32/34027_2.png) [@coen17st](https://discourse.nodered.org/u/coen17st)\
**Post date:** [29 December 2020 17:26 UTC](https://discourse.nodered.org/t/system-environmental-variables-empty-result/38212/10 "2020-12-29T17:26:23Z")

</div>

> [@dceejay](#):
>
> 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.

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

> [@TotallyInformation](#):
>
> 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](https://github.com/TotallyInformation/alternate-node-red-installer) which I am, in fact, just updating.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [29 December 2020 21:04 UTC](https://discourse.nodered.org/t/system-environmental-variables-empty-result/38212/11 "2020-12-29T21:04:26Z")

</div>

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.
