Node Red Function Node
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()andnode.error()for runtime logging. - Check for
undefinedbefore 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
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'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
DodaTech — function nodes that run without errors.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro