# Stuck with \`function\` nodes and \`env\` setting up

**URL:** <https://discourse.nodered.org/t/stuck-with-function-nodes-and-env-setting-up/100913>\
**Category:** General\
**Tags:** function-node\
**Created:** [5 May 2026 12:57 UTC](https://discourse.nodered.org/t/stuck-with-function-nodes-and-env-setting-up/100913 "2026-05-05T12:57:16Z")\
**Posts on this page:** 9\
**Page:** 1

<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:** [5 May 2026 12:57 UTC](https://discourse.nodered.org/t/stuck-with-function-nodes-and-env-setting-up/100913/1 "2026-05-05T12:57:17Z")

</div>

Sorry folks, but I am stuck.

This is the code:

```auto
let divisor = parseInt(env.get("DIVISOR")) || 2

let counter = context.get("counter") || 0
counter = (counter + 1) % divisor
context.set("counter", counter)

if (counter === 0) {
    return msg
}
return null

```

But I am stumped on how to set the `env` part.

Obviously I'm missing something.

 ![Screenshot from 2026-05-05 22-56-19](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/d/9d97aacc3ac06cc27e47d0f1f262361bfd1b7cff.png)

This is what `chatGPT` gave me, but I am still not seeing the `env` being defined/declared.

```auto
[{"id":"f1c2d3e4a5b6c7d8","type":"function","z":"9b7e7466.a4b698","name":"Rate limiter (1 in N)","func":"let divisor = parseInt(env.get(\"DIVISOR\")) || 2;\n\nlet counter = context.get(\"counter\") || 0;\ncounter = (counter + 1) % divisor;\ncontext.set(\"counter\", counter);\n\nif (counter === 0) {\n return msg;\n}\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":4340,"y":3040,"wires":[[]]}]

```

---

<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:** [5 May 2026 13:35 UTC](https://discourse.nodered.org/t/stuck-with-function-nodes-and-env-setting-up/100913/2 "2026-05-05T13:35:43Z")

</div>

Env(ironment) variables are not set in the function node. The image you've shared is for installing npm packages (libraries) for use inside the function node.

You might want to try looking in the config for your _group_. It has a button that looks like a bulleted list. In there you can set an environment variable. You can also set them in sub-flows and in flow tabs. Alternatively, you can set one at the OS level before starting Node-RED.

---

<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 May 2026 14:11 UTC](https://discourse.nodered.org/t/stuck-with-function-nodes-and-env-setting-up/100913/3 "2026-05-05T14:11:42Z")

</div>

It does not make sense to me that you are using an environment variable for the divisor and a context variable for the counter, maybe it's more appropriate to use context variables for both.

If you really want to use an environment variable, there is no way to set it for a single function.

You can enclose the function within a group and set the variable for the group, but that would be easy to break if you happened to ungroup the group, not expecting it to change the program execution.

---

<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:** [5 May 2026 15:01 UTC](https://discourse.nodered.org/t/stuck-with-function-nodes-and-env-setting-up/100913/4 "2026-05-05T15:01:31Z")

</div>

> [@jbudd](#):
>
> maybe it's more appropriate to use context variables for both.

> [@jbudd](#):
>
> You can enclose the function within a group and set the variable for the group, but that would be easy to break if you happened to ungroup the group, not expecting it to change the program execution.

Completely agree. An env variable only makes sense if it is fairly static and you want to share it within a group or tab or sub-flow. For function nodes, you have the raw context variable which is already tied to that node.

Best way to think about env "variables" is to treat them as _constants_ and not variable at all. They are only potentially variable outside the context of Node-RED.

---

<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 May 2026 15:18 UTC](https://discourse.nodered.org/t/stuck-with-function-nodes-and-env-setting-up/100913/5 "2026-05-05T15:18:09Z")

</div>

> [@TotallyInformation](#):
>
> They are only potentially variable outside the context of Node-RED.

The **environment** within which Node-red runs (on a Pi) includes a bunch of **variables** including, for example $USER, $PWD and $PATH.  
These may be available within Node-red, but if so their value is **constant** , set at startup.

And then we have "environment variables" set within Node-red at flow, group or subflow level.  
These are indeed **variables** and are _an invention of the devil_, shun them! 😉  
Context variables should be available at these levels instead.

---

<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:** [5 May 2026 16:08 UTC](https://discourse.nodered.org/t/stuck-with-function-nodes-and-env-setting-up/100913/6 "2026-05-05T16:08:09Z")

</div>

> [@jbudd](#):
>
> These are indeed **variables** and are _an invention of the devil_, shun them! 😉  
> Context variables should be available at these levels instead.

They are still mostly static, pre-defined in their own contexts. But yes, I agree, there probably should have been new context variable contexts for group, flow tab and sub-flow. 😃 But there isn't so we take what we are given hey?!

---

<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:** [5 May 2026 20:45 UTC](https://discourse.nodered.org/t/stuck-with-function-nodes-and-env-setting-up/100913/7 "2026-05-05T20:45:22Z")

</div>

Yeah, ok, the _reasoning_ for me wanting to use an `env` is that I can change it as a variable.

Of course now - in retrospect - I would just define it at the top of the code.

**Mia Culpa folks.**

I just got hung up on something silly and applied it the wrong way.

Digression:  
Originally I wanted to _half_ the rate of messages getting through.

Bt then got the thought that maybe it could be _less_.  
_How do I make it that I just change ONE thing?_  
And somehow `env` snuck into my head and wouldn't get out.

Giving this code:

```auto
const divisor = 2

let counter = context.get("counter") || 0
counter = (counter + 1) % divisor
context.set("counter", counter)

if (counter === 0) {
    return msg
}
return null

```

Yes?  
🙂

---

<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:** [5 May 2026 23:58 UTC](https://discourse.nodered.org/t/stuck-with-function-nodes-and-env-setting-up/100913/8 "2026-05-05T23:58:24Z")

</div>

Not forgetting that you can also rate-limit message flows:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/0/70b3d750379742c6397987c12c8e66e31dd62923.png)

Though that assumes you can think of a sensible message rate.

---

<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:** [19 May 2026 23:59 UTC](https://discourse.nodered.org/t/stuck-with-function-nodes-and-env-setting-up/100913/9 "2026-05-19T23:59:18Z")

</div>

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