# Using environment variables

**URL:** https://discourse.nodered.org/t/using-environment-variables/81975
**Category:** General
**Created:** [12 October 2023 15:53 UTC](https://discourse.nodered.org/t/using-environment-variables/81975 "2023-10-12T15:53:24Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![GretaC](https://avatars.discourse-cdn.com/v4/letter/g/bb73d2/32.png) [@GretaC](https://discourse.nodered.org/u/GretaC)
#### Post date: [12 October 2023 15:53 UTC](https://discourse.nodered.org/t/using-environment-variables/81975/1 "2023-10-12T15:53:24Z")

</div>

Hi,  
I try to use my own environment variables in sub-flows. That's my goal and I fail.  
So I go back to a very simple 'flow' with nothing but a Inject node and a debug node.

I set up the Inject node and replace the default 'timestamp' by 'Environment' ('Umgebungsvariable' in my German setup) and ask for "USER". The answer I get is 'root' and that's what I expect because "USER" is set in Linux and I am 'root' here.

I replace 'USER' by 'NR\_NODE\_ID' and I get a long Hex-Value.

Now I ask for 'PORT' or 'uiPort' or 'flowFile' or anything else available in 'settings.js' and here I get nothing than empty strings: "".

My understanding is that settings in 'settings.js' under 'module.exports' are treated as 'Environment Variables'.

Can anybody help me here? Because as long as this simple stuff is not working it makes no sense to play with 'env.get("something")....

---

<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: [12 October 2023 16:38 UTC](https://discourse.nodered.org/t/using-environment-variables/81975/2 "2023-10-12T16:38:56Z")

</div>

> [@GretaC](#):
>
> The answer I get is 'root' and that's what I expect because "USER" is set in Linux and I am 'root' here.

That is very bad practice and will likely cause you other issues later on.

> [@GretaC](#):
>
> Now I ask for 'PORT' or 'uiPort' or 'flowFile' or anything else available in 'settings.js' and here I get nothing than empty strings: "".

What set these env variables? They need to have been set before Node-RED starts for them to be visible to Node-RED.

> [@GretaC](#):
>
> My understanding is that settings in 'settings.js' under 'module.exports' are treated as 'Environment Variables'.

No, they exist on an object called `RED` which is available in a function node. They are only available as env variables if they were set as such prior to the launch of Node-RED.

---

<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: [12 October 2023 16:47 UTC](https://discourse.nodered.org/t/using-environment-variables/81975/3 "2023-10-12T16:47:20Z")

</div>

I think it _should_ be `RED.util.getSetting()` but I have to admit that I can't get that to work at all right now.

---

<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: [12 October 2023 16:52 UTC](https://discourse.nodered.org/t/using-environment-variables/81975/4 "2023-10-12T16:52:27Z")

</div>

Seems that, while `RED.util.getSetting()` certainly exists when called from a function node (you can look it up with `Object.keys(RED.util)`, it does not actually work. @knolleary or @Steve-Mcl - is that _supposed_ to work in a function node? Sorry, I can never remember and I can't seem to look it up.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [12 October 2023 18:43 UTC](https://discourse.nodered.org/t/using-environment-variables/81975/5 "2023-10-12T18:43:14Z")

</div>

I suspect that the environment variable $USER is inherited by Node-red from the shell that starts it up. Other Bash variables such as $PWD are available too.

The syntax for setting an environment variable in settings.js is like this - making IP and HOSTNAME available withing Node-red:

```auto
 hostname = require('os').hostname();
 const execSync = require('child_process').execSync;
 const ipaddress = execSync('/home/pi/bin/ipaddress', { encoding: 'utf-8' });
 process.env.IP = ipaddress;
 process.env.HOSTNAME = hostname;

```

I don't know why I have require('os').hostname() for one OS command but execSync('/home/pi/bin/ipaddress', { encoding: 'utf-8' }) for another!

---

<div class="post-metadata">

### Author: ![GretaC](https://avatars.discourse-cdn.com/v4/letter/g/bb73d2/32.png) [@GretaC](https://discourse.nodered.org/u/GretaC)
#### Post date: [13 October 2023 08:55 UTC](https://discourse.nodered.org/t/using-environment-variables/81975/6 "2023-10-13T08:55:39Z")

</div>

@TotallyInformation: I know about the risk to run programs as 'root'. My test- Node-Red is running inside a proxmox - LXC, so the risk is limited.

Thank you all for your input, but I still do not know how to introduce an environment variable.

To make long story short:

I want to have a flow with two nodes:

- Inject node with msg.payload = $MY\_ENV\_VAR
- A debug node to show the value of MY\_ENV\_VAR

Then I want to assign "Hello World" to MY\_EǸV\_VAR and want to see "Hello World" in the right hand debug area if I press the Inject.

Pretty simple. Isn't it ? 😀

BUT HOW DOES THIS WORK ???

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [13 October 2023 08:59 UTC](https://discourse.nodered.org/t/using-environment-variables/81975/7 "2023-10-13T08:59:22Z")

</div>

only the items listed in the code completion are relevant to the function node

![chrome_OnbdZX3OoI](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/6/266267defc6c96fee12b28633fd8d97f620fbc53.gif)

Also listed here: [@node-red/util\_util - Documentation](https://nodered.org/docs/api/modules/v/0.20.0/@node-red_util_util.html)

Anything else should be considered private.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [13 October 2023 09:13 UTC](https://discourse.nodered.org/t/using-environment-variables/81975/8 "2023-10-13T09:13:43Z")

</div>

> [@GretaC](#):
>
> Then I want to assign "Hello World" to MY\_EǸV\_VAR and want to see "Hello World" in the right hand debug area if I press the Inject.
> 
> Pretty simple. Isn't it ? 😀

Can you explain why you want to do this with an environment variable rather than a context variable?

A simple request, not a veiled criticism 😃

---

<div class="post-metadata">

### Author: ![GretaC](https://avatars.discourse-cdn.com/v4/letter/g/bb73d2/32.png) [@GretaC](https://discourse.nodered.org/u/GretaC)
#### Post date: [13 October 2023 09:25 UTC](https://discourse.nodered.org/t/using-environment-variables/81975/9 "2023-10-13T09:25:47Z")

</div>

Yes. No problem.  
As mentioned in the initial post the goal is to work with Subflows. And my understandig is that these Subflows are managed by Environment Variables.

> **Subflow Instance properties**  
> Since 0.20, Subflows can be configured with instance properties. These appear as environment variables within the Subflow and can be customised for individual instances of the subflow.

That's the reason why.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [13 October 2023 09:30 UTC](https://discourse.nodered.org/t/using-environment-variables/81975/10 "2023-10-13T09:30:13Z")

</div>

Thanks 😃

I've never used subflows hence I didn't know about that.

---

<div class="post-metadata">

### Author: ![GretaC](https://avatars.discourse-cdn.com/v4/letter/g/bb73d2/32.png) [@GretaC](https://discourse.nodered.org/u/GretaC)
#### Post date: [13 October 2023 10:04 UTC](https://discourse.nodered.org/t/using-environment-variables/81975/11 "2023-10-13T10:04:40Z")

</div>

@all: I think I got it.  
I have to define the environment variable when creating the subflow.  
If the subflow is done, I can use it. And when using it I can adapt the values of the environment variables. They appear as properties in the subflow-instance. And that's the way to customize the subflow, (if necessary).

Another way is to transfer information via 'payload'.  
But this is much more complicated. The way to use environment variables is much smarter and in line with the general handling of all other nodes.

Btw: my understanding is a little bit different from the way they are used here. But perhaps it is consistent within node red....

Thank's a lot.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [13 October 2023 10:20 UTC](https://discourse.nodered.org/t/using-environment-variables/81975/12 "2023-10-13T10:20:39Z")

</div>

Can you post an example of the way you can adapt the values of the environment variables in the subflow to help educate me and anyone else new to subflows?

So far I have managed to define environment variables in the subflow's config and to access them (and $parent flow variables) in the subflow code.

---

<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: [13 October 2023 10:59 UTC](https://discourse.nodered.org/t/using-environment-variables/81975/13 "2023-10-13T10:59:17Z")

</div>

Just to wrap up the question about access to settings.js properties inside node-red. If you are not writing a custom node, your only choice is to make a change to settings.js like this (which I do anyway always):

```auto
const nrsettings = {
  // All the existing settings go in here rather than exporting the object directly
  // ...
}

// Splitting the export this way allows us to dynamically override settings if we want to
// Or, as in this case, to pass a setting into Node-RED
nrsettings.functionGlobalContext._port = nrsettings.uiPort

module.exports = nrsettings

```

---

<div class="post-metadata">

### Author: ![GretaC](https://avatars.discourse-cdn.com/v4/letter/g/bb73d2/32.png) [@GretaC](https://discourse.nodered.org/u/GretaC)
#### Post date: [13 October 2023 12:36 UTC](https://discourse.nodered.org/t/using-environment-variables/81975/14 "2023-10-13T12:36:34Z")

</div>

@jbudd  
My project is for Smart Home.  
The current task was to display and track gauges for nominal and actual temperature and flash a 'LED' if any window in the room is open. An open window reduces the nominal temperature.

And I want to track the state of the window. I want to know if it is open and I want to know how long it is open. The state will be sent to a log file and I can get an information via email about the change of the state. But I don't want to get an email all the time, so I can switch off email support.

For a room, let's say the office, I have one thermostat and two windows.

This is the flow I started with

 ![Bildschirmfoto vom 2023-10-13 13-58-54](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/a/6a6f97b72ee48074827744e5ed3bad633ce1a1c7.png)

For all windows (10) in all rooms I have a very similar flow. They only differ in the name of the room, window or door, and in a 'Subject' for the email.

So I select some nodes and transfer these nodes into a Subflow.

 ![Bildschirmfoto vom 2023-10-13 13-59-33](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/4/643e0c113e6d80332c8dffc977cd71ec316ae5ae.png)

If I double click the subflow I get this:

 ![Bildschirmfoto vom 2023-10-13 14-14-52](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/9/e9be1aa34678fca61ffef4d612ff4ff23b210af9.png)

When selecting the properties I can add environment variables:

 ![Bildschirmfoto vom 2023-10-13 14-18-33](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/1/c184d6d8558a91ea69729eafe955326e1b05cfd2.png)

Now I go back to my application and select 'Subflow 1'.

 ![Bildschirmfoto vom 2023-10-13 14-21-53](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/1/f1c52e7690e6fe0a472f6cfcfe44c3f30175110a.png)

I modify my three values

 ![Bildschirmfoto vom 2023-10-13 14-25-26](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/1/b1aaa714aaa5b615394095d0b93c19b734addda6.png)

and do the same for the other window.

 ![Bildschirmfoto vom 2023-10-13 14-25-42](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/0/105f7ad4a921115ca824c5262ae3aaff40cb558d.png)

That's it.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [13 October 2023 19:43 UTC](https://discourse.nodered.org/t/using-environment-variables/81975/15 "2023-10-13T19:43:34Z")

</div>

Thanks, a careful detailed explanation. I'm studying it.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [27 October 2023 19:43 UTC](https://discourse.nodered.org/t/using-environment-variables/81975/16 "2023-10-27T19:43:41Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
