# Scope of constants

**URL:** https://discourse.nodered.org/t/scope-of-constants/97629
**Category:** General
**Created:** [15 June 2025 06:38 UTC](https://discourse.nodered.org/t/scope-of-constants/97629 "2025-06-15T06:38:26Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Janvi](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@Janvi](https://discourse.nodered.org/u/Janvi)
#### Post date: [15 June 2025 06:38 UTC](https://discourse.nodered.org/t/scope-of-constants/97629/1 "2025-06-15T06:38:26Z")

</div>

if I define any constant like

const value = {val:123};

its only known inside same node. Is there any possibility to put this definition to flow-scope without using context variable by flow.set ?

---

<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: [15 June 2025 07:08 UTC](https://discourse.nodered.org/t/scope-of-constants/97629/2 "2025-06-15T07:08:40Z")

</div>

Probably the preferred Node-red way is to attach the value as a message property, thus ensuring it is available in every node of the flow.

You can (if you must 🤭 ) set environment variables for the flow.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/2/c27084c0d63763fe0f54f20df73f463995b77c1e.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: [15 June 2025 07:33 UTC](https://discourse.nodered.org/t/scope-of-constants/97629/3 "2025-06-15T07:33:08Z")

</div>

can you describe the use case / provide a couple of examples of how you would use this constant?

I suspect environment variables are the right choice but you might not realise the various levels they can be set and how to utilise them in your flows.

---

<div class="post-metadata">

### Author: ![Janvi](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@Janvi](https://discourse.nodered.org/u/Janvi)
#### Post date: [15 June 2025 09:21 UTC](https://discourse.nodered.org/t/scope-of-constants/97629/4 "2025-06-15T09:21:24Z")

</div>

The constant definition is simply to make the code more readable and better maintainable for humans. Dont wonder about my odd question - as C and assembly programmer I am cought in too much concerns about the code performance.

Its always possible to take a variable with flow.set or pass as parameter to the next node what probably uses the stack. I must not take care that constants are not variable and never change. Obviously, the JIT compiler works completely diffrent to other language compilers, there is nothing like a preprocessor and I simply have to trust node.js. Im am going to try the env variables @jbudd suggested (although constants are not variable).

---

<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: [15 June 2025 09:38 UTC](https://discourse.nodered.org/t/scope-of-constants/97629/5 "2025-06-15T09:38:14Z")

</div>

> [@Janvi](#):
>
> env variables @jbudd suggested (although constants are not variable).

Yeah I've always found the name a little curious. Environment variables are 100% not variable 😂

Perhaps they should be renamed environment constants 🤔

---

<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: [15 June 2025 09:40 UTC](https://discourse.nodered.org/t/scope-of-constants/97629/6 "2025-06-15T09:40:01Z")

</div>

Ps, there are some useful tips on various ways env vars can be used in unexpected ways in the docs: [Using environment variables : Node-RED](https://nodered.org/docs/user-guide/environment-variables)

---

<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: [15 June 2025 10:03 UTC](https://discourse.nodered.org/t/scope-of-constants/97629/7 "2025-06-15T10:03:51Z")

</div>

> [@Janvi](#):
>
> The constant definition is simply to make the code more readable and better maintainable for humans.

> [@Janvi](#):
>
> the env variables @jbudd suggested

"suggested" is a bit strong. I think that the way environment variables are implemented in Node-red offers confusion over immutable "variables" and boundless scope for subtle bugs.

Which approach gives more easily maintainable code?

```auto
// embedded magic numbers are very very bad
let energy = msg.mass * 90000000000000000  

// Environment variables are very bad.
// Where was this environment variable declared = node / group / flow / environment file / settings.js / operating system?
energy = msg.mass * env.get("csquared") 

// global context variables are quite bad (except for real physical constants)
energy = msg.mass * global.get("csquared")

// msg.properties are easier to track down
// where did msg.csquared get a value?
energy = msg.mass * msg.csquared  

```

---

<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: [15 June 2025 15:59 UTC](https://discourse.nodered.org/t/scope-of-constants/97629/8 "2025-06-15T15:59:41Z")

</div>

But note that a constant referring to an object, such as  
`const value = {val:123}`  
Is a constant reference to an object, not a reference to a constant object. That means that one cannot then write  
`value = 3`  
But one _can_ write  
`value.val = 7`

---

<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: [15 June 2025 23:09 UTC](https://discourse.nodered.org/t/scope-of-constants/97629/9 "2025-06-15T23:09:36Z")

</div>

> [@Steve-Mcl](#):
>
> Yeah I've always found the name a little curious. Environment variables are 100% not variable 😂
> 
> Perhaps they should be renamed environment constants 🤔

Urm, env variables absolutely ARE variable - just not always inside an app. They are certainly variable at the OS/shell level. Which, of course, is where they originated.

Unfortunately, they are not variable inside a node.js app.

---

<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: [15 June 2025 23:13 UTC](https://discourse.nodered.org/t/scope-of-constants/97629/10 "2025-06-15T23:13:08Z")

</div>

> [@Colin](#):
>
> Is a constant reference to an object, not a reference to a constant object

And using objects in context variables can also solve one of jbudd's gripes - what set the data? Simple, just add another property and record that when you write it. I do this regularly for the reason mentioned.

---

<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: [13 September 2025 23:13 UTC](https://discourse.nodered.org/t/scope-of-constants/97629/11 "2025-09-13T23:13:53Z")

</div>

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