How to use Code Node in n8n - Data Structure & Limitations | Python in n8n

Learn How to use Code Node in n8n - Data Structure & Limitations | Python in n8n in this comprehensive n8n tutorial. Step-by-step guide with video, code examples, and expert tips from the n8n Zero to Hero course.

Table of Contents

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.

Next Steps

Continue your n8n journey with the full n8n AI Automation - Zero to Hero course.

Frequently Asked Questions

The Code node requires an array of objects where each item is wrapped under a `json` key: `[{ json: { field: value } }, ...]`. Even a single output must sit inside the outer array. Binary file outputs add a `binary` key containing a mandatory `data` field plus optional fields like `mimeType`, `fileExtension`, and `fileName`. Returning any other structure, such as a plain object or a raw value, causes the node to fail with an error.

"Run once for all items" executes the code block once and exposes every incoming record together via `$input.all()`, making it the right choice for aggregation and cross-record logic such as summing order totals. "Run once for each item" executes the code separately per record, exposing only the current item via `$input.item`, and mirrors the default behavior of other n8n nodes. Switching modes also changes the variable stub the editor pre-populates.

On cloud-hosted n8n, npm package imports are blocked entirely inside the Code node. Self-hosted instances can use select built-in or whitelisted packages only. The Code node also cannot make HTTP or API calls regardless of hosting model. Use the dedicated HTTP node for external API requests and the Read File from Disk node for file system access. Both nodes are covered in subsequent lessons of this course.

Python is supported in the Code node but is currently in beta with known limitations. The primary syntax difference is that n8n built-in variables use an underscore prefix in Python, for example `_input.all()`, rather than the dollar-sign prefix used in JavaScript, for example `$input.all()`. Basic Python logic runs, but the beta status means some features may be incomplete or unavailable compared to the stable JavaScript mode.

Dheeraj Sharma

Dheeraj Sharma

AI Systems Builder
Creator of the n8n Zero to Hero course (42 lessons, 31+ hours). I help solopreneurs build AI systems that grow revenue without growing workload.

Get the n8n Mastery Bundle

All workflows, cheat sheets, and premium resources from the entire course in one package.

Get Premium Resources