Use any npm module in Node-RED
See how you can easily import any npm module, for use in a Node-RED function node.
Node-RED has an incredibly rich resource of integrations available, but sometimes you need that little bit of extra functionality, or access to a Node.js module that doesn't have it's own custom nodes in Node-RED. We can easily import any npm module within the built-in Node-RED function nodes.
Historically in Node-RED, you would have needed to manually
npm install modules from the command line, but now that it's so easy to run Node-RED in the Cloud, where you don't have easy access to those tools, what are the other options available?
# Function Node - Setup
All you need is the name of the module you want to import, then:
- Drop in a new "function" node & double-click it
- Switch to the "Setup" tab
- Underneath the "modules" tab, click "+ add" in the bottom-left of the window.
- Enter the name of the module you want to use in the newly created row, and (optionally) modify the
variablethat this module will be imported in as.
- Switch back to the "On Message" tab and write your function. Your new module will be available via the
variableyou defined in the "Setup" tab.
# Example: Moment.js
So, all we needed to do was import the module into a function node, and define our comparison there instead, here's a working example:
# Example: Easy CRC
Something we see a lot on the Node-RED Forums are questions on how to conduct CRC calculations. There is a popular node module
easy-crc that can be imported and used in the function nodes, e.g:
# Example: PostHog
Node-RED is great for data integration. We use PostHog for our internal Product Analysis. We record live events as they occur on FlowFuse Cloud to better understand features that are (and are not) used.
We wanted to investigate whether or not we could add backdated data, which in theory was possible via their posthog-node module. We wanted to populate it with data driven from our own database and API.
Within two minutes, we could wire up a node to retrieve data from our API, and then ingest it into
posthog-node via the import of a function node.
- Updating our branding across GitHub, npm and Dockerhub
- How ChatGPT improves Node-RED Developer Experience
- Share & Preview Flows on flows.nodered.org
- Charting REST API Data in a Dashboard
- Modernize your legacy industrial data. Part 2.