Introduction

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.

Getting Started

There are two ways to use the Node-RED Function Extractor:

  1. Web Interface (what you're using now)
  2. Command Line Tool

Both methods require a Node-RED flow file in JSON format as input.

Usage Examples

Here's an example of what the tool does:

Original Node-RED Function Node in JSON:
{
  "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"]
  ]
}
Extracted JavaScript Function:
/**
 * 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.

Command Line Usage

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

Web Interface

The web interface provides a simple way to upload your Node-RED flow file and get the extracted functions:

Basic Usage

  1. Navigate to the home page
  2. Use the file uploader to select your flow JSON file
  3. Click "Extract Functions"
  4. The tool will process the file and automatically start a download of the extracted functions

Advanced Features

Our web interface now offers several advanced options:

Extraction Modes

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

Additional Options

Include Node Details: Adds detailed comments to each extracted function with:

  • Wiring information showing connections between nodes
  • Names of connected input and output nodes
  • Simple ASCII diagram of node connections
  • Node properties and metadata
Template Extraction

In addition to function nodes, the tool now extracts:

  • Standard template nodes
  • UI template nodes (from Dashboard)
  • Automatically detects and properly formats HTML and CSS content
  • Templates are saved as separate files in structured extraction mode
Results Page

After extraction, you'll be taken to a results page that shows:

  • A summary of the extraction process
  • Statistics about the number of functions, templates, and flows found
  • Processing time and file size information
  • A download link for the extracted files

Output Format

Simple Extraction Format

When using simple extraction, the output is a JavaScript file containing:

  • A header with source information and extraction timestamp
  • All extracted functions with proper JSDoc comments
  • Detailed connection information showing how nodes are wired together
  • Dependencies between functions are maintained

Function names are derived from the Node-RED node names, converted to camelCase if necessary.

Structured Extraction Format

When using structured extraction, the output is a ZIP file containing:

  • A directory structure that mirrors your Node-RED flow organization
  • Individual JavaScript files for each function node
  • HTML and CSS files for each template node
  • Metadata files with information about the original flow
  • A README file explaining the contents

This format is ideal for larger projects or when you want to maintain the organizational structure of your flows.

Advanced Options

When using the command line version, additional options are available:

  • Process multiple flow files at once
  • Specify custom output locations
  • Enable verbose logging

For more details, run node index.js --help

Frequently Asked Questions

The tool supports flow files from Node-RED version 1.0 and above. Earlier versions may work but are not officially supported.

Yes, the tool now extracts both function nodes and template nodes (including UI template nodes from Dashboard). Template content is automatically detected as HTML or CSS and properly formatted in the output.

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:

  • Creating a folder hierarchy based on your flows and subflows
  • Separating functions into individual files
  • Placing template nodes in their own files with proper extensions (.html, .css)
  • Including metadata about the extraction process
  • Packaging everything into a ZIP file for easy download

Structured extraction is ideal for larger Node-RED projects or when you want to preserve the organizational structure of your flows.

References to Node-RED specific contexts (global, flow) are preserved in the code but will not work in a standard Node.js environment. You'll need to adapt these to your target environment.

Yes, the extracted code is formatted for readability while preserving the original logic and comments. Function documentation is added based on the node properties.