Skip to content

Node Red Function Node

DodaTech 3 min read

In this tutorial, you'll learn about Node. We cover key concepts, practical examples, and best practices.

Hook

Your Function node shows a red error triangle. The deploy succeeded, but when a message arrives, the node crashes with "Function node: Error: ..." in the debug sidebar. Your carefully written JavaScript code is failing at runtime.

The Wrong Way

Wrapping the entire function in a try/catch with no error handling hides the problem without fixing it.

// BAD: Empty catch
try {
    var result = msg.payload.someMethod();
    return msg;
} catch (e) {
    // Ignore errors
    return null;
}
Function node: no output (silently fails)
Debug sidebar: empty (error hidden)
Flow continues with missing data — downstream nodes fail

The function swallows errors, making debugging impossible.

The Right Way

Use the Debug sidebar and node status to identify the exact error line.

// CORRECT: Proper error handling with logging
var result;
try {
    result = msg.payload.someMethod();
    msg.payload = result;
    return msg;
} catch (e) {
    node.error("Failed to process: " + e.message, msg);
    return null;
}
# 1. Check the Function node's execution log
# In Node-RED editor, double-click the Function node
# The editor shows a syntax check result
# 2. Enable function node logging
# In settings.js:
functionGlobalContext: {
    // ...
},
logging: {
    console: {
        level: "debug",
        metrics: false
    }
}
# 3. Check for common JS errors:
# - msg.payload is undefined when you call .method() on it
# - Variable not declared with var/let/const
# - Returning undefined when node expects an array
// Test with a simple passthrough first
msg.payload = msg.payload;
return msg;
if (msg.payload === undefined) {
    node.warn("payload is undefined");
    return msg;
}
# 4. Restart Node-RED after code changes
node-red-restart
# 5. Test with an Inject node sending a known payload
[Inject node: {"payload": {"value": 42}}]  [Function node]  [Debug node]
Debug: {"payload": 42}
Function node: processed successfully ✓

Prevention

  • Test Function nodes with simple inputs before adding complexity.
  • Use node.warn() and node.error() for runtime logging.
  • Check for undefined before accessing nested properties: msg?.payload?.value.
  • Keep functions short — split complex logic across multiple Function nodes.
  • Use the Template node for string manipulation instead of Function nodes when possible.

Common Mistakes with red function node

  1. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  2. Misunderstanding that String is [Char] with poor performance for large text operations
  3. Using foldl instead of foldl' causing stack overflow on large lists

These mistakes appear frequently in real-world NODE code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

What does 'Function node: Error: Unexpected identifier' mean?

JavaScript syntax error. Check for missing commas, brackets, or quotes. The Function node editor does syntax highlighting but not full linting. Use an external linter for complex code.

Can I use ES6 features in Function nodes?

Yes — Node-RED runs on Node.js, which supports modern JavaScript (arrow functions, const/let, template literals, destructuring). The version depends on your Node.js version.

How do I return multiple messages from one Function node?

Return an array of messages: return [msg1, msg2, msg3]. Each message in the array is sent to a separate output port or sequentially on the same output.


DodaTech — function nodes that run without errors.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro