Now liveThe Skillselion MCP - thousands of ranked skills, loaded into your agent mid-task. No install.Get it →
czlonkowski avatar

N8n Code Javascript

  • 5.3k installs
  • 6k repo stars
  • Updated August 4, 2026
  • czlonkowski/n8n-skills

n8n-code-javascript is an n8n agent skill for writing production JavaScript in Code nodes with correct modes, data access, return shape, and sandbox-safe helpers.

About

This n8n agent skill teaches expert JavaScript for Code nodes across production workflows. It covers choosing Run Once for All Items versus Each Item mode, with All Items recommended for aggregation, filtering, batch transforms, and cross-item logic while Each Item suits isolated per-row API calls. Data access patterns use $input.all(), $input.first(), $input.item, and $node references always via .json, plus the critical webhook rule that POST fields live under $json.body. Return format must be an array of {json: {...}} objects on every code path. Built-ins include this.helpers.httpRequest for unauthenticated calls, DateTime (Luxon), $jmespath, and $getWorkflowStaticData, while auth helpers and $env may be sandbox-blocked. Production guidance covers SplitInBatches output wiring, cross-iteration accumulation, pairedItem for downstream Set nodes, and performance: All Items costs roughly twenty-five times less per item than Each Item on large datasets. Use when workflows need custom data transformation, HTTP parsing, loop logic, or Code node troubleshooting; switch to n8n-code-tool for AI Agent Custom Code Tool nodes.

  • Run Once for All Items is the default for aggregation, filtering, and batch transforms; Each Item only for isolated per-
  • Must return [{json: {...}}] on every branch; webhook POST fields live under $json.body not top-level $json
  • Use this.helpers.httpRequest for unauthenticated calls; $helpers global is undefined in the task-runner sandbox
  • SplitInBatches main[0] is done output, main[1] is loop body; accumulate cross-iteration data via $getWorkflowStaticData
  • All Items mode costs roughly twenty-five times less per item than Each Item on large datasets

N8n Code Javascript by the numbers

  • 5,348 all-time installs (skills.sh)
  • +160 installs in the week ending Aug 5, 2026 (Skillselion tracking)
  • Ranked #66 of 2,715 Automation & Workflows skills by installs in the Skillselion catalog
  • Security screen: MEDIUM risk (skills.sh audit)
  • Data as of Aug 5, 2026 (Skillselion catalog sync)
At a glance

n8n-code-javascript capabilities & compatibility

Capabilities
code node mode selection · webhook data parsing · batch loop patterns · sandbox http requests
Works with
n8n
Use cases
orchestration · api development
From the docs

What n8n-code-javascript says it does

Always use this skill when a workflow needs a Code node — whether for data aggregation, filtering, API calls, format conversion, batch processing logic, or any custom JavaScript.
SKILL.md
Code **Each Item** (a full sandbox per item) | ~0.6 ms/item — ~25–30× All Items
SKILL.md
npx skills add https://github.com/czlonkowski/n8n-skills --skill n8n-code-javascript

Add your badge

Show developers this skill is listed on Skillselion. Paste this into your README.

Listed on Skillselion
Installs5.3k
repo stars6k
Security audit2 / 3 scanners passed
Last updatedAugust 4, 2026
Repositoryczlonkowski/n8n-skills

How do you write reliable, fast JavaScript in n8n Code nodes without wrong return formats, webhook nesting mistakes, or per-item performance traps?

Expert JavaScript guidance for n8n Code nodes covering modes, data access, return format, sandbox helpers, and production loop patterns.

Who is it for?

n8n workflows needing aggregation, custom transforms, webhook parsing, SplitInBatches loops, or Code node error troubleshooting at scale.

Skip if: AI Agent Custom Code Tool nodes (use n8n-code-tool), simple field mapping (use Set node), or authenticated HTTP (prefer HTTP Request node).

When should I use this skill?

Writing JavaScript in n8n Code nodes, choosing execution mode, using $input/$json/$node syntax, or debugging slow Code node workflows.

What you get

Correct All Items versus Each Item choice, valid [{json}] returns, sandbox-safe HTTP and DateTime usage, and production patterns for loops, pairedItem, and large datasets.

  • n8n Code node scripts
  • Working workflow transform logic

Files

SKILL.mdMarkdownGitHub ↗

JavaScript Code Node

Expert guidance for writing JavaScript code in n8n Code nodes.

---

Quick Start

// Basic template for Code nodes
const items = $input.all();

// Process data
const processed = items.map(item => ({
  json: {
    ...item.json,
    processed: true,
    timestamp: new Date().toISOString()
  }
}));

return processed;

Essential Rules

1. Choose "Run Once for All Items" mode (recommended for most use cases) 2. Access data: $input.all(), $input.first(), or $input.item 3. CRITICAL: Must return [{json: {...}}] format 4. CRITICAL: Webhook data is under $json.body (not $json directly) 5. Built-ins available: this.helpers.httpRequest() (no auth — the bare $helpers global is undefined in the task-runner sandbox, so $helpers.httpRequest() throws ReferenceError: $helpers is not defined; ignore the validator if it suggests $helpers), DateTime (Luxon), $jmespath(). Not available: this.helpers.httpRequestWithAuthentication (deny-listed), $env (when N8N_BLOCK_ENV_ACCESS_IN_NODE=true), require() (unless allowlisted). For anything beyond a trivial unauthenticated GET (auth, pagination, retries), prefer the HTTP Request node and keep Code nodes for pure logic. 6. Instance-allowlisted libraries: Self-hosted instances can allowlist modules via N8N_RUNNERS_ALLOWED_BUILT_IN_MODULES and N8N_RUNNERS_ALLOWED_EXTERNAL_MODULES (legacy: NODE_FUNCTION_ALLOW_BUILTIN / NODE_FUNCTION_ALLOW_EXTERNAL). If the user says their instance allows specific modules (e.g. axios, lodash, crypto), use them via require() — don't refuse. If unsure, ask or default to built-ins only. 7. Wrong skill? If you're writing code for a Custom Code Tool attached to an AI Agent (@n8n/n8n-nodes-langchain.toolCode), stop — that node has a different contract (input via query, must return a string, no $input/$helpers). Use the n8n-code-tool skill.

---

Mode Selection Guide

The Code node offers two execution modes. Choose based on your use case:

Run Once for All Items (Recommended - Default)

Use this mode for: 95% of use cases

  • How it works: Code executes once regardless of input count
  • Data access: $input.all() or items array
  • Best for: Aggregation, filtering, batch processing, transformations, API calls with all data
  • Performance: Faster for multiple items (single execution)
// Example: Calculate total from all items
const allItems = $input.all();
const total = allItems.reduce((sum, item) => sum + (item.json.amount || 0), 0);

return [{
  json: {
    total,
    count: allItems.length,
    average: total / allItems.length
  }
}];

When to use:

  • ✅ Comparing items across the dataset
  • ✅ Calculating totals, averages, or statistics
  • ✅ Sorting or ranking items
  • ✅ Deduplication
  • ✅ Building aggregated reports
  • ✅ Combining data from multiple items

Run Once for Each Item

Use this mode for: Specialized cases only

  • How it works: Code executes separately for each input item
  • Data access: $input.item or $item
  • Best for: Item-specific logic, independent operations, per-item validation
  • Performance: Slower for large datasets (multiple executions)
// Example: Add processing timestamp to each item
const item = $input.item;

return [{
  json: {
    ...item.json,
    processed: true,
    processedAt: new Date().toISOString()
  }
}];

When to use:

  • ✅ Each item needs independent API call
  • ✅ Per-item validation with different error handling
  • ✅ Item-specific transformations based on item properties
  • ✅ When items must be processed separately for business logic

Decision Shortcut:

  • Need to look at multiple items? → Use "All Items" mode
  • Each item completely independent? → Use "Each Item" mode
  • Not sure? → Use "All Items" mode (you can always loop inside)

Why "All Items" is faster — the per-item boundary

Mode choice is the single biggest performance lever in a Code node. Each per-item execution context costs a setup tax (measured on n8n 2.x, small records):

What runs per itemApprox. cost
Code All Items (one run for the whole set)~0.02 ms/item
Expression in any node (IF / Set / etc.)~0.2 ms/item
Code Each Item (a full sandbox per item)~0.6 ms/item — ~25–30× All Items

So Run Once for Each Item over 10k items is ~6 s of pure overhead vs ~0.2 s in Run Once for All Items. Use Each Item only when an item genuinely needs isolating (independent error handling, or a per-item API call you can't batch); otherwise loop inside one All Items node. Expression complexity itself is essentially free (~90% of the cost is the per-item context, not your code) and every node→node hop re-copies all items — so reduce the number of per-item boundaries, don't micro-optimize each one. Below a few hundred items none of this matters; reach for it on the hot path (large item counts, little I/O).

See: DATA_ACCESS.md → "Mode Performance" for the corollaries, hop costs, and scale check.

---

Data Access Patterns

Four ways to pull data from upstream nodes. Note $node["Name"] and $('Name') need .first().json or .all() — never .json directly.

const allItems = $input.all();          // 1. All items — batch ops, aggregation (most common)
const data = $input.first().json;       // 2. First item — single objects, API responses
const item = $input.item;               // 3. Current item — "Each Item" mode ONLY (undefined otherwise)
const other = $node["Webhook"].json;    // 4. Named node — combine data across nodes

Always access fields via .json (e.g. item.json.name, not item.name), and prefer the explicit $input.first().json.field over a bare $json.field.

See: DATA_ACCESS.md for the full guide — every pattern with examples, a decision tree, and the common mistakes (mutating originals, missing length checks, $input.item in the wrong mode).

---

Critical: Webhook Data Structure

MOST COMMON MISTAKE: Webhook data is nested under .body

// ❌ WRONG - Will return undefined
const name = $json.name;
const email = $json.email;

// ✅ CORRECT - Webhook data is under .body
const name = $json.body.name;
const email = $json.body.email;

// Or with $input
const webhookData = $input.first().json.body;
const name = webhookData.name;

Why: Webhook node wraps all request data under body property. This includes POST data, query parameters, and JSON payloads.

See: DATA_ACCESS.md for full webhook structure details

---

Return Format Requirements

CRITICAL RULE: Always return array of objects with json property

Correct Return Formats

// ✅ Single result
return [{
  json: {
    field1: value1,
    field2: value2
  }
}];

// ✅ Multiple results
return [
  {json: {id: 1, data: 'first'}},
  {json: {id: 2, data: 'second'}}
];

// ✅ Transformed array
const transformed = $input.all()
  .filter(item => item.json.valid)
  .map(item => ({
    json: {
      id: item.json.id,
      processed: true
    }
  }));
return transformed;

// ✅ Empty result (when no data to return)
return [];

// ✅ Conditional return
if (shouldProcess) {
  return [{json: processedData}];
} else {
  return [];
}

Incorrect Return Formats

// ❌ WRONG: Object without array wrapper
return {
  json: {field: value}
};

// ❌ WRONG: Array without json wrapper
return [{field: value}];

// ❌ WRONG: Plain string
return "processed";

// ❌ WRONG: Raw data without mapping
return $input.all();  // Missing .map()

// ❌ WRONG: Incomplete structure
return [{data: value}];  // Should be {json: value}

Why it matters: Next nodes expect array format. Incorrect format causes workflow execution to fail.

See: ERROR_PATTERNS.md #3 for detailed error solutions

---

Common Patterns Overview

The most useful Code node shapes from production workflows. One quick example — sum/aggregate across all items:

const items = $input.all();
const total = items.reduce((sum, item) => sum + (item.json.amount || 0), 0);
return [{ json: { total, count: items.length, average: total / items.length } }];

The full library covers 10 patterns: multi-source aggregation, regex filtering, markdown/structured-text parsing, JSON comparison, CRM/form transformation, release processing, array transformation with computed fields, Slack Block Kit formatting, top-N ranking, and string-aggregation reporting — each with variations.

See: COMMON_PATTERNS.md for the 10 detailed production patterns (and the Best Practices section: validate input, try-catch, filter-early, array methods over loops, console.log debugging).

---

Error Prevention - Top Mistakes

The recurring Code node failures, in rough frequency order:

1. Empty code / missing return — always end with return [...], and make sure every branch returns. 2. Expression syntax in code — no {{ }}. Use JavaScript: ` ${$json.field} or $input.first().json.field. 3. **Wrong return wrapper** — return {json:{...}} fails; must be return [{json:{...}}]. 4. **Missing null checks** — use optional chaining: item.json?.user?.email || 'fallback'. 5. **Webhook body nesting** — $json.email is undefined; use $json.body.email. 6. **Auth helpers blocked** (httpRequestWithAuthentication) and $env` blocked — route secrets through credentials/HTTP Request node, not the Code node sandbox.

See: ERROR_PATTERNS.md for the comprehensive guide — each error with wrong/right code, escaping rules, the sandbox restrictions (Errors #6–#7), a prevention checklist, and a quick error-message lookup table.

---

Built-in Functions & Helpers

// HTTP requests (no auth — see sandbox note below)
const res = await this.helpers.httpRequest({ method: 'GET', url: 'https://api.example.com/data' });

// DateTime (Luxon): now, formatting, arithmetic
const now = DateTime.now();
const formatted = now.toFormat('yyyy-MM-dd');
const tomorrow = now.plus({ days: 1 });

// $jmespath() — query JSON structures
const adults = $jmespath($input.first().json, 'users[?age >= `18`]');

// $getWorkflowStaticData() — data that persists across executions

Sandbox (since n8n v2.0, JsTaskRunnerSandbox): the accessor is this.helpers.httpRequest() — the bare $helpers global is undefined here ($helpers.httpRequest() throws ReferenceError). Inside a nested async function where this is lost, call it as await fn.call(this, ...). this.helpers.httpRequestWithAuthentication and this.helpers.requestWithAuthenticationPaginated are deny-listed (→ UnsupportedFunctionError); for authenticated calls use an HTTP Request node with the credential (preferred), a sub-workflow, or a manual Authorization: Bearer ${token} header on this.helpers.httpRequest() only when the token already flows through the workflow as data. $env is blocked when N8N_BLOCK_ENV_ACCESS_IN_NODE=true; require() works only for allowlisted modules. Buffer, URL, and standard JS globals (Math, JSON, Object, Array) always work.

See: BUILTIN_FUNCTIONS.md for the complete reference — full httpRequest options, all DateTime/Luxon operations, JMESPath patterns, static-data use cases, and the sandbox-restriction details.

---

Best Practices

  • Validate input first — guard for empty arrays / missing .json before processing.
  • Try-catch risky work (HTTP calls) and return an error object instead of crashing.
  • Prefer array methods (filter/map/reduce) over manual loops.
  • Filter early, transform late — shrink the dataset before expensive work.
  • Descriptive names and console.log() for debugging (output goes to the browser console).

See: COMMON_PATTERNS.md → "Best Practices" for code examples of each.

---

Production Gotchas

Hard-won lessons from real deployments — summarized here, with code in DATA_ACCESS.md → "Production Gotchas":

  • SplitInBatches outputs are counterintuitive: main[0] = done (fires once, after all batches), main[1] = each batch (the loop body). Add a Limit 1 node after the done output as a safety.
  • Iteration count is the cost: each loop iteration re-runs the whole body through the engine (~0.8 ms overhead each). batchSize: 1 is the loop equivalent of Each Item — use the largest batch your real constraint (rate limit, page size, memory) allows, or don't loop at all.
  • Cross-iteration accumulation (CRITICAL): after the loop, $('Node Inside Loop').all() returns ONLY the last iteration's items. Accumulate via $getWorkflowStaticData('global') (reset before, push inside, read after).
  • pairedItem: when emitting items that don't map 1:1 to input, set pairedItem: { item: i } or downstream Set nodes fail with paired_item_no_info.
  • Node reference syntax: $('Node').first().json or $('Node').all() — never .json directly on the reference.
  • Float precision: compare currency at the cent level — Math.round(a*100) !== Math.round(b*100) — to avoid false positives from float noise.

---

When to Use Code Node

Before reaching for a Code node, walk the transform gatekeeper in the n8n Expression Syntax skill: expression → arrow-function IIFE inside an Edit Fields field → Code node, in that order. The first two paths cover most "transform this data" tasks at ~1–10ms each, versus the Code node's sandboxed ~500–1000ms — a ~100x gap on pure single-item shaping, with no functional difference. The Code node earns its place only for whole-dataset aggregation ($input.all()), allowlisted libraries, or async work. And before writing code for crypto (HMAC, hashing, signing) or XML/SOAP/RSS parsing, check for a native node — n8n has a Crypto node (nodes-base.crypto) and an XML node (nodes-base.xml) that cover those without any JavaScript. Dropping into Code for something a native node already does is one of the most common false positives.

Use Code node when:

  • ✅ Complex transformations requiring multiple steps
  • ✅ Custom calculations or business logic
  • ✅ Recursive operations
  • ✅ API response parsing with complex structure
  • ✅ Multi-step conditionals
  • ✅ Data aggregation across items

Consider other nodes when:

  • ❌ Simple field mapping → Use Set node
  • ❌ Basic filtering → Use Filter node
  • ❌ Simple conditionals → Use IF or Switch node
  • ❌ HTTP requests only → Use HTTP Request node

Code node excels at: Complex logic that would require chaining many simple nodes

---

Integration with Other Skills

Works With:

n8n Expression Syntax:

  • Expressions use {{ }} syntax in other nodes
  • Code nodes use JavaScript directly (no {{ }})
  • When to use expressions vs code

n8n MCP Tools Expert:

  • How to find Code node: search_nodes({query: "code"})
  • Get configuration help: get_node({nodeType: "nodes-base.code"})
  • Validate code: validate_node({nodeType: "nodes-base.code", config: {...}})

n8n Node Configuration:

  • Mode selection (All Items vs Each Item)
  • Language selection (JavaScript vs Python)
  • Understanding property dependencies

n8n Workflow Patterns:

  • Code nodes in transformation step
  • Webhook → Code → API pattern
  • Error handling in workflows

n8n Validation Expert:

  • Validate Code node configuration
  • Handle validation errors
  • Auto-fix common issues

---

Quick Reference Checklist

Before deploying Code nodes, verify:

  • [ ] Code is not empty - Must have meaningful logic
  • [ ] Return statement exists - Must return array of objects
  • [ ] Proper return format - Each item: {json: {...}}
  • [ ] Data access correct - Using $input.all(), $input.first(), or $input.item
  • [ ] No n8n expressions - Use JavaScript template literals: ` ${value} `
  • [ ] Error handling - Guard clauses for null/undefined inputs
  • [ ] Webhook data - Access via .body if from webhook
  • [ ] Mode selection - "All Items" for most cases
  • [ ] Performance - Prefer map/filter over manual loops
  • [ ] Output consistent - All code paths return same structure

---

Additional Resources

Related Files

  • DATA_ACCESS.md - Comprehensive data access patterns
  • COMMON_PATTERNS.md - 10 production-tested patterns
  • ERROR_PATTERNS.md - Top 5 errors and solutions
  • BUILTIN_FUNCTIONS.md - Complete built-in reference

n8n Documentation

  • Code Node Guide: https://docs.n8n.io/code/code-node/
  • Built-in Methods: https://docs.n8n.io/code-examples/methods-variables-reference/
  • Luxon Documentation: https://moment.github.io/luxon/

---

Ready to write JavaScript in n8n Code nodes! Start with simple transformations, use the error patterns guide to avoid common mistakes, and reference the pattern library for production-ready examples.

Related skills

FAQ

Which Code node execution mode should I use by default?

Run Once for All Items handles about ninety-five percent of cases including aggregation and cross-item logic; use Each Item only when each row needs fully isolated processing.

Why does $json.email return undefined from a Webhook node?

Webhook nodes wrap request data under body; read $json.body.email or $input.first().json.body.email instead of top-level fields.

Can I call $helpers.httpRequest inside a Code node?

No. The bare $helpers global is undefined in the task-runner sandbox; use this.helpers.httpRequest() for unauthenticated requests.

Is N8n Code Javascript safe to install?

skills.sh reports 2 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.

Automation & Workflowsintegrationsbackend

This week in AI coding

Five minutes, every Monday - the tools, releases and tactics for developers.

unsubscribe anytime.