# Add own functions to node red by using functionGlobalContext

**URL:** https://discourse.nodered.org/t/add-own-functions-to-node-red-by-using-functionglobalcontext/17312
**Category:** General
**Created:** [30 October 2019 08:35 UTC](https://discourse.nodered.org/t/add-own-functions-to-node-red-by-using-functionglobalcontext/17312 "2019-10-30T08:35:03Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Meng](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@Meng](https://discourse.nodered.org/u/Meng)
#### Post date: [30 October 2019 08:35 UTC](https://discourse.nodered.org/t/add-own-functions-to-node-red-by-using-functionglobalcontext/17312/1 "2019-10-30T08:35:03Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)
#### Post date: [30 October 2019 08:41 UTC](https://discourse.nodered.org/t/add-own-functions-to-node-red-by-using-functionglobalcontext/17312/2 "2019-10-30T08:41:23Z")

</div>

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

> [@Importing function from another node](https://discourse.nodered.org/t/importing-function-from-another-node/15885):
>
> Hi everyone, i want to import a function from another node. For example i have two nodes. One node e.g. the 'hello-world-node' prints "hello world" to a txt-file. The other node, 'fake-hello-world-node', should do the same as the 'hello-world-node' but the second node just import the functionality from the first node. Is this possible?

> [@Where to define class to use in multiple nodes](https://discourse.nodered.org/t/where-to-define-class-to-use-in-multiple-nodes/15808):
>
> Hi, I'm using node-red for some time now and just started to create custom nodes. Because of good documentation I was able to get pretty far up till now. I created 3 custom nodes so far. These are passing json objects. To create these objects I use multiple classes. This works fine as long as the class definition is inside the .js file. But I whould like to use these class definitions in multiple nodes. Copying them to the single file seems to be worst idea wink So is there any clever wa…

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [30 October 2019 08:53 UTC](https://discourse.nodered.org/t/add-own-functions-to-node-red-by-using-functionglobalcontext/17312/3 "2019-10-30T08:53:23Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Meng](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@Meng](https://discourse.nodered.org/u/Meng)
#### Post date: [30 October 2019 11:20 UTC](https://discourse.nodered.org/t/add-own-functions-to-node-red-by-using-functionglobalcontext/17312/4 "2019-10-30T11:20:01Z")

</div>

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.

---

<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: [30 October 2019 11:24 UTC](https://discourse.nodered.org/t/add-own-functions-to-node-red-by-using-functionglobalcontext/17312/5 "2019-10-30T11:24:25Z")

</div>

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)
