# Can I set the initialization value of node properties through environment variables?

**URL:** <https://discourse.nodered.org/t/can-i-set-the-initialization-value-of-node-properties-through-environment-variables/50325>\
**Category:** Developing Nodes\
**Created:** [28 August 2021 11:30 UTC](https://discourse.nodered.org/t/can-i-set-the-initialization-value-of-node-properties-through-environment-variables/50325 "2021-08-28T11:30:36Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![chiche](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chiche/32/35401_2.png) [@chiche](https://discourse.nodered.org/u/chiche)\
**Post date:** [28 August 2021 11:30 UTC](https://discourse.nodered.org/t/can-i-set-the-initialization-value-of-node-properties-through-environment-variables/50325/1 "2021-08-28T11:30:36Z")

</div>

Can I set the initialization value of node properties through environment variables?

![init](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/49fd32bf9409323b3e428a60902a9330f10afc60.png)

---

<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:** [28 August 2021 11:55 UTC](https://discourse.nodered.org/t/can-i-set-the-initialization-value-of-node-properties-through-environment-variables/50325/2 "2021-08-28T11:55:12Z")

</div>

Some yes - from the documentation...

> Any node property can be set with an environment variable by setting its value to a string of the form `${ENV_VAR}` . When the runtime loads the flows, it will substitute the value of that environment variable before passing it to the node.
> 
> This only works if it replaces the entire property - it cannot be used to substitute just part of the value. For example, it is _not_ possible to use `CLIENT-${HOST}` .
> 
> As nodes provide their own edit dialog, not all properties will provide a text input that can be used to enter the env-var string. In that case, you may consider hand-editing the flow file to set the property.

[https://nodered.org/docs/user-guide/environment-variables](https://nodered.org/docs/user-guide/environment-variables)

---

<div class="post-metadata">

**Author:** ![chiche](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chiche/32/35401_2.png) [@chiche](https://discourse.nodered.org/u/chiche)\
**Post date:** [30 August 2021 01:46 UTC](https://discourse.nodered.org/t/can-i-set-the-initialization-value-of-node-properties-through-environment-variables/50325/3 "2021-08-30T01:46:17Z")

</div>

Thanks a lot!  
I tried all the ways but I can't

```auto
RED.nodes.registerType("my-node", {
    color: "rgb(231, 231, 174)",
    category: "test",
    defaults: {
      name: { value: `${global_name}` }
    },

```

---

<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:** [30 August 2021 02:04 UTC](https://discourse.nodered.org/t/can-i-set-the-initialization-value-of-node-properties-through-environment-variables/50325/4 "2021-08-30T02:04:41Z")

</div>

Hi. It wasn't clear you meant as a programmer. I thought you meant as a user.

See the below link for accessing node env vars ...

"How to read environment variables from Node.js" [https://nodejs.dev/learn/how-to-read-environment-variables-from-nodejs](https://nodejs.dev/learn/how-to-read-environment-variables-from-nodejs)

---

<div class="post-metadata">

**Author:** ![chiche](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chiche/32/35401_2.png) [@chiche](https://discourse.nodered.org/u/chiche)\
**Post date:** [30 August 2021 02:23 UTC](https://discourse.nodered.org/t/can-i-set-the-initialization-value-of-node-properties-through-environment-variables/50325/5 "2021-08-30T02:23:17Z")

</div>

Yes, I tried nodejs env (process.env.global\_name) in file my\_node.js ok but try in my\_node.html it doesn't work.  
Browser error 'process is not defined'

```auto
RED.nodes.registerType("my-node", {
    color: "rgb(231, 231, 174)",
    category: "test",
    defaults: {
      name: { value: `${process.env.global_name}` }
    },

```

---

<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:** [30 August 2021 05:13 UTC](https://discourse.nodered.org/t/can-i-set-the-initialization-value-of-node-properties-through-environment-variables/50325/6 "2021-08-30T05:13:51Z")

</div>

There is no need for the template literal, you can use the value directly.

Try this...

```auto
console.log("process.env.global_name", process.env.global_name);
RED.nodes.registerType("my-node", {
    color: "rgb(231, 231, 174)",
    category: "test",
    defaults: {
      name: { value: process.env.global_name}
    },

```

And see what the console reports.

Ps. Are you sure the terminal / service / env you use to start node-red actually has `global_name` set?

## Edit.....

Aaaaahhhhhhhggggggghhhhhhh

I just realised. This is client side code.

The browser will never have access to either the users computer or the node-red servers environment.

---

<div class="post-metadata">

**Author:** ![chiche](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chiche/32/35401_2.png) [@chiche](https://discourse.nodered.org/u/chiche)\
**Post date:** [30 August 2021 07:21 UTC](https://discourse.nodered.org/t/can-i-set-the-initialization-value-of-node-properties-through-environment-variables/50325/7 "2021-08-30T07:21:03Z")

</div>

Yes, it's client side, sorry for not making this clear

---

<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:** [30 August 2021 07:40 UTC](https://discourse.nodered.org/t/can-i-set-the-initialization-value-of-node-properties-through-environment-variables/50325/8 "2021-08-30T07:40:06Z")

</div>

There is _kinda_ a way.

You create an admin endpoint in the js file then call that endpoint in `onedirprepare` client side code. The server-side code can then access and return vars that reside in the servers environment.

Look at node-red src code in the serial node for and example of how to.

---

<div class="post-metadata">

**Author:** ![chiche](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chiche/32/35401_2.png) [@chiche](https://discourse.nodered.org/u/chiche)\
**Post date:** [30 August 2021 08:48 UTC](https://discourse.nodered.org/t/can-i-set-the-initialization-value-of-node-properties-through-environment-variables/50325/9 "2021-08-30T08:48:38Z")

</div>

> [@Steve-Mcl](#):
>
> You create an admin endpoint in the js file then call that endpoint in `onedirprepare` client side code. The server-side code can then access and return vars that reside in the servers environment.
> 
> Look at node-red src code in the serial node for and example of how to.

oneditprepare() only works when double-click to open node. In this case I just select node and look at the node property, that function is not called, so this way is not fixable

---

<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:** [30 August 2021 09:00 UTC](https://discourse.nodered.org/t/can-i-set-the-initialization-value-of-node-properties-through-environment-variables/50325/10 "2021-08-30T09:00:18Z")

</div>

Correct, but why is that a problem. Typically when a user adds a node into the editor they open it to set up.

---

<div class="post-metadata">

**Author:** ![chiche](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chiche/32/35401_2.png) [@chiche](https://discourse.nodered.org/u/chiche)\
**Post date:** [30 August 2021 09:02 UTC](https://discourse.nodered.org/t/can-i-set-the-initialization-value-of-node-properties-through-environment-variables/50325/11 "2021-08-30T09:02:47Z")

</div>

I am doing the following way

```auto
RED.events.on("view:selection-changed", function (selections) {
Call to server to get global_name = process.env.global_name
Set my_node.global_name = global_name;
RED.sidebar.info.refresh();
});

```

I checked it was running but I don't know if there is any risk

---

<div class="post-metadata">

**Author:** ![chiche](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chiche/32/35401_2.png) [@chiche](https://discourse.nodered.org/u/chiche)\
**Post date:** [30 August 2021 09:12 UTC](https://discourse.nodered.org/t/can-i-set-the-initialization-value-of-node-properties-through-environment-variables/50325/12 "2021-08-30T09:12:53Z")

</div>

I see the problem is this event is called too many times, do you know another event to determine the node is being initialized?  
**EDIT**  
I decided to use the event "nodes:add" and I think it is ok

---

<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:** [29 October 2021 09:13 UTC](https://discourse.nodered.org/t/can-i-set-the-initialization-value-of-node-properties-through-environment-variables/50325/13 "2021-10-29T09:13:19Z")

</div>

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