# functionGlobalContext on Raspberry PI

**URL:** <https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983>\
**Category:** General\
**Created:** [21 October 2019 20:24 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983 "2019-10-21T20:24:40Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![KeMa](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@KeMa](https://discourse.nodered.org/u/KeMa)\
**Post date:** [21 October 2019 20:24 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/1 "2019-10-21T20:24:40Z")

</div>

Hello everybody,

I have a problem with loading an external function on a Raspberry PI.  
To load the module I added it in the "settings.js".

```auto
functionGlobalContext: {
		arfunc:require('to-array')
}

```

In the function-node I include it.

```auto
var arFunc = context.global.get('arfunc');

```

On my Win10 notebook it works fine, but on the rasp i get an error.

```auto
21 Oct 21:28:39 - [error] [function:DataToJSON] TypeError: Cannot read property 'toArray' of undefined

```

I stopped and started node red several times, but it didn't solve it. 😆  
Any ideas, what I'm doing wrong? 🤔

Thanks in advance for any answer!

Cheers KeMa

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [21 October 2019 20:29 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/2 "2019-10-21T20:29:30Z")

</div>

As per [documentation](https://nodered.org/docs/user-guide/runtime/configuration):

```auto
functionGlobalContext: { osModule:require('os') }

```

can be accessed in a function node as:

```auto
var myos = global.get('osModule');

```

---

<div class="post-metadata">

**Author:** ![KeMa](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@KeMa](https://discourse.nodered.org/u/KeMa)\
**Post date:** [21 October 2019 20:40 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/3 "2019-10-21T20:40:17Z")

</div>

Wow,

this was a really quick response, thank you bakman2! 👍

I changed the function node, but unfortunately I get the same error.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [21 October 2019 20:48 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/4 "2019-10-21T20:48:35Z")

</div>

@KeMa what versions of Node.js and Node-RED are you running on the Win10 notebook and on the Pi?

---

<div class="post-metadata">

**Author:** ![KeMa](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@KeMa](https://discourse.nodered.org/u/KeMa)\
**Post date:** [21 October 2019 20:52 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/5 "2019-10-21T20:52:33Z")

</div>

On win10 I'm running

```auto
20 Oct 14:35:08 - [info] Node-RED version: v0.20.7
20 Oct 14:35:08 - [info] Node.js version: v10.16.3

```

on the rasp the latest version

```auto
21 Oct 22:33:12 - [info] Node-RED version: v1.0.2
21 Oct 22:33:12 - [info] Node.js version: v10.16.3

```

---

<div class="post-metadata">

**Author:** ![KeMa](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@KeMa](https://discourse.nodered.org/u/KeMa)\
**Post date:** [21 October 2019 20:58 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/6 "2019-10-21T20:58:03Z")

</div>

After an update on the win10 to

```auto
21 Oct 22:55:39 - [info] Node-RED version: v1.0.2

```

the flow also runs well.

---

<div class="post-metadata">

**Author:** ![KeMa](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@KeMa](https://discourse.nodered.org/u/KeMa)\
**Post date:** [22 October 2019 19:51 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/7 "2019-10-22T19:51:33Z")

</div>

I made a reinstall on rasp with no effect. Could this problem be related to some rights?

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [22 October 2019 20:01 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/8 "2019-10-22T20:01:06Z")

</div>

Which directory did you install the module in?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [22 October 2019 20:01 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/9 "2019-10-22T20:01:56Z")

</div>

Can you share a bit more of your Function code so we can see how you are trying to use the module?

---

<div class="post-metadata">

**Author:** ![KeMa](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@KeMa](https://discourse.nodered.org/u/KeMa)\
**Post date:** [22 October 2019 20:07 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/10 "2019-10-22T20:07:34Z")

</div>

The location of the folder with the Index.js is

```auto
/usr/lib/node_modules/node-red/node_modules/to-array 

```

I'm using it like that

```auto
var datAr = msg.payload;
var arFunc = global.get('arfunc');
var dbg = arFunc.toArray(datAr, 5);

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [22 October 2019 20:13 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/11 "2019-10-22T20:13:16Z")

</div>

From the docs for the `to-array` module, the example usage is:

```auto
var toArray = require("to-array")
    , elems = document.links
 
var array = toArray(elems)

```

Note that the `toArray` function is what the `require(...)` call returns.

Looking at what you've shared, you are adding the module to global context like this:

```auto
arfunc:require('to-array')

```

This means the `arfunc` global context value _is_ the `toArray` function.

Looking at the Function code:

```auto
var arFunc = global.get('arfunc');
var dbg = arFunc.toArray(datAr, 5);

```

You access the global and assign it locally to `arFunc`. You then try to call `.toArray` ... but `arFunc` is _already_ the `toArray` function you want to use.

This code should be:

```auto
var datAr = msg.payload;
var arFunc = global.get('arfunc');
var dbg = arFunc(datAr,5);

```

---

<div class="post-metadata">

**Author:** ![KeMa](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@KeMa](https://discourse.nodered.org/u/KeMa)\
**Post date:** [22 October 2019 21:21 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/12 "2019-10-22T21:21:06Z")

</div>

Ok, understood!  
But now I get the error message

```auto
TypeError: arFunc is not a function

```

I tryed it with the module 'bytes'

```auto
functionGlobalContext: {
                arfunc:require('to-array'),
                bfunc:require('bytes')
}

```

call it

```auto
var bytes = global.get('bfunc');
flow.warn(bytes(65535));

```

and get the error message

```auto
TypeError: bytes is not a function

```

When I send the 'bytes' to a debug-node it is undefined.

I try to understand, how to write and include external functions.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 October 2019 21:51 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/13 "2019-10-22T21:51:37Z")

</div>

> [@KeMa](#):
>
> call it
> 
> ```auto
> var bytes = global.get('bfunc');
> flow.warn(bytes(65535));
> 
> ```
> 
> and get the error message
> 
> ```auto
> TypeError: bytes is not a function
> 
> ```

> [@KeMa](#):
>
> When I send the 'bytes' to a debug-node it is undefined.

This might suggest the module is not in the correct place.

How did you install these 2 modules?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 October 2019 21:55 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/14 "2019-10-22T21:55:10Z")

</div>

I don't know your final intended use cases but as a suggestion...

Have you considered using [https://flows.nodered.org/node/node-red-contrib-function-npm](https://flows.nodered.org/node/node-red-contrib-function-npm)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [22 October 2019 22:17 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/15 "2019-10-22T22:17:32Z")

</div>

This suggests the settings file you are editing is not the settings file Node-RED is using.

Which file are you editing and what settings file does Node-RED log it is using when it starts up?

---

<div class="post-metadata">

**Author:** ![KeMa](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@KeMa](https://discourse.nodered.org/u/KeMa)\
**Post date:** [23 October 2019 07:37 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/16 "2019-10-23T07:37:49Z")

</div>

@knolleary that's it!  
I edited the wrong settings file in "/usr/lib/node\_modules/node-red" all the time! 🤦‍♂️

Thanks a lot to all!👍

---

<div class="post-metadata">

**Author:** ![SteveM](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@SteveM](https://discourse.nodered.org/u/SteveM)\
**Post date:** [29 June 2021 09:26 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/17 "2021-06-29T09:26:18Z")

</div>

How did you decide which was the correct settings file? I am having exactly the same issue...

- I am editing home/pi/.node-red/settings.js

- The JS file is part of a node\_module that I installed. The node\_module is in /home/pi/.node-red/node\_modules/dateformat and the js file is in /home/pi/.node-red/node\_modules/dateformat/lib/dateformat.js

- in my settings.js I have ...

> functionGlobalContext: {  
> dateformat:require("/home/pi/.node-red/node\_modules/dateformat/lib/dateformat.js")  
> },

At first, instead of the path to the JS file, I tried simply dateformat.require("dateformat") but that didn't work either.

- this all works in Visual Code Editor running as a node.js module.

- in node-red function node I have...

> var dateFormat = global.get("dateformat");  
> var theDate = new Date(msg.payload);  
> var formattedDate = dateFormat(theDate, "ddd dd-mmm-yyyy");  
> msg.payload = formattedDate;  
> return msg;

And the response I get is TypeError: dateFormat is not a function

I can't find any useful documentation on how to do this.

Any help or pointers would be most appreciated.

---

<div class="post-metadata">

**Author:** ![KeMa](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@KeMa](https://discourse.nodered.org/u/KeMa)\
**Post date:** [29 June 2021 11:12 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/18 "2021-06-29T11:12:08Z")

</div>

> [@SteveM](#):
>
> I am editing home/pi/.node-red/settings.js

Yes, that's the right one.

> [@SteveM](#):
>
> functionGlobalContext: {  
> dateformat:require("/home/pi/.node-red/node\_modules/dateformat/lib/dateformat.js")  
> },

I did it like this:

```auto
dateformat:require('dateformat');

```

The node module must conform to the conventions!  
In the module folder "dateformat" must be the index.js which includes the sub-modules in the lib folder.  
It is also possible to type the code direct in the index.js.

> **[Node.js Modules](https://www.w3schools.com/nodejs/nodejs_modules.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

---

<div class="post-metadata">

**Author:** ![SteveM](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@SteveM](https://discourse.nodered.org/u/SteveM)\
**Post date:** [29 June 2021 11:32 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/19 "2021-06-29T11:32:35Z")

</div>

That's given me a few ideas as to what is wrong then.

> [@KeMa](#):
>
> In the module folder "dateformat" must be the index.js which includes the sub-modules in the lib folder.  
> It is also possible to type the code direct in the index.js.

Does that mean that if I copy dateformat.js to become index.js in the dateformat folder that it should work?

I also saw articles about **module.exports** - does that come into play somewhere?

---

<div class="post-metadata">

**Author:** ![SteveM](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@SteveM](https://discourse.nodered.org/u/SteveM)\
**Post date:** [29 June 2021 12:12 UTC](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983/20 "2021-06-29T12:12:59Z")

</div>

I'm answering my own last msg 😀 😀

What I learned is this: when I hit Ctrl-C on the node-red console, it chops the console but it doesn't chop the node-red service, so I had been making edits, hitting Ctrl-C in the console and then kicking off the service again. **That did not recycle the service and so did not pick up my changes**. I learned that after the Ctrl-C, I need to run **node-red-stop** followed by **node-red-start** in order to deploy my changes.

So, backing out stuff I didn't need...

- I didn't need to add an exports command to my dateformat.js

- I didn't need to have an index.js file in the dateformat folder - it had already been installed as a node using **npm install** and that was sufficient.

- All I needed was...

> ```
> functionGlobalContext: {
> dateformat:require("dateformat")
> 
> ```
> 
> },

in the settings.js

- and in my Function Node...

> var dateFormat = global.get("dateformat");
> 
> var theDate = new Date(msg.payload);  
> var formattedDate = dateFormat(theDate, "ddd dd-mmm-yyyy");  
> msg.payload = formattedDate;  
> return msg;

then after properly restarting the service it all works.

So, all along, the help given in this thread was spot on, and my problem was that the node-red service was not being recycled like I thought it was.

Thanks for you help guys - I learned a lot.

[Next page](https://discourse.nodered.org/t/functionglobalcontext-on-raspberry-pi/16983.md?page=2)
