Skip to main content

JavaScript Code Nodes

Expert guidance for writing JavaScript code in n8n Code nodes.

Quick Start Template

Essential rules:
  1. Choose “Run Once for All Items” mode (recommended for 95% of use cases)
  2. Access data via $input.all(), $input.first(), or $input.item
  3. Must return [{json: {...}}] format — array of objects with a json key
  4. Webhook data is under $json.body (not $json directly)
  5. Built-ins available: $helpers.httpRequest(), DateTime (Luxon), $jmespath()
Webhook data is nested under .body, not at the root.This is the most common Code node mistake.

Mode Selection

Data Access Patterns

Return Format Requirements

Always return an array of objects with a json property. Any other format causes the workflow to fail.

Built-in Functions

$helpers.httpRequest()

Make HTTP requests from within a Code node:

DateTime (Luxon)

Date and time operations using the Luxon library:

$jmespath()

Query and filter JSON structures:

Top 5 Error Patterns

10 Production-Tested Patterns

Best Practices

When to Use a Code Node

Pre-Deploy Checklist

  • Code is not empty — has meaningful logic
  • Return statement exists
  • Return format is [{json: {...}}]
  • Data access uses $input.all(), $input.first(), or $input.item
  • No {{ }} expressions — using direct JavaScript access
  • Null/undefined guards with optional chaining (?.) or defaults
  • Webhook data accessed via .body if input is from a Webhook node
  • Mode is “All Items” unless per-item isolation is required
  • All code paths return the same structure