Function-node access to a global-function in Settings.js

I'm trying to access a global function I created in the Settings.js file.
I placed my function "testGlobalFunction" in Settings.js under "functionGlobalContext:":

    functionGlobalContext: {
        // os:require('os'),
	global.testGlobalFunction = function(){
		let x = {payload:'Returned Value'}
		return x;
	}
    },

Then I restarted Node.js.

In the Function-node I tried several ways to access it;

var testGlobalFunction=global.get('testGlobalFunction')
var x= global.testGlobalFunction()
node.warn(x)
return x;

Or

var x= global.testGlobalFunction()
node.warn(x)
return x;

and using the settings to import module 'testGlobalFunction()'.

In each case I get an error saying that it can't find the function.

What am I doing wrong?

That property only refers to global storage so you don't need to repeat it. It should just be testGlobalFunction

Are you referring to the "global.testGlobalFunction" in the Settings.js, or the reference in the Function-Node?

UPDATE: I have tried every combination; with and without "global." and I still get Function not found.

functionGlobalContext: {
	testGlobalFunction = function(){
		let x = {payload:'Returned Value'}
		return x;
	}
},
const testGlobalFunction = global.get('testGlobalFunction')
const x = testGlobalFunction()
node.warn(x)
return x;

This is the code I have in the settings.js file:

    functionGlobalContext: {
        // os:require('os'),
    	testGlobalFunction = function(){
		let x = {payload:'Returned Value'}
		return x;
	}
    },

I believe I have it exactly as you specified, but the Node.js server fails to start. The cmd window pops-up and then immediately closes.

Even though it was commented out; I removed " // os:require('os'),"; it made no difference; the server won't run.

UPDATE: If I comment-out the function code the Node.js server runs again.

Did you look in your log?

0|Node-RED  | Error loading settings file: D:\src\nr\data\settings.js
0|Node-RED  | D:\src\nr\data\settings.js:899
0|Node-RED  |         testGlobalFunction = function(){
0|Node-RED  | SyntaxError: Invalid shorthand property initializer
0|Node-RED  |     at internalCompileFunction (node:internal/vm:73:18)
0|Node-RED  |     at wrapSafe (node:internal/modules/cjs/loader:1178:20)
0|Node-RED  |     at Module._compile (node:internal/modules/cjs/loader:1220:27)
0|Node-RED  |     at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
0|Node-RED  |     at Module.load (node:internal/modules/cjs/loader:1119:32)
0|Node-RED  |     at Module._load (node:internal/modules/cjs/loader:960:12)
0|Node-RED  |     at Module.require (node:internal/modules/cjs/loader:1143:19)
0|Node-RED  |     at Hook._require.Module.require (C:\Users\julia\AppData\Roaming\npm\node_modules\pm2\node_modules\require-in-the-middle\index.js:101:39)
0|Node-RED  |     at require (node:internal/modules/cjs/helpers:121:18)
0|Node-RED  |     at Object.<anonymous> (D:\src\nr\node_modules\node-red\red.js:140:20)
        testGlobalFunction: function() {
            let x = { payload: 'Returned Value' }
            return x
        },

Corrected version. Note the : instead of the =.

Well that little ":" certainly makes a difference; everything works now.

Regarding the log-file; where is it? There're no logs in the .node-red folder, or in Program Files. The only log files I've found are in npm-cache, buried in appdata, and they are not the node-red logs.

BTW: thanks for catching the error.

Also, will a function placed in settings.js be able to access suncalc, for example:

var gate
var location = { latitude: 40.392, longitude: -74.1843 };
var times = suncalc.getTimes(new Date(), location.latitude, location.longitude);
gate = {
    sunrise: times.sunrise.getTime(),
    sunset: times.sunset.getTime(),
    now: new Date().getTime()}

I import using the function-node setting; how do I import it just using java-script?
I've tried:

const suncalc = require('suncalc');

Says 'require' undefined
and

import suncalc from 'suncalc';

Says: Cannot use import statement outside a module
Neither seem to resolve 'suncalc'
ALL TESTING BEING DONE IN A FUNCTION-NODE: NOT IN SETTING.JS.

Question RESOLVED:

const suncalc = require('suncalc');

This code does not work in a function-node, BUT, it does work in the settings.js file.
I'm able to access the suncalc data from any function-node.

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