n8n Code Node Explained: JavaScript & Python in Your Workflows (2025)
Sooner or later a built-in node won’t do exactly what you need — and that’s when the Code node earns its place. It lets you run JavaScript or Python directly inside a workflow to reshape data, do math, or build a payload no other node can. Here’s how to use it well without turning your workflow into a black box.
Run Once for All Items vs. Run Once for Each Item
The Code node has two modes. Run Once for All Items gives you the entire items array at once — ideal for aggregating, deduping, or sorting. Run Once for Each Item runs your snippet per item, exposing $json for the current item — cleaner for simple per-record transforms. Pick the one that matches the shape of the work.
The data structure you must return
n8n expects the Code node to return an array of objects, each wrapped as { json: { ... } }. A minimal snippet looks like: return items.map(i => ({ json: { name: i.json.first + ' ' + i.json.last } }));. Return the wrong shape and you’ll get “Code doesn’t return items properly” — always wrap your data under a json key.
⚙️ Skip the build: Text-to-SQL — Ask Your Database in Plain English
Skip the custom code entirely — an n8n workflow that turns plain-English questions into safe SQL and returns the answer.
Get the ready-to-import template on Gumroad →
JavaScript or Python?
JavaScript is the default and has the richest support, including helper variables like $json, $node, and $now. Python (Beta) runs via Pyodide and is great if your team thinks in Python, but it’s slower to start and can’t use arbitrary pip packages. For most glue-code tasks, JavaScript is the pragmatic choice.
When NOT to use the Code node
Reach for built-in nodes first. Filtering? Use the Filter node. Renaming or setting fields? Use the Set (Edit Fields) node. Merging sources? Use Merge. The Code node is for logic those nodes can’t express — custom parsing, non-trivial math, or building an odd API payload. Every Code node is code someone has to maintain, so use it deliberately.
Debugging tips
Use console.log() and check the browser console, or simply return intermediate values to inspect them in the node output panel. Guard against missing fields with optional chaining (i.json?.email) so one malformed record doesn’t crash the whole run. And keep snippets short — if a Code node grows past ~30 lines, consider splitting the logic across nodes for readability.
Ready to automate?
Get the Text-to-SQL — Ask Your Database in Plain English workflow — import the JSON, connect your accounts, and it runs on autopilot.