# Env variable at runtime?

**URL:** <https://discourse.nodered.org/t/env-variable-at-runtime/80335>\
**Category:** General\
**Created:** [5 August 2023 13:41 UTC](https://discourse.nodered.org/t/env-variable-at-runtime/80335 "2023-08-05T13:41:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pacmanmt](https://avatars.discourse-cdn.com/v4/letter/p/dfb087/32.png) [@pacmanmt](https://discourse.nodered.org/u/pacmanmt)\
**Post date:** [5 August 2023 13:41 UTC](https://discourse.nodered.org/t/env-variable-at-runtime/80335/1 "2023-08-05T13:41:05Z")

</div>

Maybe it is only a silly question due to my dumbness in programming.  
Is it possible to define an env variable inside a flow so that other flows can access it?

Thank you in advance for helping out

---

<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:** [5 August 2023 13:50 UTC](https://discourse.nodered.org/t/env-variable-at-runtime/80335/2 "2023-08-05T13:50:54Z")

</div>

This is perhaps not quite what you mean but you can define global context variables, for example a change node can set global.ipaddress.  
Once set, this variable is accessible in all flows, eg a change node can set msg.ipaddress to the value of global.ipaddress.

The function code to do this is

```auto
// set the value 
global.set("ipaddress", msg.payload)  

// use the value (in another flow)
msg.payload = global.get("ipaddress) || "0.0.0.0" // default 0.0.0.0

```

There can be context variables at flow (editor tab) level too. If you wanted to confine a variable to just a couple of tabs you could pass the value using link-out and link-in nodes

---

<div class="post-metadata">

**Author:** ![pacmanmt](https://avatars.discourse-cdn.com/v4/letter/p/dfb087/32.png) [@pacmanmt](https://discourse.nodered.org/u/pacmanmt)\
**Post date:** [5 August 2023 16:21 UTC](https://discourse.nodered.org/t/env-variable-at-runtime/80335/3 "2023-08-05T16:21:13Z")

</div>

Thanks Jbudd, but this won't work in my case.  
I need to set at runtime the serial port to which a serial node should connect to and I learnt that the only way to achieve it, is to use an env variable; the global variable cannot be used in this case.  
I can set statically the env variable on the specific flow, but I would like to set it the first time the flow is run instead.

---

<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:** [5 August 2023 16:39 UTC](https://discourse.nodered.org/t/env-variable-at-runtime/80335/4 "2023-08-05T16:39:07Z")

</div>

Even if you could set an env var at run time it still wouldn't do what you want. The point is that the serial port is configured, and connects, when node red is started (or deployed) so even if you set an env var later it still wouldn't affect the serial port.

---

<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:** [5 August 2023 18:22 UTC](https://discourse.nodered.org/t/env-variable-at-runtime/80335/5 "2023-08-05T18:22:58Z")

</div>

I didn't realise this was about your serial port question, I thought that was solved.

If you can't do it in Node-red then presumably you could have an external program to communicate over the serial ports, exchanging data with Node-red over MQTT?

---

<div class="post-metadata">

**Author:** ![pacmanmt](https://avatars.discourse-cdn.com/v4/letter/p/dfb087/32.png) [@pacmanmt](https://discourse.nodered.org/u/pacmanmt)\
**Post date:** [5 August 2023 18:52 UTC](https://discourse.nodered.org/t/env-variable-at-runtime/80335/6 "2023-08-05T18:52:13Z")

</div>

It makes sense now, thank you for clarifying this aspect I had not taken into account.

---

<div class="post-metadata">

**Author:** ![pacmanmt](https://avatars.discourse-cdn.com/v4/letter/p/dfb087/32.png) [@pacmanmt](https://discourse.nodered.org/u/pacmanmt)\
**Post date:** [5 August 2023 18:55 UTC](https://discourse.nodered.org/t/env-variable-at-runtime/80335/7 "2023-08-05T18:55:12Z")

</div>

The serial port configuration is actually solved and it is working fine.  
It was just a fixation of mine on doing it runtime, Colin clarified why it cannot work.

Thank you both for giving help to this dumb man, much appreciated  
😃

---

<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:** [4 October 2023 18:55 UTC](https://discourse.nodered.org/t/env-variable-at-runtime/80335/8 "2023-10-04T18:55:55Z")

</div>

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