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

**URL:** https://discourse.nodered.org/t/function-node-access-to-a-global-function-in-settings-js/82791
**Category:** General
**Created:** [11 November 2023 21:16 UTC](https://discourse.nodered.org/t/function-node-access-to-a-global-function-in-settings-js/82791 "2023-11-11T21:16:03Z")
**Posts on this page:** 9
**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: [11 November 2023 21:16 UTC](https://discourse.nodered.org/t/function-node-access-to-a-global-function-in-settings-js/82791/1 "2023-11-11T21:16:03Z")

</div>

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:":

```auto
    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;

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

```

Or

```auto
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?

---

<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: [11 November 2023 23:52 UTC](https://discourse.nodered.org/t/function-node-access-to-a-global-function-in-settings-js/82791/2 "2023-11-11T23:52:36Z")

</div>

> [@xytsrm](#):
>
> `global.testGlobalFunction`

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

---

<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: [12 November 2023 00:08 UTC](https://discourse.nodered.org/t/function-node-access-to-a-global-function-in-settings-js/82791/3 "2023-11-12T00:08:43Z")

</div>

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.

---

<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: [12 November 2023 02:25 UTC](https://discourse.nodered.org/t/function-node-access-to-a-global-function-in-settings-js/82791/4 "2023-11-12T02:25:07Z")

</div>

```auto
functionGlobalContext: {
	testGlobalFunction = function(){
		let x = {payload:'Returned Value'}
		return x;
	}
},

```

```auto
const testGlobalFunction = global.get('testGlobalFunction')
const x = testGlobalFunction()
node.warn(x)
return x;

```

---

<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: [12 November 2023 02:41 UTC](https://discourse.nodered.org/t/function-node-access-to-a-global-function-in-settings-js/82791/5 "2023-11-12T02:41:52Z")

</div>

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

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

---

<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: [12 November 2023 12:41 UTC](https://discourse.nodered.org/t/function-node-access-to-a-global-function-in-settings-js/82791/6 "2023-11-12T12:41:31Z")

</div>

Did you look in your log?

```auto
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)

```

```auto
        testGlobalFunction: function() {
            let x = { payload: 'Returned Value' }
            return x
        },

```

Corrected version. Note the `:` instead of the `=`.

---

<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: [12 November 2023 13:41 UTC](https://discourse.nodered.org/t/function-node-access-to-a-global-function-in-settings-js/82791/7 "2023-11-12T13:41:50Z")

</div>

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:

```auto
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:

```auto
const suncalc = require('suncalc');

```

Says 'require' undefined  
and

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

---

<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: [12 November 2023 16:01 UTC](https://discourse.nodered.org/t/function-node-access-to-a-global-function-in-settings-js/82791/8 "2023-11-12T16:01:53Z")

</div>

Question RESOLVED:

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

---

<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: [26 November 2023 16:02 UTC](https://discourse.nodered.org/t/function-node-access-to-a-global-function-in-settings-js/82791/9 "2023-11-26T16:02:18Z")

</div>

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