The Node-RED Function Extractor is a tool from SayHi.io designed to extract function nodes from Node-RED flows and convert them to reusable JavaScript functions.
When developing in Node-RED, you often create complex logic within function nodes. This tool allows you to export those functions for use in regular Node.js applications, enabling code reuse and easier maintenance.
There are two ways to use the Node-RED Function Extractor:
Both methods require a Node-RED flow file in JSON format as input.
Here's an example of what the tool does:
{
"id": "a1b2c3d4e5f6",
"type": "function",
"z": "main_flow_tab",
"name": "Calculate Temperature Average",
"func": "const readings = msg.payload;\nif (!Array.isArray(readings)) {\n node.error(\"Expected array of temperature readings\");\n return null;\n}\n\nconst sum = readings.reduce((total, val) => total + val, 0);\nconst avg = sum / readings.length;\n\nmsg.payload = {\n average: avg,\n count: readings.length\n};\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 380,
"y": 240,
"wires": [
["temperatureDisplay", "temperatureLogger"]
]
}
/**
* Calculate Temperature Average
*
* Node ID: a1b2c3d4e5f6
* Flow: main_flow_tab
*
* Connections:
* ├─ Input: Sensor Reading Node
* └─ Output:
* ├─ temperatureDisplay
* └─ temperatureLogger
*
* @param {Object} msg - The message object containing the payload
* @param {Array} msg.payload - Array of temperature readings
* @returns {Object} msg - The message with calculated average
*/
function calculateTemperatureAverage(msg) {
const readings = msg.payload;
if (!Array.isArray(readings)) {
throw new Error("Expected array of temperature readings");
}
const sum = readings.reduce((total, val) => total + val, 0);
const avg = sum / readings.length;
msg.payload = {
average: avg,
count: readings.length
};
return msg;
}
As shown above, the tool converts function nodes from your Node-RED flow JSON into clean, reusable JavaScript functions with detailed comments documenting node connections and metadata.
The tool can be used from the command line with the following options:
node index.js [options]
Options:
-r, --read <file> Specify input flow JSON file(s)
-t, --target <file> Specify output JavaScript file
-v, --verbose Enable verbose output
-h, --help Show help
Example command:
node index.js -r flows.json -t extracted_functions.js
The web interface provides a simple way to upload your Node-RED flow file and get the extracted functions:
Our web interface now offers several advanced options:
Simple Extraction: Extracts all functions into a single JavaScript file (default behavior)
Structured Extraction: Organizes functions and templates into a folder hierarchy based on flows and subflows, then packages everything into a ZIP file for download
Include Node Details: Adds detailed comments to each extracted function with:
In addition to function nodes, the tool now extracts:
After extraction, you'll be taken to a results page that shows:
When using simple extraction, the output is a JavaScript file containing:
Function names are derived from the Node-RED node names, converted to camelCase if necessary.
When using structured extraction, the output is a ZIP file containing:
This format is ideal for larger projects or when you want to maintain the organizational structure of your flows.
When using the command line version, additional options are available:
For more details, run node index.js --help
Simple extraction outputs all functions into a single JavaScript file, making it easy to import into another project or review all code at once.
Structured extraction creates a more organized output by:
Structured extraction is ideal for larger Node-RED projects or when you want to preserve the organizational structure of your flows.