Skip to content
n8n · error message

n8n: Cannot read properties of undefined (reading '...') [line N]

ConfigurationCode node

Last verified

The exact message
Cannot read properties of undefined (reading 'length') [line 2]

What it means

This is a JavaScript TypeError raised inside a Code node (or the older Function node). The code tried to read a property from something that is undefined. n8n appends the line number in square brackets, so [line 2] tells you exactly where.

In n8n it almost always means the incoming data has a different shape than the code expects: a field name is different, a level of nesting is missing, an item has no json because the previous node returned nothing, or the node is in "Run Once for All Items" mode while the code is written for a single item (or the reverse). AI and HTTP nodes are common sources, because their output structure varies.

What is not wrong: n8n itself. The fix is in the code or the input data.

What it looks like

// Run Once for All Items
return $input.all().map(item => ({
  json: { count: (item.json?.results ?? []).length }
}));

Why it happens

  • A field path in the code does not exist in this item (typo, renamed field, different nesting).
  • The previous node returned no items or an empty response.
  • The Code node mode does not match how the code accesses items.
  • Some items have the field and others do not.

How to fix it

  1. Go to the line number in the error.
  2. Open the node's input panel and check the real structure of the data.
  3. Log the input to inspect it: console.log(JSON.stringify($input.all())).
  4. Use $input.first().json or $input.item.json according to the node's mode.
  5. Guard optional data with optional chaining and defaults, for example item.json?.results ?? [].

How to stop it recurring

Validate inputs at the start of Code nodes, handle empty and missing data explicitly, and prefer built-in nodes such as Edit Fields where possible, since they tolerate shape changes better.

Official documentation: docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.code/common-issues. See all n8n error messages or the n8n limits and quotas.

Related errors

Definitions

Quick answers

What does "Cannot read properties of undefined (reading '...') [line N]" mean in n8n?

This is a JavaScript TypeError raised inside a Code node (or the older Function node). The code tried to read a property from something that is undefined . n8n appends the line number in square brackets, so [line 2] tells you exactly where.

How do I fix "Cannot read properties of undefined (reading '...') [line N]"?

1. Go to the line number in the error. 2. Open the node's input panel and check the real structure of the data. 3. Log the input to inspect it: console.log(JSON.stringify($input.all())). 4. Use $input.first().json or $input.item.json according to the node's mode. 5. Guard optional data with optional chaining and defaults, for example item.json?.results ?? [].

Error handling you can see in the workflow

Autonoly workflows have per-node retries, timeouts, continue-on-fail and error-handler branches, so a failing step can be retried or routed instead of stopping the whole run. Saved workflows have no loop node; lists are worked through by the AI agent or one run per trigger item.

Also see Visual workflow builder.