Reusing Logic in Node-RED with Subflows: A Practical Guide
Learn how Node-RED subflows let you encapsulate reusable node groups, make them configurable, share them via JSON, and avoid common pitfalls.
03 May 2026, 05:41 UTC

Problem: Repeating the same nodes across flows
When you build multiple Node-RED flows you often find yourself copying a small group of nodes – for example, an inject node that triggers a timestamp, a function node that formats the message, and a debug node that logs the result. Duplicating this logic means any fix or improvement has to be applied in every copy, which is error‑prone and tedious.
Takeaway: Subflows let you treat a group of nodes as a single reusable component
By converting those nodes into a subflow you create a template that can be dropped into any flow. Editing the subflow updates every instance automatically, and you can expose only the parts you need to configure per use‑case.
1. Creating a subflow from existing nodes
- Open a flow tab and place the nodes you want to reuse:
inject → function → debug. - Select all three nodes (click‑drag or Shift‑click).
- From the menu choose Edit → Convert to Subflow. Node-RED creates a new subflow entry in the sidebar and replaces the selected nodes with a single subflow node on the canvas.
- Double‑click the subflow node to open its editor. You will see the original nodes inside a shaded box, ready for editing.
2. Making the subflow configurable per instance
Inside the subflow, the function node might contain hard‑coded logic like:
msg.payload = new Date().toISOString();
return msg;
If you want each instance to use a different timestamp format, expose that format as an input:
- In the subflow editor, add an input node (found under the Subflow section). Rename it to
format. - Replace the function node code with:
// msg.format comes from the subflow input
const fmt = msg.format || 'ISO';
let timestamp;
if (fmt === 'ISO') {
timestamp = new Date().toISOString();
} else if (fmt === 'UNIX') {
timestamp = Date.now();
} else {
timestamp = new Date().toLocaleString();
}
msg.payload = timestamp;
return msg;
format. You can set it to UNIX or ISO for each instance without opening the subflow again.3. Sharing and versioning subflows
Subflows are portable JSON objects. To move a subflow to another Node‑RED instance:
- In the sidebar, right‑click the subflow name and choose Export. Save the
.jsonfile. - In the target instance, use Import → Clipboard or File and select the exported JSON.
- The imported subflow appears ready to use; any instances you add will behave exactly like the original.
Because the subflow is just a JSON file, you can store it in a Git repository, tag releases, and review changes with pull requests – treating it like any other code artifact.
4. Trade‑offs and limitations
- Interface complexity: Every configurable aspect you expose becomes a subflow input or environment variable. Too many inputs make the subflow node hard to use; aim for a small, well‑named set of inputs.
- Context sharing: Subflows do not isolate
floworglobalcontext. All instances share the same context unless you namespace keys (e.g.,flow["subflow1_"]). If you need per‑instance state, consider usingnode.context()inside the subflow or pass state via messages. - Performance: The overhead is minimal – essentially the extra message hop through the subflow’s input and output ports, comparable to adding a function node.
Actionable closing
Start by identifying a repeated node pattern in your current flows. Convert it to a subflow, expose only the parameters that truly need to vary per use‑case, and replace the duplicates with subflow instances. Verify the change by editing the subflow logic and confirming that all instances update automatically. Keep the subflow interface tight, namespace any context you modify, and treat the exported JSON as version‑controlled artifact. This approach reduces duplication, simplifies maintenance, and makes your Node‑RED projects easier to scale.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.