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
variable
that 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
variable
you defined in the "Setup" tab.
# Example: Moment.js
Recently we wanted to use moment for some custom date calculations. Whilst there was set of Moment Node-RED nodes already available, it didn't have all of the functionality we needed.
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.
# Simplify Function Node Creation with FlowFuse
FlowFuse provides a powerful platform to enhance, scale, and secure your Node-RED applications efficiently. One of our latest features, the FlowFuse Assistant, is designed to streamline the process of creating Function nodes.
With the FlowFuse Assistant, you can leverage AI to generate Function nodes effortlessly. Just input your prompt, and the Assistant will handle the creation for you, saving time and reducing manual coding.
To explore how to make the most of the FlowFuse Assistant and its capabilities, check out the Assistants Documentation.
Written By:
Published on:
Related Articles:
- FlowFuse Cloud now available for free!
- Data Modeling: The Key to a Successful Unified Namespace
- Streamlining Node-RED Collaboration with FlowFuse
- Publishing Modbus data to your UNS with FlowFuse
- Building a Unified Namespace (UNS) with FlowFuse