Use installed node.js modules from node-red functions


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.

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

Hi Colin, thanks for the quick reply.

So here's the error:

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

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

    // 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: {
        // 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:

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

Thanks for your asistance.

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

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?


Do you mean you need it inside a function node? In which case use Math.random().

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!

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.

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