# Entering a global variable specification into settings.js

**URL:** <https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811>\
**Category:** General\
**Created:** [13 November 2023 02:38 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811 "2023-11-13T02:38:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![xytsrm](https://avatars.discourse-cdn.com/v4/letter/x/6de8d8/32.png) [@xytsrm](https://discourse.nodered.org/u/xytsrm)\
**Post date:** [13 November 2023 02:38 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/1 "2023-11-13T02:38:02Z")

</div>

I've been successful in entering a global function in settings.js under functionGlobalContext: {}. So far I have not been able to create a global variable in settings.js; that node.js is happy with. Where do you put it and how should it be specified?

Also, I've noticed that the change-node in my version (node.js v18.14.2, node-red v3.0.2) doesn't have the memory/file option; was it eliminated, or do I have an old version, and where does the set go?

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [13 November 2023 03:08 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/2 "2023-11-13T03:08:37Z")

</div>

You should be able to just click on the down arrow ?

That's assuming you have setup more than 1 context store in settings.js

If you share what you have tried with the global variable, then perhaps someone can help ?

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

---

<div class="post-metadata">

**Author:** ![xytsrm](https://avatars.discourse-cdn.com/v4/letter/x/6de8d8/32.png) [@xytsrm](https://discourse.nodered.org/u/xytsrm)\
**Post date:** [13 November 2023 04:01 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/3 "2023-11-13T04:01:27Z")

</div>

That's just it, my change-node has no "down arrow":  
 ![Change-Node Setup](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/e/be91b274447b2c2f78abb9936ce6c7dcb148510b.jpeg)

As far as the global variable goes, I've tried:

```auto
functionGlobalContext: {
    myGlobalVariable: "Hello, world!"
}

```

and

```auto
functionGlobalContext: {
   var myGlobalVariable = "Hello, world!";
}

```

For both node.js refuses to run.

---

<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:** [13 November 2023 07:52 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/4 "2023-11-13T07:52:25Z")

</div>

You have to use a persistent context store. Search for  
Node red working with context  
And checkout the link to the node red docs.

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<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:** [13 November 2023 09:36 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/5 "2023-11-13T09:36:04Z")

</div>

If you are putting a global into functionGlobalContext, you don't need a persistent store. Since the settings.js is already persistent. You put stuff in that area that is effectively a CONSTANT (unless you've defined a function of course).

---

<div class="post-metadata">

**Author:** ![xytsrm](https://avatars.discourse-cdn.com/v4/letter/x/6de8d8/32.png) [@xytsrm](https://discourse.nodered.org/u/xytsrm)\
**Post date:** [13 November 2023 13:44 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/6 "2023-11-13T13:44:27Z")

</div>

The question is why node.js would not run with either of the specifications I tried:

> [@xytsrm](#):
>
> ```auto
> functionGlobalContext: {
> myGlobalVariable: "Hello, world!"
> }
> 
> ```

> [@xytsrm](#):
>
> ```auto
> functionGlobalContext: {
> var myGlobalVariable = "Hello, world!";
> }
> 
> ```

Also, It was mentioned that anything placed in functionGlobalContext is a constant; does that mean you can't do a "set"?

Also, why does my change-node not have the memory/file option?

---

<div class="post-metadata">

**Author:** ![xytsrm](https://avatars.discourse-cdn.com/v4/letter/x/6de8d8/32.png) [@xytsrm](https://discourse.nodered.org/u/xytsrm)\
**Post date:** [13 November 2023 13:53 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/7 "2023-11-13T13:53:41Z")

</div>

Hi Colin,

I've watched some of the videos already; they're great. Watching videos on YouTube have become a bit more problematical with Greedy-Google cracking-down on Ad Blockers. Although I've found if you're not logged-in to an account your ok, or set you browser to delete session cookies, or use a non-chrome based browser like Opera. [The war between Ad-Blockers and Google starts; who will win?]

---

<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:** [13 November 2023 14:17 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/8 "2023-11-13T14:17:08Z")

</div>

> [@xytsrm](#):
>
> why does my change-node not have the memory/file option?

As was mentioned earlier, because you have not configured a persistent store.

> [@xytsrm](#):
>
> I've watched some of the videos already; they're great

If you had watched them all, or searched for the docs I suggested then you would have a better chance of answering the questions yourself. Let me do the search for you.  
[https://nodered.org/docs/user-guide/context](https://nodered.org/docs/user-guide/context)

> [@xytsrm](#):
>
> does that mean you can't do a "set"?

If you want a global variable (rather than a constant) then use persistent context.

---

<div class="post-metadata">

**Author:** ![xytsrm](https://avatars.discourse-cdn.com/v4/letter/x/6de8d8/32.png) [@xytsrm](https://discourse.nodered.org/u/xytsrm)\
**Post date:** [13 November 2023 14:21 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/9 "2023-11-13T14:21:59Z")

</div>

How do I set "persistent store"; I know watch the videos, and make the forum irrelevant; [or use chat-GPT 🙂].

---

<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:** [13 November 2023 14:26 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/10 "2023-11-13T14:26:28Z")

</div>

> [@xytsrm](#):
>
> How do I set "persistent store";

Follow the instructions 👇

> [@Colin](#):
>
> [Working with context : Node-RED](https://nodered.org/docs/user-guide/context)

in particular, this part: [Saving context data to the file-system](https://nodered.org/docs/user-guide/context#saving-context-data-to-the-file-system)

---

<div class="post-metadata">

**Author:** ![xytsrm](https://avatars.discourse-cdn.com/v4/letter/x/6de8d8/32.png) [@xytsrm](https://discourse.nodered.org/u/xytsrm)\
**Post date:** [13 November 2023 14:34 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/11 "2023-11-13T14:34:16Z")

</div>

Thanks, but I already got the procedures from Chat-GPT.

---

<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:** [13 November 2023 14:39 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/12 "2023-11-13T14:39:49Z")

</div>

So what is the problem?

---

<div class="post-metadata">

**Author:** ![xytsrm](https://avatars.discourse-cdn.com/v4/letter/x/6de8d8/32.png) [@xytsrm](https://discourse.nodered.org/u/xytsrm)\
**Post date:** [13 November 2023 14:40 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/13 "2023-11-13T14:40:51Z")

</div>

None, now.

---

<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:** [13 November 2023 17:44 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/14 "2023-11-13T17:44:22Z")

</div>

> [@xytsrm](#):
>
> or use a non-chrome based browser like Opera. [The war between Ad-Blockers and Google starts; who will win?]

Just use Brave browser. Still Chromium, just without the privacy impacts. And a decent built-in ad-blocker that is updated to avoid the YT issues.

---

<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:** [13 November 2023 17:47 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/15 "2023-11-13T17:47:30Z")

</div>

> [@xytsrm](#):
>
> `}`

Missing trailing comma maybe?

> [@xytsrm](#):
>
> `;`

Virtually never needed.

> [@xytsrm](#):
>
> Also, It was mentioned that anything placed in functionGlobalContext is a constant; does that mean you can't do a "set"?

You should maybe try it. To be honest, I never have so I don't actually know the answer. My guess would be that it probably works but of course will be reset on restart. But I've never used it that way and always treat those as constants.

---

<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:** [13 November 2023 17:49 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/16 "2023-11-13T17:49:56Z")

</div>

And I took the 30sec to try it so I now know the answer 😀

---

<div class="post-metadata">

**Author:** ![xytsrm](https://avatars.discourse-cdn.com/v4/letter/x/6de8d8/32.png) [@xytsrm](https://discourse.nodered.org/u/xytsrm)\
**Post date:** [13 November 2023 18:01 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/17 "2023-11-13T18:01:20Z")

</div>

Good suggestion Julian; I actually have Brave on my cell; I try it on the PC's. Opera works well too and has a built-in VPN. It seems one of the simplest solutions is to just watch youtube videos without being signed-in to an account.

---

<div class="post-metadata">

**Author:** ![xytsrm](https://avatars.discourse-cdn.com/v4/letter/x/6de8d8/32.png) [@xytsrm](https://discourse.nodered.org/u/xytsrm)\
**Post date:** [13 November 2023 18:07 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/18 "2023-11-13T18:07:57Z")

</div>

Julian, regarding the variable/constants in settings.js; I get the feeling that there isn't a lot of experience with using setting.js. The function I created in settings.js works quite well. I have a few ideas I want to try; I'll let you know as soon as I get back to it.

---

<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:** [13 November 2023 19:36 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/19 "2023-11-13T19:36:00Z")

</div>

It has possibly not been as needed in more recent years with the expansion of other capabilities. But it was originally the one way to get reusable functions available to function nodes as well as the way to get external data into Node-RED easily. It has been there for a long time unchanged - I can't quite remember back the full 10 years now but I've a feeling it may have been there in the early release I first tried.

---

<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:** [12 January 2024 19:36 UTC](https://discourse.nodered.org/t/entering-a-global-variable-specification-into-settings-js/82811/20 "2024-01-12T19:36:22Z")

</div>

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