Ova

How to import module in node red?

Published in Node-RED Modules 5 mins read

To import modules in Node-RED, especially for use within function nodes, you typically need to install the desired npm package globally and then configure Node-RED's settings.js file to make it available in the global context. This method allows you to leverage a vast ecosystem of JavaScript libraries directly within your flows.

How to Import External NPM Modules in Node-RED Function Nodes

This process is used when you want to utilize a standard JavaScript library (an npm package) that isn't a pre-built Node-RED node, but rather a utility you'd call from a function node, such as moment.js for date manipulation or axios for HTTP requests.

Prerequisites

Before you begin, ensure you have:

  • Node.js and npm installed on your system.
  • Node-RED installed and running.

Step-by-Step Guide to Importing External NPM Modules

Follow these steps to make an npm module available for use in your Node-RED function nodes:

1. Install the NPM Package Globally

The first step is to install the desired npm package. Installing it globally ensures it's accessible system-wide and simplifies its inclusion in Node-RED.

  • Open your terminal or command prompt.

  • Execute the npm install -g command followed by the package name.

    npm install -g <package-name>

    Example: To install the moment library for date/time handling:

    npm install -g moment

2. Locate and Edit Node-RED's settings.js File

The settings.js file is Node-RED's primary configuration file. You'll need to modify it to declare the external module.

  • Locate the settings.js file.

    • Typically, this file is found in your Node-RED user directory, which is usually ~/.node-red/settings.js on Linux/macOS or C:\Users\<username>\.node-red\settings.js on Windows.
  • Open the file using a text editor. You can use nano in a terminal:

    nano ~/.node-red/settings.js

3. Configure functionGlobalContext

Inside settings.js, you'll find a section named functionGlobalContext. This object allows you to expose modules and variables to all function nodes.

  • Find the module.exports section and locate the functionGlobalContext object within it.

  • Uncomment the functionGlobalContext section if it's commented out (remove the // at the beginning of the lines).

  • Add an entry for your module using a key-value pair. The key will be the name you use to access the module in your function nodes (e.g., momentjs), and the value will be the actual npm package name (e.g., "moment").

    // The following properties can be used to seed your global context
    // In your node-red functions you can access them like:
    //   var myModule = context.global.get('myModule');
    //   var fs = context.global.get('fs');
    //
    functionGlobalContext: {
        // os:require('os'),
        // octalbonescript:require('octalbonescript'),
        // jfive:require("johnny-five"),
        // jf:require("johnny-five"),
        // Here, add your module:
        moment: require('moment'), // Example: making the 'moment' library available as 'moment'
        // customUtil: require('/home/pi/.node-red/custom-lib/util'),
    },

    Important: Notice require('moment') instead of "moment". While the reference might imply a string, for Node.js modules, require() is the correct way to load them into the context. If you simply use "moment", Node-RED will try to resolve it as a string literal instead of loading the module.

4. Save Changes and Restart Node-RED

After modifying settings.js, you must save the file and restart your Node-RED instance for the changes to take effect.

  • Save the settings.js file.
    • If using nano, press Ctrl+X, then Y to confirm save, then Enter.
  • Stop Node-RED. The command depends on how you run Node-RED (e.g., node-red-stop if running as a service, or Ctrl+C in the terminal if started manually).
  • Start Node-RED again. (e.g., node-red-start or node-red).

Using the Imported Module in a Function Node

Once the module is configured and Node-RED is restarted, you can access it within any function node in your flows.

  • Drag a Function node onto your flow.

  • Double-click it to open its editor.

  • Access the module using global.get().

    const moment = global.get('moment'); // 'moment' matches the key you set in settings.js
    
    if (moment) {
        const now = moment().format('YYYY-MM-DD HH:mm:ss');
        node.warn(`Current time: ${now}`);
        msg.payload = now;
    } else {
        node.error("Moment.js module not found in global context!");
        msg.payload = "Error: Module not loaded.";
    }
    
    return msg;

    Now, your function node can utilize the functionalities provided by the moment library.


Adding Pre-built Node-RED Nodes (via Palette Manager)

It's important to distinguish between importing general npm modules for function nodes and installing pre-built Node-RED nodes. If you want to add new nodes to your Node-RED palette (e.g., nodes for specific hardware, protocols, or services), you use the Palette Manager. This method does not involve editing settings.js or functionGlobalContext.

Steps to Install Nodes via Palette Manager

  1. Open your Node-RED editor in a web browser.
  2. Click the menu icon (three horizontal lines) in the top-right corner.
  3. Select "Manage palette."
  4. Go to the "Install" tab.
  5. Use the search bar to find the desired Node-RED package (e.g., node-red-contrib-mqtt, node-red-node-serialport).
  6. Click the "install" button next to the package you wish to add.
  7. Node-RED will automatically download and install the nodes. A restart of Node-RED is not usually required for these changes to take effect, as the editor reloads the palette dynamically.

This method is simpler for extending Node-RED with new visual components and functionality, while the settings.js method is for adding raw JavaScript library capabilities to your custom code in function nodes.