# Creating Global Varibles using Setting.js

**URL:** <https://discourse.nodered.org/t/creating-global-varibles-using-setting-js/82431>\
**Category:** General\
**Created:** [28 October 2023 20:52 UTC](https://discourse.nodered.org/t/creating-global-varibles-using-setting-js/82431 "2023-10-28T20:52:13Z")\
**Posts on this page:** 12\
**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:** [28 October 2023 20:52 UTC](https://discourse.nodered.org/t/creating-global-varibles-using-setting-js/82431/1 "2023-10-28T20:52:13Z")

</div>

I'm interested in creating about 20 global variables that would be available to all Function-Nodes. I had found this suggestion:  
I placed this code-snippet in the Setting.js file:

```auto
module.exports = {
    functionGlobalContext: {
        var01: 'value01',
        var02: 'value02',
        var03: 'value03',
        var04: 'value04',
        var05: 'value05',
        var06: 'value06',
        var07: 'value07',
        var08: 'value08',
        var09: 'value09',
        var10: 'value10',
        var11: 'value11',
        var12: 'value12',
        var13: 'value13',
        var14: 'value14',
        var15: 'value15',
        var16: 'value16',
        var17: 'value17',
        var18: 'value18',
        var19: 'value19',
        var20: 'value20'
    }
}

Unfortunately, the server refused to run until I removed the code-snippet.

Is it even possible to create global variables this way, or is there just something wrong with the structure?
```

---

<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:** [28 October 2023 20:54 UTC](https://discourse.nodered.org/t/creating-global-varibles-using-setting-js/82431/2 "2023-10-28T20:54:11Z")

</div>

You are missing 1 or 2 comma's.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/3/537160492740a6f426f8210995c0e11fd489395e.png)

```auto
    // stuff here 
    functionGlobalContext: {
        // ....
    },
    // There is more stuff here
}

```

It never hurts to have trailing comma's in JavaScript, it is a useful habit to get into and saves a ton of pain when you move things around as well.

---

<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:** [28 October 2023 20:58 UTC](https://discourse.nodered.org/t/creating-global-varibles-using-setting-js/82431/3 "2023-10-28T20:58:16Z")

</div>

'valure20' is the last value of the structure before the brace, it doesn't need a comma. I don't see the second missing comma you maybe referring to?

UPDATE: I see the second comma you're referring to.

---

<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:** [28 October 2023 21:00 UTC](https://discourse.nodered.org/t/creating-global-varibles-using-setting-js/82431/4 "2023-10-28T21:00:04Z")

</div>

It doesn't need one, but one doesn't hurt.

But it is the next line that DOES need on assuming that your settings.js file is fairly standard. Because there is more after the global context part and you've cut it off by not having a comma.

I strongly recommend using a decent programmers editor such as VScode by the way, it will highlight these errors.

---

<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:** [28 October 2023 21:00 UTC](https://discourse.nodered.org/t/creating-global-varibles-using-setting-js/82431/5 "2023-10-28T21:00:26Z")

</div>

Does this approach to creating global variable look valid to you?

---

<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:** [28 October 2023 21:01 UTC](https://discourse.nodered.org/t/creating-global-varibles-using-setting-js/82431/6 "2023-10-28T21:01:29Z")

</div>

It is perfectly valid, yes. Here is the section from my test server.

```auto
    /** Allow external modules to be specified in function nodes
     * See https://nodered.org/docs/user-guide/writing-functions#using-the-functionexternalmodules-option
     */
    functionExternalModules: true, // default: true

    /** The following property can be used to set predefined values in Global Context.
     * This allows extra node modules to be made available with in Function node.
     * For example, the following:
     * functionGlobalContext: { os:require('os') }
     * will allow the `os` module to be accessed in a Function node using:
     * global.get("os")
     */
    functionGlobalContext: {
        _pid: process.pid, // Node-RED process ID, allows a flow to stop Node-RED
        _env: process.env, // Pass ALL environment vars to Node-RED - dangerous
        _userid: process.env.user || process.env.username || process.env.USER,
        _userHome: process.env.home || process.env.userhome || process.env.HOME,
        _hostName: require('os').hostname(),
        _userDir: __dirname,
        require: require, // DANGER Will Robinson!
        _port: this.uiPort,
        // _: require('lodash'),
        // wiser: require('node-drayton-wiser')(),
    },

    /** The maximum number of messages nodes will buffer internally as part of their
     * operation. This applies across a range of nodes that operate on message sequences.
     * Defaults to no limit. A value of 0 also means no limit is applied.
     */
    // nodeMessageBufferMaxLength: 0,

```

---

<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:** [28 October 2023 21:04 UTC](https://discourse.nodered.org/t/creating-global-varibles-using-setting-js/82431/7 "2023-10-28T21:04:21Z")

</div>

Did you insert your global variables in module.exports?

> [@xytsrm](#):
>
> `module.exports = {}`

Yours look like they are stand-alone, outside module.exports (although I tried that too)

---

<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:** [28 October 2023 21:15 UTC](https://discourse.nodered.org/t/creating-global-varibles-using-setting-js/82431/8 "2023-10-28T21:15:36Z")

</div>

I placed the code snippet in module.exports{} as before, but I added the comma after the brace as you suggested; the server ran.

Thanks for catching that one.

BTW: Do I just reference them in a Function-node, or import them, or what?

---

<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:** [28 October 2023 21:41 UTC](https://discourse.nodered.org/t/creating-global-varibles-using-setting-js/82431/9 "2023-10-28T21:41:06Z")

</div>

In the Function-Node I added:  
var flowState01 = global.get('flowState01');  
node.error('flowstate01: '+flowState01)

But flowState01 prints to Debug as undefined.

---

<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:** [28 October 2023 23:57 UTC](https://discourse.nodered.org/t/creating-global-varibles-using-setting-js/82431/10 "2023-10-28T23:57:56Z")

</div>

> [@xytsrm](#):
>
> Yours look like they are stand-alone, outside module.exports (although I tried that too)

It isn't. That's just a small section.

* * *

Though I do actually do things slightly differently than the default settings file.

```auto
const nrsettings = {
  // all the settings
}

module.exports = nrsettings

```

That's because it gives you the ability to create settings that are based on other settings, as in this example.

```auto
const nrsettings = {
  // all the settings
}

/** Splitting the export this way allows us to dynamically override settings if we want to */
nrsettings.functionGlobalContext._port = nrsettings.uiPort

module.exports = nrsettings

```

But that isn't really relevant.

> [@xytsrm](#):
>
> var flowState01 = global.get('flowState01');

Well if you put a property called `flowState01` into the globals in settings then that code will recover its value.

---

<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:** [29 October 2023 00:14 UTC](https://discourse.nodered.org/t/creating-global-varibles-using-setting-js/82431/11 "2023-10-29T00:14:18Z")

</div>

Hi, I changed the structure; I still haven't been able to get it to work the variable after the global.get() is undefined.  
New Structure:

> functionGlobalContext: {  
> flowState01: true,  
> flowState02: true,  
> flowState03: true,  
> flowState04: true,  
> flowState05: true,  
> flowState06: true,  
> flowState07: true,  
> flowState08: true,  
> flowState09: true,  
> flowState10: true,  
> flowState11: true,  
> flowState12: true,  
> flowState13: true,  
> flowState14: true,  
> flowState15: true,  
> flowState16: true,  
> flowState17: true,  
> flowState18: true,  
> flowState19: true,  
> flowState20: true  
> }

I've been doing it more conventionally in Function Nodes.

---

<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:** [28 December 2023 00:15 UTC](https://discourse.nodered.org/t/creating-global-varibles-using-setting-js/82431/12 "2023-12-28T00:15:11Z")

</div>

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