To use the Code node in n8n, add it to your workflow, choose a mode (Run Once for All Items, the default, or Run Once for Each Item), pick JavaScript or Python and write your code. Use the built-in variables, return the items you want the next node to get, and remember that npm modules only work on self-hosted n8n.
Sooner or later every n8n workflow needs something the built-in nodes don't quite do: reshape some JSON, calculate a field, merge two lists. The Code node is for exactly that. It lets you write custom JavaScript or Python and run it as a step in your workflow.
Pick the mode before you write a line
| Mode | How it runs | Use it when |
|---|---|---|
| Run Once for All Items (default) | Once, however many items come in | You need to see all items together, such as totals or de-duplication |
| Run Once for Each Item | Once per incoming item | Each item is handled on its own, such as formatting a field |
Choosing the wrong mode is the most common reason code behaves oddly. If you write per-item logic in the default mode, it only runs once and you'll wonder where the rest went.
Adding the node and writing code
- Add a Code node after the node whose data you want to process.
- Choose a mode: Run Once for All Items or Run Once for Each Item.
- Choose JavaScript, or Python where your n8n version supports it.
- Write your code using n8n's built-in methods and variables. They start with a special prefix symbol, and typing that symbol shows suggestions.
- Return the items your code produces, so the next node receives them.
- Run the node and use console.log, which prints to your browser console, to debug.
The JavaScript side supports Node.js, including Promises, so you can await asynchronous work. If you're coming from an older workflow, the Code node replaced the Function and Function Item nodes from n8n 0.198.0.
Python: what changed
Python in the Code node has been through two versions, and it matters which one an old tutorial describes.
- Pyodide Python is legacy and was removed in n8n 2.0. It had no file system or HTTP request access and was slower than JavaScript.
- Native Python runs through task runners. It's available from n8n 1.111.0 and stable from n8n 2.
- Native Python uses _items in all-items mode and _item in per-item mode.
- It only accepts bracket access notation, for example item["json"]["my_new_field"].
- On self-hosted n8n, it can import standard library modules and the third-party modules that the n8nio/runners image includes and allowlists.
Modules: the Cloud versus self-hosted split
This is where the n8n Docs draw a hard line. If you self-host n8n, you can import built-in and external npm modules. On n8n Cloud you can't import external npm modules at all. What you do get everywhere are the two modules n8n makes available: the crypto Node.js module and the moment npm package.
So before you plan a workflow around a favourite library, check where it'll run. On Cloud, you'll be working with plain JavaScript plus crypto and moment, which covers hashing, IDs and date handling but not much more.
What the Code node isn't for
The docs say you can't access the file system or make HTTP requests from the Code node; that's stated for the legacy Python mode, and n8n points you to dedicated nodes either way. Use the Read/Write File From Disk node for files and the HTTP Request node for API calls. Keeping those jobs in their own nodes also makes the workflow easier to read and debug.
Scripting steps in Zapier and Pipedream
Code by Zapier runs JavaScript inside a Zap, as one step among your apps. Pipedream supports writing Node.js (v20) at any point in a workflow, though its docs banner says Workflows are shutting down, so check that before you build there. My n8n versus Zapier comparison looks at which suits code-heavy automation.
Want AI doing the heavy lifting in your marketing?
I build the systems that handle the boring 80 percent, so you get your week back. Done properly, with the human kept in.
My advice for your first Code node
Start small: one Code node that reshapes data, in the mode that matches the job, returning clean items. Log as you go, keep file and API work in their own nodes, and check whether you're on Cloud or self-hosted before you reach for a module. Once you're comfortable, the Code node is what lets n8n handle almost anything. For more ambitious builds, see my guide to building AI agents in n8n.
Want your tool on this list? Featured spots and reviews here are paid partnerships. Here's how to partner with me.
Frequently asked questions
What languages does the n8n Code node support?
JavaScript, and Python, with native Python from n8n 1.111.0.
What are the Code node modes?
Run Once for All Items, the default, and Run Once for Each Item.
Can I use npm packages?
Only on self-hosted n8n. On Cloud only crypto and moment are available.
Did Python support change?
Yes. Pyodide Python was removed in n8n 2.0, and native Python uses task runners.
Does the Code node replace the Function node?
Yes. It replaces the Function and Function Item nodes from n8n 0.198.0.


