# Use installed node.js modules from node-red functions

**URL:** <https://discourse.nodered.org/t/use-installed-node-js-modules-from-node-red-functions/42490>\
**Category:** General\
**Created:** [11 March 2021 14:54 UTC](https://discourse.nodered.org/t/use-installed-node-js-modules-from-node-red-functions/42490 "2021-03-11T14:54:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Don](https://avatars.discourse-cdn.com/v4/letter/d/ed655f/32.png) [@Don](https://discourse.nodered.org/u/Don)\
**Post date:** [11 March 2021 14:54 UTC](https://discourse.nodered.org/t/use-installed-node-js-modules-from-node-red-functions/42490/1 "2021-03-11T14:54:24Z")

</div>

Hi,

New to node-red, new to node.js - please forgive (and correct) newbie errors.

My node-red is installed on Linux, following directions from various node-red web pages. All working well. I read directions for installing a node.js module, that it has to be a non-global install in the directory ~/.node-red. Did that installation (for "flipacoin"), inspected the results, looks good as far as I can tell.

Tweaked the node-red "settings.js" file to include the line "flip:require('flip')", emulating other commented lines in the otherwise empty "functionGlobalContext" stanza. When attempting to start node-red, it fails to parse the settings file.

Can someone point me at information to allow me to get access to installed node.js functions, so that I can use them in node-red functions? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 March 2021 15:10 UTC](https://discourse.nodered.org/t/use-installed-node-js-modules-from-node-red-functions/42490/2 "2021-03-11T15:10:15Z")

</div>

Welcome to the forum @Don  
Possibly you have a small error in your settings, possible a comma misplaced for example. If you can't see it then copy/paste a few lines around the change you made so we can have a look. For how to post code and flows so they appear correctly here see this post - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![Don](https://avatars.discourse-cdn.com/v4/letter/d/ed655f/32.png) [@Don](https://discourse.nodered.org/u/Don)\
**Post date:** [11 March 2021 15:25 UTC](https://discourse.nodered.org/t/use-installed-node-js-modules-from-node-red-functions/42490/3 "2021-03-11T15:25:48Z")

</div>

Hi Colin, thanks for the quick reply.

So here's the error:

```auto
$ node-red
Error loading settings file: /home/ddoerner/.node-red/settings.js

```

Here's a snippet from the settings.js file:

```auto
    // The following property can be used to seed Global Context with predefined
    // values. This allows extra node modules to be made available with the
    // Function node.
    // For example,
    // functionGlobalContext: { os:require('os') }
    // can be accessed in a function block as:
    // global.get("os")
    functionGlobalContext: {
        flip:require('flip')
        // os:require('os'),
        // jfive:require("johnny-five"),
        // j5board:require("johnny-five").Board({repl:false})
    },

```

And here's a peek at the contents of my ~/.node-red directory:

```auto
$ tree .
.
├── flows_ddoerner-ubuntu.json
├── flows_ddoerner-ubuntu_cred.json
├── lib
│ └── flows
├── node_modules
│ └── flipacoin
│ ├── README.md
│ ├── bin
│ │ └── flip.js
│ ├── index.js
│ └── package.json
├── package-lock.json
├── package.json
└── settings.js

```

Thanks for your asistance.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 March 2021 15:31 UTC](https://discourse.nodered.org/t/use-installed-node-js-modules-from-node-red-functions/42490/4 "2021-03-11T15:31:55Z")

</div>

Since you are requiring flip not flipacoin then I think you should have installed flip, or required flipacoin.

However, I think you could achieve the same thing using the random node configured to generate integer 0 to 1. [node-red-node-random (node) - Node-RED](https://flows.nodered.org/node/node-red-node-random)

---

<div class="post-metadata">

**Author:** ![Don](https://avatars.discourse-cdn.com/v4/letter/d/ed655f/32.png) [@Don](https://discourse.nodered.org/u/Don)\
**Post date:** [11 March 2021 15:37 UTC](https://discourse.nodered.org/t/use-installed-node-js-modules-from-node-red-functions/42490/5 "2021-03-11T15:37:03Z")

</div>

Colin, thanks for your help: when I changed the require statement, it worked. Unfortunately, I need the random true/false as part of additional logic, so I elected not to install & use that node.

While I have your attention, I am not so clear on how to get access to the 'flip' function from inside a node-red function node. Can you point me to an example?

TY!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 March 2021 15:53 UTC](https://discourse.nodered.org/t/use-installed-node-js-modules-from-node-red-functions/42490/6 "2021-03-11T15:53:45Z")

</div>

> [@Don](#):
>
> I need the random true/false as part of additional logic,

Do you mean you need it inside a function node? In which case use [Math.random()](https://www.w3schools.com/js/js_random.asp).

---

<div class="post-metadata">

**Author:** ![Don](https://avatars.discourse-cdn.com/v4/letter/d/ed655f/32.png) [@Don](https://discourse.nodered.org/u/Don)\
**Post date:** [11 March 2021 16:18 UTC](https://discourse.nodered.org/t/use-installed-node-js-modules-from-node-red-functions/42490/7 "2021-03-11T16:18:33Z")

</div>

Yes, I need it in a function node. I can do it with Math.random, but was trying to learn how to incorporate an npm module in general. Next time the problem won't be as simple as a random Boolean! But this solves the problem for now, thanks!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 March 2021 17:11 UTC](https://discourse.nodered.org/t/use-installed-node-js-modules-from-node-red-functions/42490/8 "2021-03-11T17:11:40Z")

</div>

If you want to us flipacoin then in the function you should be able to say  
`let flipacoin = global.get("flipacoin")`  
and then use flipacoin. I don't know that module so can't say exactly how.

---

<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:** [25 March 2021 17:11 UTC](https://discourse.nodered.org/t/use-installed-node-js-modules-from-node-red-functions/42490/9 "2021-03-25T17:11:59Z")

</div>

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