Entering a global variable specification into settings.js

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?

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 ?


That's just it, my change-node has no "down arrow":
Change-Node Setup

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

functionGlobalContext: {
    myGlobalVariable: "Hello, world!"


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

For both node.js refuses to run.

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. 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.

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).

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

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?

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?]

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

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.

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

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

Follow the instructions :point_down:

in particular, this part: Saving context data to the file-system

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

So what is the problem?

None, now.

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.

Missing trailing comma maybe?

Virtually never needed.

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.

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

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.

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.

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.

1 Like

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