Overview
This lesson is part of the n8n AI Automation - Zero to Hero course, Section: 3. Deep Dive Concepts.
Watch the video above for the full tutorial, or read the written guide below.
What is the Code node in n8n and when do you need it?
The Code node lets you write multi-line JavaScript or Python directly inside an n8n workflow as a single pipeline step. Unlike expressions, which are limited to one line, the Code node handles complex data manipulation, filtering, and object creation. Use it when built-in nodes cannot express the logic you need, for example aggregating 291 merged order-detail rows into 100 unique order totals with a running sum.
How to configure and use the Code node in n8n
The Code node exposes two parameters you must set before writing any logic. First, Language: JavaScript (stable) or Python (beta). In JavaScript, n8n built-in variables use a $ prefix, for example $input, $execution, $binary. In Python the same variables use an underscore prefix instead, for example _input. Python support is in beta and carries known limitations.
Second, Mode: "Run once for all items" executes the code block one time and makes every incoming record available together via $input.all(). This is the right choice when you need cross-record logic, such as summing order totals across 291 rows. "Run once for each item" executes the code separately per record and exposes only the current item via $input.item, mirroring how other n8n nodes behave by default. Switching modes changes the access variable automatically in the editor stub.
Every Code node must return an array of objects where each item is wrapped under a json key: [{ json: { field: value } }, ...]. Even a single result must sit inside the outer array. Binary file outputs use a binary key with a mandatory data field plus recommended fields such as mimeType, fileExtension, and fileName. Breaking this structure causes the node to fail or report an error. In the worked example, the code built an orderTotals lookup dictionary by looping through $input.all(), reading item.json.orderId and item.json.totalPrice, then converted each key-value pair into { json: { orderId: id, orderTotal: Number(total).toFixed(2) } } before returning the result array. The output count changed from 291 inputs to 100 unique-order outputs, which is valid as long as the array-of-json-objects structure is preserved.
The Code node carries two hard limitations. On cloud-hosted n8n, importing npm packages is blocked entirely. Self-hosted instances can use select built-in or whitelisted packages only. Regardless of hosting, the Code node cannot access the file system or make HTTP and API calls. Those tasks belong to dedicated nodes: the HTTP node for external requests and the Read File from Disk node for file access.
Key Takeaways
- The Code node runs multi-line JavaScript or Python as a single n8n step, filling the gap where single-line expressions run out of capability.
- "Run once for all items" gives simultaneous access to every input record via
$input.all(); "run once for each item" gives one record at a time via$input.item. Switching modes changes both the execution count and the access variable. - JavaScript variables use the
$prefix; equivalent Python variables use an underscore prefix. Python support is currently in beta with known gaps. - Every Code node output must be an array of
{ json: { ... } }objects. Returning a plain object, a raw value, or a non-array breaks the node. Output item count does not have to match input item count. - The Code node cannot import arbitrary npm packages on cloud, access the file system, or make HTTP calls. Use the HTTP node and Read File from Disk node for those tasks.
Related Lessons
- Lesson 11: How Branching works in n8n Workflows | Smart Automations with Multiple Paths
- Lesson 12: How to Use Merge Node in n8n | Combine Data Like a Pro
- Lesson 13: How to Use Set Node in n8n | Edit Fields Node | Add, Edit, Clean Data
- Lesson 14: How to Use Aggregate Node in n8n | Combine & Summarize Data
- Lesson 15: How to Use Remove Duplicates Node in n8n | Clean Your Data Fast
Next Steps
Continue your n8n journey with the full n8n AI Automation - Zero to Hero course.