n8n Node Library

The Code Node

Your escape hatch for anything the built-in nodes can’t quite do.

What it actually does

n8n’s built-in nodes cover an enormous range of everyday tasks, but sooner or later you’ll hit something they genuinely can’t do — a calculation with several steps, a data transformation that doesn’t map cleanly onto Edit Fields’ simple field-by-field model, logic that depends on conditions too intricate for an If node to express cleanly. The Code node is where you drop into actual JavaScript (or Python, if your n8n instance has it enabled) and write exactly what you need.

It has two modes: Run Once for All Items, where your code sees the entire batch of incoming data at once and you return an array — useful for anything that needs to look across multiple items together, like sorting or deduplication. And Run Once per Item, where your code runs individually for each item passing through, which is simpler to reason about for straightforward per-item transformations.

Inside the node, incoming data is available as $input.all() (all items) or, in per-item mode, $json (the current item’s data directly). Whatever you return becomes the node’s output, flowing on to the next step exactly like any built-in node’s result would.

A worked example

You’ve got a list of orders and need the total value of all of them combined — not something any single built-in node calculates directly. In “Run Once for All Items” mode: let total = 0; for (const item of $input.all()) { total += item.json.amount; } return [{ json: { total } }]; — three lines, and you have a single output item with the sum.

A per-item example: cleaning up a phone number field that arrives in inconsistent formats. In “Run Once per Item” mode, you can strip spaces, dashes, and parentheses with a couple of lines of JavaScript string manipulation — the kind of fiddly text-cleanup that would take several chained Edit Fields nodes to approximate, if it’s even possible that way at all.

The mistake almost everyone makes first

Forgetting the return format. The Code node expects an array of objects, each with a json key wrapping your actual data — return [{ json: { ... } }] — not just the bare data itself. Returning something like return { total: 5 }; directly (without the array-and-json wrapper) is the single most common first error, and it usually surfaces as a confusing “propertyName” or type error rather than an obvious “you formatted this wrong” message.

Related nodes

Edit Fields — try this first for simple reshaping before reaching for code · If — for branching logic simple enough not to need a script