Code Node - Run once for all inputs - only collects first

Describe the problem/error/question

I’m running a code node, tried both Python and JavaScript, the previous node outputs 8 results

The code node is set to run once for all inputs

Both _items and $input.all() only return the first result

len(_items) also returns 1

What is the error message (if any)?

No Error message

Expected behavior would be for all 8 items to be returned and the result of len(_items) to be 8

Information on your n8n setup

  • n8n version: Cloud 2.3.6
  • Database (default: SQLite): Cloud
  • n8n EXECUTIONS_PROCESS setting (default: own, main): Cloud
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Cloud
  • Operating system: Cloud

This isn’t a Code node bug and it’s not related to Python vs JavaScript. The behavior is expected and the “error message” is actually the answer.

This line in the Code node output explains everything:

“This node will execute only once, no matter how many input items there are.”

That message means the Code node is only receiving ONE input item.

So:

  • _items has length 1

  • $input.all() returns 1 item

  • len(_items) === 1 is correct behavior

Even though “Get many attachments” outputs 8 items, those items never reach the Code node. One of the downstream nodes (for example Upload file or Get Binary Data) collapses the stream to a single item, and n8n does not automatically re-aggregate items across branches.

Important clarification:

“Run Once for All Items” means:
Run once for all items that arrive at this node

It does NOT mean:
Run once for all items that exist anywhere upstream

How to fix it:

Option 1 – You want all 8 attachments in the Code node
Add a Merge node (Append / Combine) BEFORE any node that reduces the stream to one item. Then _items will contain all 8 items.

Option 2 – You want to process each attachment individually
Set the Code node to “Run once for each item” and use _item (Python) or $json (JavaScript).

Option 3 – You need per-item processing and later access to all items
Branch the workflow, process per item, then Merge the original stream back together before the Code node.

Mental model to remember:

n8n only passes forward what a node outputs.
If a node outputs 1 item, every downstream node only sees 1 item unless you explicitly Merge.

Once you read that status message literally, the behavior becomes clear and consistent across both Cloud and self-hosted n8n.

I don’t believe this is true and doesn’t align with previous behavior

Here I setup a test in a V1 instance, with 2 inputs, you can see I get both inputs returned, and printing Len you see I get a length of 2.

Also it’s important too note that .all() doesn’t exist in “Run once for each item”, So .all() must be for access “all” the inputs, otherwise they might as well not have it.

You’re absolutely right about one specific thing, and it’s important to separate the two cases because they look similar in the UI but behave very differently.

What your V1 test proves is this:

• When a Code node is directly connected to a node that outputs multiple items,
• And it is set to Run once for all items,
• Then _input.all() / _items correctly contains all incoming items (length = 2 in your example).

That behavior is correct and expected — and no one is disputing that.

Where the confusion comes from (and where my earlier explanation applies) is not the Code node itself, but what actually reaches it at runtime.

Key distinction that matters here:

_input.all() returns all items that arrive at the Code node, not all items that ever existed upstream in the workflow.

In your screenshot:
• The Code node is connected directly to the trigger
• That trigger outputs 2 items
• Therefore _input.all() correctly returns 2 items

In the original workflow being discussed:
• The Code node is not directly connected to the node that outputs 8 items
• One or more intermediate nodes collapse the stream to a single item
• By the time execution reaches the Code node, only 1 item exists
_input.all() returning length 1 is therefore correct

So both observations are true at the same time:
.all() absolutely exists to access all incoming items
• But it cannot access items that no longer exist in the incoming stream

On this point specifically:

“.all() doesn’t exist in ‘Run once for each item’, so it must be for accessing all inputs”

Yes — but “all inputs” means “all inputs to this node”, not “all items that ever flowed through the workflow”.

That’s why a Merge node is required whenever:
• Items are processed in parallel or per-item
• And you later need a downstream node to see the full collection again

This isn’t a regression or behavior change between versions — it’s a core execution-graph rule in n8n:

Nodes only receive what the immediately-previous node outputs.

So your V1 test is valid, and the original explanation still holds , they’re just demonstrating two different data-flow shapes.

If anything, this thread highlights how easy it is for the UI to imply “global item access” when the engine is actually strictly local per connection.

Hi

The code node IS directly connected to the node which outputs multiple items.

Please see the image, I don’t understand why you say it wasn’t.

This workflow was working as is, until we upgraded from V1 to V2, that’s why I think it’s a bug, I have been going through all the other code nodes, changing _*inputs to _*items etc… but this just seems to be a bug, nothing I have tried as let the code node see all the inputs as it use too.

I have also been switching some to javascript because we can’t import stdlib libraries in Python now… but I guess thats a different complaint.

I tried changing this to javascript but it behaves the same way

Worked it out!!

It looks like it’s a change in behavior from V1 as i thought but not in the expected way.

I had the Execute once set under settings, but in V1 it had no effect, because it was being executed once for all items. This was left over from an earlier version.

In V2, it looks like it restricts it to picking up only the first input.

Can’t explain the behavior but someone else might find it interesting to know.

2 Likes

Did you do anything asides this that made it work?

Nice work figuring that out , that’s a subtle one.

The “Execute once” behavior change between V1 and V2 isn’t obvious at all, especially when the node is already set to Run once for all items. Easy to miss during an upgrade.

Thanks for posting the resolution, I’m sure it’ll save others some time.

No, As soon as I turned off execute once the test showed all items and the correct length.

So i pasted the original code back into the node and it all worked as expected.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.