The Code node: when code is actually needed
The Code node runs JavaScript (or Python) inside a workflow. That is what "low-code" means in n8n: when the ready-made nodes run out, there is a way out.
The most important parameter is the mode, and there are two:
- Run Once for All Items (the default) — the code runs once with access to every incoming item together. For grouping, overall calculations, or changing the item count.
- Run Once for Each Item — the code runs separately per item. More readable for simple transformations, and
$jsonhere gives the current item.
The code must return the result in item structure. The good news: if you forget the json key or the array wrapper in the Code node, n8n adds them for you.
// Rejim / Mode: Run Once for All Items
// Bütün item-lərə çıxış var / access to every item at once
const items = $input.all();
// Regionlara görə qruplaşdır / group by region
const byRegion = {};
for (const item of items) {
const region = item.json.region ?? 'unknown';
byRegion[region] ??= { region, total: 0, count: 0 };
byRegion[region].total += Number(item.json.amount) || 0;
byRegion[region].count += 1;
}
// Hər qrup üçün bir item qaytar / return one item per group
return Object.values(byRegion).map((row) => ({ json: row }));"Run Once for All Items" mode: 500 items in, one item per group out.
// Rejim / Mode: Run Once for Each Item
// $json cari item-in datasıdır / $json is the current item's data
const raw = $json.phone ?? '';
const digits = raw.replace(/\D/g, '');
return {
json: {
...$json,
phoneNormalized: digits.startsWith('994') ? `+${digits}` : `+994${digits}`,
isValid: digits.length >= 9,
},
};"Run Once for Each Item" mode: runs per item, and the item count stays the same.
| Limitation | What to do instead |
|---|---|
| No file system access | Use the Read/Write Files from Disk node |
| You cannot make HTTP requests | Use the HTTP Request node |
| On n8n Cloud you cannot import external npm modules | Use the built-in `crypto` and `moment`; on self-hosted they can be enabled through configuration |
| `$binary`, `$if()` and `$jmespath()` are absent in the Code node | They exist only in the expression editor |
| Some Luxon methods take different arguments | Check for the "custom n8n functionality" marker in the docs and use the native equivalent |
About Python. Since n8n 1.111.0 there is native Python support through task runners, and it is stable as of n8n 2. The older Pyodide-based support no longer exists in n8n 2. Native Python supports only _items (all-items mode) and _item (per-item mode) — the other n8n variables are not available — and requires bracket access, item["json"]["field"], rather than dots. The practical conclusion: JavaScript has the wider surface in n8n.
📚 Sources and documentation
- Using the Code nodeofficialdocs.n8n.io
The official source for the modes, the limitations and Python support.
- Code node cookbookofficialdocs.n8n.io
- Code node — common issuesofficialdocs.n8n.io