Creating Global Varibles using Setting.js

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:

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?

You are missing 1 or 2 comma's.


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

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

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.

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

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

    /** Allow external modules to be specified in function nodes
     * See
    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:, // 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,

Did you insert your global variables in module.exports?

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

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?

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

But flowState01 prints to Debug as undefined.

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

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

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.

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.

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

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.

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