When you’re building automation workflows in n8n, you’ll often find yourself needing to do something a little… custom. Maybe you need to perform a complex calculation, reformat data in a very specific way, or apply conditional logic that’s just beyond the standard nodes. That’s precisely where the n8n Function Node — now officially known as the Code node — becomes your best friend. This node lets you drop in JavaScript or Python code right into your workflow, giving you near-limitless flexibility to manipulate data and implement unique business logic. It’s the go-to tool for breaking free from the constraints of pre-built nodes and truly tailoring your automation.
Understanding the Power of the Code (Formerly Function) Node
Let’s be honest, while n8n has hundreds of fantastic pre-built nodes for connecting to apps and performing common tasks, no platform can cover everything. There will inevitably be times you need to apply a specific algorithm, perform string manipulation based on complex rules, or transform data structures in ways only code can easily achieve. This is where the Code node shines.
Think of it like this: most nodes are like pre-packaged tools – great for their specific job. The Code node is your workshop where you can build any custom tool you need using code. If you’re comfortable with a bit of JavaScript or Python, this node unlocks a whole new level of automation possibilities within n8n.
(And hey, if you’ve been using older versions of n8n, you might know this as the Function or Function Item node. They’ve been consolidated and improved into the single, more powerful Code node since version 0.198.0. Just wanted to clear that up right away!)
Choosing Your Execution Mode: Run Once or Loop Through Items
The Code node offers two main ways to run your code, which dramatically impacts how you handle the data flowing into it:
- Run Once for All Items: This is the default. Your code executes once for all the data items passed into the node from the previous step. You’ll typically work with an array containing all input items. This is great for aggregating data, performing calculations across the entire dataset, or setting a single value based on all inputs.
- Run Once for Each Item: Select this if you want your code to run individually for every single data item that enters the node. Your code will receive one item at a time. This is perfect for transforming or processing each item independently, like formatting addresses or calculating a value unique to each record.
Understanding which mode to use is step one. Step two is knowing how to grab the data in your code!
Navigating Data: The items
Saga and Current Best Practices
Okay, here’s a common sticking point, especially if you’re looking at older examples or migrating workflows. In the old Function node, you often accessed input data via a global items
array. Simple, right?
Well, with the current Code node, things are structured a bit differently for clarity and consistency. You now use the $input
object to access data.
- To get all input items when running in “Run Once for All Items” mode, you’ll use
$input.all()
. - To get the current item when running in “Run Once for Each Item” mode, you’ll use
$input.item
.
I’ve seen folks in the community forums (like the one provided!) confused because code using items
directly suddenly didn’t work in the new Code node. The error “items is not defined” was a clear sign! A quick fix, as pointed out in the forum, might be adding let items = $input.all();
at the top for compatibility with older code snippets, but learning to use $input.all()
and $input.item
is the modern, recommended way.
Your code inside the node should then process this data (either the array from $input.all()
or the single item from $input.item
) and return a new array of data items. The structure of the output array is crucial for the next node in your workflow. It usually needs to be in the format [{ json: { ... your data ... } }]
or a similar structure depending on what you’re doing. Managing item linking – making sure your output items correspond correctly to input items, especially if you add or remove items – is another advanced topic you’ll tackle here.
JavaScript vs. Python: Which Language to Choose?
The Code node supports both JavaScript (Node.js) and Python.
- JavaScript: This is the native language of n8n’s backend, so it’s generally faster. You can use promises, and standard built-in modules (plus
moment
andcrypto
in n8n Cloud). If you self-host, you can even enable importing other npm packages! - Python: Added in n8n v1.0, Python support uses Pyodide, which runs Python via WebAssembly. This is fantastic if you’re a Python expert or need a specific library available in Pyodide. However, it can be a bit slower due to the compilation step, and library support is limited compared to standard Python or Node.js.
Choose the language you’re most comfortable with, keeping the performance and library availability differences in mind.
Real-World Applications and Practical Examples
So, when would you actually use this powerful function node n8n offers?
- Data Cleanup: Standardize phone numbers, parse complex strings, or extract specific pieces of text using regular expressions.
- Calculations: Perform mathematical operations that depend on multiple data points or require complex formulas.
- Conditional Logic: Implement branching or data modification based on intricate conditions that the standard
IF
node can’t handle easily. - Data Formatting: Reshape JSON objects, create custom CSV rows, or generate unique IDs based on input data.
Let’s say you get data with first_name
and last_name
separately, but the next API needs a single full_name
field formatted as “Last, First”. A Code node running “Run Once for Each Item” could easily take $input.item.json.first_name
and $input.item.json.last_name
, combine them, and return a new item like { json: { full_name: $input.item.json.last_name + ', ' + $input.item.json.first_name } }
. Simple, but sometimes necessary!
Tips for Mastering the Code Node
- Use
console.log()
: Just like regular coding,console.log()
is your best friend for debugging. Check the output in the node’s “Output” data or the browser console. - Know the Limitations: Remember you can’t access the file system or make external HTTP calls from within the Code node. Use the dedicated Read/Write File and HTTP Request nodes for those tasks.
- Leverage Built-in Methods: n8n provides handy built-in variables and functions (like
$json
,$nodes
,$env
, date/time helpers) accessible with$
in JavaScript (_
in Python). Type$
or_
in the code editor to see suggestions! - Check the Docs: The official n8n documentation for the Code node (and Built-in methods) is thorough and constantly updated.
- AI Assist (Cloud): If you’re on n8n Cloud, don’t forget the AI tab in the Code node – it can help you generate boilerplate code based on your description.
Conclusion: Unleash Your Automation Potential
The function node n8n provides, now called the Code node, is an incredibly powerful tool in your automation arsenal. It’s where you inject tailored logic, overcome limitations, and truly customize data flow in your workflows. Don’t be intimidated by writing a little code; start simple, use the built-in tools, and soon you’ll be building complex, highly specific automations that wouldn’t be possible otherwise. So, dive in, experiment, and unlock the full power of custom code in your n8n workflows!