Search code examples
javascriptnode.jsnpmnode-red

How to use NPM imports in a Node-RED node?


I would like to use NPM to manage dependencies for a Node-RED node, rather than including the files within the packaged node. How can I define a node, so that it uses NPM to pull in package dependencies?


Solution

  • As you would any other NodeJS NPM module, you include a package.json in your node with a dependencies section. npmjs.org has full documentation for the package.json file format here

    The dependencies sections should look something like this:

    { "dependencies" :
      {
        "foo" : "1.0.0 - 2.9999.9999",
        "bar" : ">=1.0.2 <2.1.2",
        "baz" : ">1.0.2 <=2.3.4",
        "boo" : "2.0.1"
      }
    }
    

    Adding a package.json file also is lets you define the node so Node-RED will find it when it's npm installed, along with allowing you to have multiple nodes in one package and how to add translated text for different languages.

    The Node-RED doc has a section on packaging your node up properly here