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?
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.
It is perfectly valid, yes. Here is the section from my test server.
/** 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,
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.