Most n8n Code node errors come down to one thing: the shape of what you return. n8n passes data between nodes as a list of items, each wrapped in a json object, and if your code hands back a bare string or an array in the wrong place, the node fails with "Code doesn't return items properly".
Once that shape clicks, the Code node gets simple. You read items in, change them, and send items out.
This guide covers the item structure, the two run modes, the built-in variables you'll actually use, copyable JavaScript and Python snippets for common transforms, and the cases where a regular node beats code. If you're still wiring up APIs, my n8n HTTP Request node guide pairs well with this one, because most Code nodes sit right after an API call.
Everything here comes from n8n's official docs. I haven't run timed tests for this post, and the snippets are written from those docs.

What is the n8n Code node?
The Code node runs your own JavaScript or Python as a step in a workflow. You use it to transform data or add logic that the built-in nodes don't cover.
It replaced the older Function and Function Item nodes in n8n 0.198.0. If you find an old tutorial using those, the ideas carry over, but the variable names have changed.
There are two places to write code in n8n: the Code node and the expressions editor (the {{ }} fields inside other nodes). Expressions are for one value in one field. The Code node is for reshaping whole items or whole lists.
Here's what each language gets, per n8n's docs:
| Feature | JavaScript | Python (native) |
|---|---|---|
| Built-in variables | Full set ($input, $json, $('Node') and more) | Only _items (all-items mode) and _item (per-item mode) |
| Field access | item.json.name or item.json["name"] | item["json"]["name"] only |
| Libraries on n8n Cloud | crypto and moment only | No imports at all |
| Libraries self-hosted | Allowlisted npm and Node.js built-ins | Allowlisted stdlib and pip packages |
| Debug output | console.log() | print() |
My take: use JavaScript unless you have a specific Python library you need on a self-hosted instance. JavaScript gets every built-in helper, and most community examples are in it.
How does n8n structure data between nodes?
All data passed between nodes is an array of objects. Each object is one item, and its data lives under a json key:
Files ride along under a separate binary key, with base64 data plus optional mimeType, fileName and fileExtension fields.
Two details save a lot of confusion. First, nodes process each item on their own, so a Slack node that receives 20 items sends 20 messages. Second, since n8n 0.166.0 the Code node adds the json key and the outer array for you if you leave them out. Other nodes don't do that, which is why the shape matters.

What's the difference between the two run modes?
The Mode dropdown decides how often your code runs. This is the setting people get wrong most often.
| Mode | Runs | Read input with | Return |
|---|---|---|---|
| Run Once for All Items (default) | One time, whatever the item count | $input.all() | An array of items |
| Run Once for Each Item | Once per input item | $input.item or $json | One item (an object) |
Run Once for All Items is the right default for anything that looks across items: sorting, deduplicating, totals, grouping, or turning one item into many.
Run Once for Each Item suits simple per-row changes, like cleaning one field. It reads more cleanly, but anything that compares items with each other won't work in it.
If your output has a different number of items than your input, you want all-items mode.
Which built-in variables do you actually need?
n8n ships a long list of built-ins, but a handful cover almost every Code node. Type $ in the editor to see the full list with autocomplete.
| Variable | What it returns |
|---|---|
| $input.all() | Every input item, as an array |
| $input.first() / $input.last() | The first or last input item |
| $input.item | The current item (per-item mode) |
| $json | Shorthand for the current item's json (per-item mode) |
| $('Node Name').all() | Every output item of an earlier node |
| $('Node Name').first() | The first output item of an earlier node |
| $('Node Name').item | The earlier node's item that produced the current one |
| $('Node Name').isExecuted | Whether that node has run in this execution |
The $('Node Name') helpers are how you reach back past the previous node. For example, a Code node after an HTTP Request can still read the original form submission from your Webhook node. If you build webhook flows, my guide to n8n webhooks shows where those items come from.
How do you write your first Code node in JavaScript?
Add a Code node, leave Language on JavaScript and Mode on Run Once for All Items. Then replace the sample code with this:
Select Execute step and check the output panel. You should see the same number of items, each with the cleaned email and the new isPaid field.
The same change in Run Once for Each Item mode is shorter:
Both work. The difference shows up as soon as you need to look at more than one item at a time.
What are the most common Code node transforms?
These are the patterns I'd keep in a snippets file. All of them use all-items mode.
Filter items by a condition
Pick and rename fields
Split one item with a list into many items
APIs often return one item holding an array, like a results field. To work with each result on its own:
Combine many items into one
Remove duplicates by a key

How do you keep item linking working?
Item linking is how n8n knows which input item produced which output item. It's what makes $('Node Name').item work in later nodes.
n8n handles this for you with a single item, and when your code edits items in place. Per n8n's docs, you need to set it yourself when you create new items and there's more than one input item. Add a pairedItem field with the index of the source item:
Skip this and a later node using $('Webhook').item can fail to find the matching item, or pick the wrong one. If you split one item into many, point every new item at that one source index.
How does Python work in the Code node?
n8n now runs Python natively through task runners. Native Python arrived in n8n 1.111.0 and is stable as of n8n 2. The older Pyodide version (Python compiled to WebAssembly) was removed in n8n 2.0, so old Python snippets may break.
Native Python has fewer helpers than JavaScript. You get _items in all-items mode and _item in per-item mode, and you must use bracket access:
Dot access like item.json.email worked in Pyodide but not in native Python. That's the first thing to fix in an older workflow.
On n8n Cloud, Python can't import any library, not even from the standard library. Self-hosted, you can allowlist modules with the N8N_RUNNERS_STDLIB_ALLOW and N8N_RUNNERS_EXTERNAL_ALLOW settings in the runner config, as long as the runner image includes them.
Can the Code node use npm packages, HTTP calls or credentials?
Partly, and the limits are deliberate.
- npm packages: on n8n Cloud you only get the crypto module and the moment package. Self-hosted, you can allowlist built-in modules with NODE_FUNCTION_ALLOW_BUILTIN and npm packages with NODE_FUNCTION_ALLOW_EXTERNAL, after installing them in your n8n or runner image.
- import statements: not supported. Use require() instead, or you'll get "'import' and 'export' may only appear at the top level".
- Credentials: Code nodes can't read n8n credentials by design. For authenticated API calls, use the HTTP Request node with a stored credential. My n8n credentials guide covers the setup.
- Files and HTTP: n8n's docs point you to the Read/Write Files from Disk and HTTP Request nodes for these jobs rather than code.
If you self-host, n8n's docs are blunt about task runners: they're the only isolation layer between workflow code and your n8n instance. Without external mode, anyone who can edit a workflow could reach your database, encryption key and stored credentials. Internal mode is deprecated from n8n 3.0.
For a self-hosted setup I'd run n8n with Docker on a Hostinger KVM VPS and add the n8nio/runners sidecar container in external mode. My step-by-step guide to self-hosting n8n with Docker walks through the base install.

How do you debug a Code node?
Start small and look at the data at each step.
- Log it: console.log() in JavaScript or print() in Python writes to your browser's developer console when you run the node from the editor.
- Pin input data: run the earlier nodes once, then pin their output so you can rerun the Code node without hitting the API again.
- Return early: return $input.all() at the top of your code to confirm what's coming in before you change anything.
- Check for missing fields: an undefined value from one odd item is the usual cause of "Code doesn't return an object".
Here's what the common errors mean, from n8n's troubleshooting docs:
| Error | Usual cause | Fix |
|---|---|---|
| Code doesn't return items properly | Returned something other than an array of items | Return an array of objects with a json key |
| A 'json' property isn't an object | Set json to an array or a string | Wrap the value: { json: { results: [...] } } |
| Code doesn't return an object | Nothing returned, or undefined | Add a return, check the field exists on every item |
| 'import' and 'export' may only appear at the top level | Used ES import syntax | Switch to require() |
| Cannot find module | Module not installed or not allowlisted | Install it and allowlist it (self-hosted only) |
| $getCredentials is not defined | Tried to read credentials in code | Use the HTTP Request node with a credential |
For runtime failures in production, pair this with an error workflow. My n8n error handling guide shows how to set up retries and alerts.
When should you avoid the Code node?
Code is the most flexible node, and also the hardest one for someone else to read later. n8n's own docs recommend dedicated nodes for common transforms. Reach for these first:
| You want to | Use this node instead |
|---|---|
| Add, rename or remove fields | Edit Fields (Set) |
| Keep only some items | Filter |
| Split a list field into items | Split Out |
| Group items into one list | Aggregate |
| Totals, counts, pivot-style summaries | Summarize |
| Drop repeated items, even across runs | Remove Duplicates |
| Order or cap the item list | Sort and Limit |
| Call an API | HTTP Request |
My rule of thumb: if two or more nodes would do it cleanly, skip the code. Use the Code node when the logic needs loops, several conditions, or a calculation no node offers, like scoring a lead from five fields. You'll find plenty of both styles in my 30 n8n workflow examples.
The Code node also fits well inside AI workflows. In an n8n AI agent, a short Code node is a clean way to trim an API response before the model sees it, which keeps prompts smaller.
Frequently asked questions
Wrapping up
The Code node is small once you know its contract: items in, items out, each one wrapped in json. Pick the mode by whether your logic looks across items, set pairedItem when you create new items, and keep credentials and API calls in the nodes built for them.
Start with one of the transforms above on real data, then swap it for a dedicated node wherever one does the same job.


