n8n Code Node (Formerly Function Node): Modes, Limits, Python and When to Use It
By Abhishek Patoliya · 2026-08-15
The Code node is where you write JavaScript (or Python) inside an n8n workflow. It replaced the old Function and Function Item nodes in n8n 0.198.0. The rename matters less than two practical skills: knowing when a few lines of code beat a chain of built-in nodes, and knowing what the Code node can't do. It can't make HTTP requests or touch the file system, and Python works differently from JavaScript.
Before you start: read n8n expressions and paired items. Facts below follow n8n's Code node documentation as of 9 November 2026.
Two modes
| Mode | Runs | Typical code |
| Run Once for All Items (default) | Once, however many items arrive | Read $input.all(), return an array of items |
| Run Once for Each Item | Once per input item | Read $json / $input.item, return one item |
Use all-items mode for anything that looks across items (deduplicating, sorting, totals); use each-item mode for per-record transformations. A common bug is writing per-item code in all-items mode, which then only processes the first item. Since 0.166.0, n8n adds the json wrapper automatically if you return plain objects, but returning { json: {...} } explicitly keeps the intent clear and lets you add binary and pairedItem.
What the Code node can't do
- No HTTP requests and no file system access. n8n's docs say so directly and point to the HTTP Request node and the Read/Write Files from Disk node instead. That's also better design: the HTTP Request node brings credentials, retries, pagination and batching.
- Limited modules. On self-hosted n8n you can allow built-in Node.js modules (
NODE_FUNCTION_ALLOW_BUILTIN=crypto) and external npm modules installed with n8n (NODE_FUNCTION_ALLOW_EXTERNAL=lodash). If you run task runners, set these on the runners. On n8n Cloud you can't import modules; crypto and moment are provided.
- Not the expression engine. Code runs in plain JavaScript with Luxon available, so a few expression-only helpers don't exist there.
Python: native vs the old Pyodide mode
Python support changed significantly:
- Native Python (via task runners) is available from n8n 1.111.0 and stable in n8n 2. It only exposes
_items (all-items mode) and _item (each-item mode), requires bracket access (item["json"]["email"]) and blocks insecure built-ins by default. Upgrading from the old mode is a breaking change.
- Pyodide (the WebAssembly-based Python from n8n 1.0) was removed in n8n 2.0.
- On n8n Cloud, Python can't import any libraries, not even from the standard library. On self-hosted, extra modules must be included in and allow-listed on the runners image.
Unless you need Python specifically, JavaScript has the fuller feature set in n8n (all built-in variables, getBinaryDataBuffer() and so on).
When built-in nodes are better
Built-in nodes document themselves. "Filter: status is equal to active" is readable on the canvas without opening anything, and non-developers can maintain it. For renaming fields, simple conditions, filtering, merging and aggregation, use Edit Fields, IF/Filter, Merge and Aggregate even if code would be shorter.
When the Code node is the right call
- One coherent transformation that would take four or more built-in nodes chained together.
- String parsing beyond simple patterns, custom date arithmetic, recursive restructuring of nested data.
- Algorithmic logic: deduplication by a composite key, rolling calculations, sorting by a derived value.
Example: deduplicate by email + campaign, keeping the newest
// Run Once for All Items
const newest = new Map();
for (const item of $input.all()) {
const key = `${item.json.email.toLowerCase()}::${item.json.campaignId}`;
const current = newest.get(key);
if (!current || new Date(item.json.updatedAt) > new Date(current.json.updatedAt)) {
newest.set(key, item);
}
}
return [...newest.values()];
It returns a subset of the original items, so n8n can keep the item links. If you build brand-new items instead, set pairedItem (see paired items).
Handling bad records
A thrown error fails the node and, by default, the execution. Two options:
- Catch per record and mark it, so downstream nodes can route problems:
// Run Once for Each Item
try {
return { json: { ...$json, normalized: $json.email.trim().toLowerCase() } };
} catch (err) {
return { json: { ...$json, normalized: null, error: err.message } };
}
- Or let it throw and use the node's Settings → On Error → Continue (using error output) to route failures on a separate output.
Debugging and testing
console.log() writes to your browser's developer console while you test in the editor (Python: print()).
- For non-trivial logic, write it as a plain function, test it outside n8n with a normal test runner (empty input, missing fields, wrong types), then paste it in with a thin wrapper around
$input.all().
- Pin realistic input on the previous node (pin data) so each test run uses the same items.
When it shouldn't live in n8n at all
If logic needs libraries you can't enable, heavy computation, or is reused across many workflows, put it behind a small internal API and call it with the HTTP Request node. It then gets version control and tests of its own, and n8n keeps doing orchestration.
Key takeaways
- The Code node replaced Function and Function Item (n8n 0.198.0); choose all-items or each-item mode deliberately.
- It can't make HTTP requests or access files; use the HTTP Request and file nodes.
- Python is native (task runners) from 1.111.0, stable in n8n 2; Pyodide was removed in 2.0; Cloud Python can't import libraries.
- Default to built-in nodes for readability; use code for genuinely algorithmic steps.
Exercise: pin 10 items with duplicate emails in mixed case and run the deduplication code. Then rewrite it in each-item mode and explain why that can't work. Next lesson: binary data.
References (checked 9 November 2026)
Written by Abhishek Patoliya, Senior n8n Developer & Automation Architect, Surat, India. Verified n8n Creator.