Skill

Generate and Debug JavaScript for n8n

Expert guidance for writing JavaScript in n8n Code nodes - mode selection, data access, return format, and common mistakes.

Works with n8n

87
Spark score
out of 100
Updated 10 days ago
Version 15.1.0

Add to Favorites

Why it matters

Master n8n's JavaScript Code nodes with expert guidance. Learn to efficiently process data, integrate with other nodes, and avoid common pitfalls for robust workflow automation.

Outcomes

What it gets done

01

Write and optimize JavaScript code for n8n Code nodes.

02

Understand data access patterns like $input.all() and $input.item.

03

Correctly format return values for seamless workflow integration.

04

Debug common issues, especially with webhook data handling.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/ag-n8n-code-javascript | bash

Overview

JavaScript Code Node

A skill providing expert guidance for n8n JavaScript Code nodes - execution mode selection, data access patterns, mandatory array return format, and the most common execution-failure causes. Use it when writing custom JavaScript logic in a Code node - simpler tasks like field mapping or basic filtering are better served by n8n's dedicated Set, Filter, or IF nodes.

What it does

This skill provides expert guidance for writing JavaScript in n8n Code nodes. Essential rules up front: choose "Run Once for All Items" mode for most use cases; access data via $input.all(), $input.first(), or $input.item; always return the [{json: {...}}] array-of-objects format; remember webhook data lives under $json.body, not $json directly; and built-ins like $helpers.httpRequest(), Luxon's DateTime, and $jmespath() are available inside the node.

// 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;

The Code node offers two execution modes: "Run Once for All Items" (the default, recommended for roughly 95% of cases - code runs once regardless of input count, best for aggregation, filtering, batch transforms, totals, sorting, and deduplication) and "Run Once for Each Item" (code runs separately per item via $input.item, best for independent per-item API calls or validation with different error handling - slower on large datasets). The decision shortcut: use All Items unless each item is genuinely independent, and default to All Items when unsure since you can always loop inside it. Four data-access patterns cover most cases: $input.all() for batch/aggregation work, $input.first() for single-object/API-response handling, $input.item inside Each Item mode only, and $node["NodeName"].json to pull output from a specific named node in the workflow.

The most common mistake is accessing webhook data directly as $json.name instead of $json.body.name - the Webhook node wraps all request data (POST body, query params, JSON payload) under a body property. The other hard requirement is return format: every code path must return an array of {json: {...}} objects - a bare object, an array without the json wrapper, a plain string, or raw unmapped $input.all() output will all fail downstream nodes expecting the array format. Five common production patterns cover multi-source data aggregation, regex-based text filtering, field mapping/enrichment, top-N filtering/ranking, and sum/count aggregation reporting. The top five mistakes, beyond the two already named, are: an empty code block with no return statement; using n8n's {{ }} expression syntax inside JavaScript instead of template literals; returning an object instead of an array; and missing null checks on nested fields, fixed with optional chaining or an early guard-clause return.

Best practices include validating input exists and has the expected shape before processing, wrapping HTTP calls in try/catch rather than letting them throw uncaught, preferring array methods (filter/map) over manual loops, filtering early before expensive transformations rather than after, using descriptive variable names over single letters, and debugging with console.log(). Use the Code node for complex multi-step transformations, custom business logic, recursive operations, or multi-step conditionals - simpler cases are better served by the Set node (field mapping), Filter node (basic filtering), IF/Switch node (simple conditionals), or HTTP Request node (requests alone).

When to use - and when NOT to

Use it when writing or debugging JavaScript inside an n8n Code node - mode selection, data access, return format, or error prevention. Reach for a simpler dedicated node instead when the task is just field mapping, basic filtering, a simple conditional, or a bare HTTP request, since Code node logic only earns its complexity when it replaces chaining many simple nodes.

Inputs and outputs

Input is upstream node data accessed via $input/$node, plus any external API calls made through $helpers.httpRequest(). Output must always be an array of {json: {...}} objects, validated against a pre-deployment checklist covering non-empty code, correct return format, webhook .body access, guard clauses for null input, and consistent output structure across all code paths.

Integrations

It works alongside n8n's expression syntax (used in other nodes' {{ }} fields, never inside Code nodes), n8n MCP tools for finding and validating the Code node's configuration, and companion skills for node configuration, workflow patterns, and validation.

Who it's for

n8n workflow builders writing custom JavaScript logic in Code nodes who need correct mode selection, data access patterns, and return formatting to avoid the most common causes of workflow execution failure.

FAQ

Common questions

Discussion

Questions & comments ยท 0

Sign In Sign in to leave a comment.