# How to set env variable

**URL:** https://discourse.nodered.org/t/how-to-set-env-variable/18612
**Category:** General
**Created:** [29 November 2019 14:33 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612 "2019-11-29T14:33:28Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![DemonKing](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@DemonKing](https://discourse.nodered.org/u/DemonKing)
#### Post date: [29 November 2019 14:33 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/1 "2019-11-29T14:33:28Z")

</div>

env.get() working  
but env.set() is not a function like flow.set(),global.set()ext,

so please find me how to set env variable

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [29 November 2019 14:36 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/2 "2019-11-29T14:36:36Z")

</div>

You cannot set environment variables at runtime. They are only evaluated when flows are started.

---

<div class="post-metadata">

### Author: ![DemonKing](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@DemonKing](https://discourse.nodered.org/u/DemonKing)
#### Post date: [29 November 2019 14:38 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/3 "2019-11-29T14:38:50Z")

</div>

then please told me how to do that

---

<div class="post-metadata">

### Author: ![DemonKing](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@DemonKing](https://discourse.nodered.org/u/DemonKing)
#### Post date: [29 November 2019 14:43 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/4 "2019-11-29T14:43:09Z")

</div>

i am pass one variable from front end ok then i used that value into some nodes as from knowledge nodes suporting only env varibales so i want to set that front end varible to one env varibale .. so what can i do for that

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [29 November 2019 14:44 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/5 "2019-11-29T14:44:18Z")

</div>

One way of setting environment variables is in your `settings.js` file.

The following will set the env var `MY_ENV_VAR`:

```auto
process.env.MY_ENV_VAR = "Hello World";

```

You can add lines like that to the top of your settings file.

The other option is to set them completely outside of Node-RED in shell you are running Node-RED in. How you do that will depend on your OS and how you are running Node-RED.

---

<div class="post-metadata">

### Author: ![DemonKing](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@DemonKing](https://discourse.nodered.org/u/DemonKing)
#### Post date: [29 November 2019 14:47 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/6 "2019-11-29T14:47:51Z")

</div>

no this way is also looking like a static ri8 .. i just want a dynamic way

then why cant use like this inside function

process.env.myvar = "abc";

error: precess is not defined;

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [29 November 2019 15:16 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/7 "2019-11-29T15:16:48Z")

</div>

You cannot change env vars dynamically. We do not expose the `process` object in the Function node. That is just how it is.

---

<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 November 2019 20:15 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/8 "2019-11-29T20:15:33Z")

</div>

@DemonKing if you tell us which node or nodes you are trying to change at run time we may be able to help.

---

<div class="post-metadata">

### Author: ![DemonKing](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@DemonKing](https://discourse.nodered.org/u/DemonKing)
#### Post date: [30 November 2019 05:28 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/9 "2019-11-30T05:28:40Z")

</div>

Node-red-contrib-mssql- plus

---

<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: [30 November 2019 07:19 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/10 "2019-11-30T07:19:50Z")

</div>

Which fields?

---

<div class="post-metadata">

### Author: ![DemonKing](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@DemonKing](https://discourse.nodered.org/u/DemonKing)
#### Post date: [30 November 2019 17:33 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/11 "2019-11-30T17:33:14Z")

</div>

All fields of conection like ip dbname everything I want to use dynamic

---

<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: [30 November 2019 18:24 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/12 "2019-11-30T18:24:03Z")

</div>

I haven't examined the code of that node, but I imagine it is written to connect to the database on startup and does not have the code to switch to a different db. Therefore even if you could change environment vars at runtime the node does not have the code to disconnect from one and connect to another.  
What you are asking for is a new feature of the node, not of node-red.

---

<div class="post-metadata">

### Author: ![DemonKing](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@DemonKing](https://discourse.nodered.org/u/DemonKing)
#### Post date: [1 December 2019 05:47 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/13 "2019-12-01T05:47:20Z")

</div>

Yes I think so.. but in my project I had this problem.. we are using entire project for node red.... So what's the solution for my problem .. please let me know

---

<div class="post-metadata">

### Author: ![DemonKing](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@DemonKing](https://discourse.nodered.org/u/DemonKing)
#### Post date: [1 December 2019 05:53 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/14 "2019-12-01T05:53:55Z")

</div>

But I hope if I can change the env variable run time it's should be work for me.. bcz If I give. A env variable given for the ipout for that node.. it's working fine

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [1 December 2019 08:13 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/15 "2019-12-01T08:13:15Z")

</div>

@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 you are short of options other than creating a custom node of your own.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [1 December 2019 08:21 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/16 "2019-12-01T08:21:22Z")

</div>

(Another of my dumb questions)

Though I am not using data bases and that stuff.

Why an `env` variable anyway? Why can't a `global` variable be used?  
That seems more the right thing for the job.

If it needs to be changed and _all to see it_ - in all flows - then surly a `global` variable (context) is just what the doctor ordered.

---

<div class="post-metadata">

### Author: ![DemonKing](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@DemonKing](https://discourse.nodered.org/u/DemonKing)
#### Post date: [1 December 2019 08:27 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/17 "2019-12-01T08:27:58Z")

</div>

I lready tried global variables but I am pretty sure node config allows env variables only

---

<div class="post-metadata">

### Author: ![DemonKing](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@DemonKing](https://discourse.nodered.org/u/DemonKing)
#### Post date: [1 December 2019 08:28 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/18 "2019-12-01T08:28:41Z")

</div>

Could you tell me how to create a custom nodes .. just told me the option how to do that

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [1 December 2019 08:31 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/19 "2019-12-01T08:31:25Z")

</div>

> [@DemonKing](#):
>
> but I am pretty sure node config allows env variables only

Hey, I'm not expert, but that doesn't sound right.

As I said, I don't use databases. I some how can't get my head around them and I have been trying for 30 years - there about.

Explain the mechanics of what you are doing. I am looking at your second post and can't work out what you really mean.

Node-Red must be able to use the data base, otherwise there wouldn't be a node for it.

I can't believe that settings which allow/need to be changed can't be access from Node-Red's working system.

> [@DemonKing](#):
>
> All fields of conection like ip dbname everything I want to use dynamic

So? Use DNS and access it by the name rather than the IP.

Your local network would/should have a DNS server.

Somewhere in your RPI settings you can get it to also publish (bad word, but....) its name and IP together.

That way rather than needing to enter the IP address, you enter the name.  
The name gets sent to the DNS and gets translated to the IP.

Problem solved - I think.  
Or it should be to the best of my knowledge.

---

<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: [1 December 2019 09:49 UTC](https://discourse.nodered.org/t/how-to-set-env-variable/18612/20 "2019-12-01T09:49:14Z")

</div>

The possible use of global vars is a red herring. The problem is not whether the node is capable of getting config from global or env vars, it is whether the node can change its connection to a different database without restarting or deploying node red, which it apparently can't.

[Next page](https://discourse.nodered.org/t/how-to-set-env-variable/18612.md?page=2)
