How to Load External Modules

Home > Guides

This guide provides instructions on how to make Node.js modules available to Node-RED for ubuntu logo Ubuntu and BeagleBone.


ubuntu logo Ubuntu Steps

1. Make sure Node.js, npm and Nod-RED are installed on your system.

2. Download and install the npm packages (modules) globally on your system you wish to use in Node-RED:

npm install <package_name> -g

2 Change into the directory that contains the Node-RED settings file:

cd /home/ubuntu/.node-red

3. Open the settings.js file with nano (or your favorite editor):

nano settings.js

4. Edit the settings.js file.

Modify the functionGlobalContext function in the settings file to reference modules of interest.
Below demonstrates the syntax for making the os, bonescript and moment node.js modules available to Node-RED.
(Tip: Make sure to use commas correctly).

functionGlobalContext: {
os:require('os'),
bonescript:require('bonescript'),
moment:require('moment')
// jfive:require("johnny-five"),
// j5board:require("johnny-five").Board({repl:false})
},

5. Save and exit the settings.js file.

6. Stop and start Node-RED for the changes to take effect. We typically use PM2 process manager to run Node-RED on Ubuntu. So starting and stopping is accomplished with PM2 commands.

7. List the processes running under PM2.
sudo pm2 listPM2 List

8. Stop the Node-RED application.
sudo pm2 stop node-red
Stop PM2

9. Start the Node-RED application.
sudo pm2 start node-red
Start PM2


BeagleBone (Debian 10) Steps

1. Connect to the BeagleBone

2. Download and install the npm packages (modules) globally on your system you wish to use in Node-RED:

npm install <package_name> -g

3. Change into the directory that contains the Node-RED settings file:

cd /home/ubuntu/.node-red

4. Open the settings.js file with nano (or your favorite editor):

nano settings.js

5. Edit the settings.js file.

Modify the functionGlobalContext function in the settings file to reference modules of interest.
Below demonstrates the syntax for making the os, bonescript and moment node.js modules available to Node-RED.
(Tip: Make sure to use commas correctly).

functionGlobalContext: {
os:require('os'),
bonescript:require('bonescript'),
moment:require('moment')
// jfive:require("johnny-five"),
// j5board:require("johnny-five").Board({repl:false})
},

6. Save and exit the settings.js file.

7. Stop and start Node-RED for the changes to take effect.

8. Stop Node-RED

sudo systemctl stop nodered

9. Start Node-RED

sudo systemctl start nodered


References