Sparround

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 $json here 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.

javascript
// 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.

javascript
// 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.

LimitationWhat to do instead
No file system accessUse the Read/Write Files from Disk node
You cannot make HTTP requestsUse the HTTP Request node
On n8n Cloud you cannot import external npm modulesUse the built-in `crypto` and `moment`; on self-hosted they can be enabled through configuration
`$binary`, `$if()` and `$jmespath()` are absent in the Code nodeThey exist only in the expression editor
Some Luxon methods take different argumentsCheck 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