Add own functions to node red by using functionGlobalContext

Including the os library works fine by including in the settings.js under functionGlobalContext::

os:require('os')

And including in a function node:

var os = global.get('os');
msg.payload=os.arch();

Now I want to include my own functions into NodeRed, resulting in a cannot read property 'function' of undefined. I just included a simple function in the file called test.js:

function myFunction(p1, p2) {
return p1 * p2; // The function returns the product of p1 and p2
}

For the functionGlobalContext i used;

test:require('./test.js')

So how can i use my own function using functionGlobalContext and assure that the function module is found?

If you search the forum, you will find that there are some solutions to your problem available already.

At the more basic level your test.js file needs to export its capability
https://www.tutorialsteacher.com/nodejs/nodejs-module-exports

1 Like

Is there also a way to deal with global variables inside the external functions, as the global.set and get functions do not work. Or should this be done outside the functions.

If you are providing your own functions defined in external files and loaded via global context then they will not have any direct access to the apis that are available in the Function node.

One option would be for your to write your functions so you pass in reference to the bits of the api you need ( ie the global, flow objects)

1 Like