All skills
czlonkowski avatar

/n8n-code-javascript

@b90ba12

Write JavaScript code in n8n Code nodes. Use when writing JavaScript in n8n, using $input/$json/$node syntax, making HTTP requests with this.helpers / the $helpers global, working with dates using DateTime, troubleshooting Code node errors, choosing between Code node modes, or doing any custom data transformation in n8n. 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. Covers SplitInBatches loop patterns, cross-iteration data, pairedItem, and real-world production patterns. Also use when asked why a Code node or workflow is slow, which execution mode is faster, or how to cut per-item overhead on large datasets. EXCEPTION — for the AI-agent-callable Custom Code Tool (@n8n/n8n-nodes-langchain.toolCode, a tool attached to an AI Agent), use the n8n-code-tool skill instead; it has a different runtime contract.

Use this Skill: https://skilld.dev/gh/czlonkowski/n8n-skills/n8n-code-javascript

This session only. Nothing lands on disk.

README.md

≈2.5k tokens on demand. Your agent reads this file only when SKILL.md points to it.

n8n Code JavaScript

Expert guidance for writing JavaScript code in n8n Code nodes.


Purpose

Teaches how to write effective JavaScript in n8n Code nodes, avoid common errors, and use built-in functions effectively.


Activates On

Trigger keywords:

  • "javascript code node"
  • "write javascript in n8n"
  • "code node javascript"
  • "$input syntax"
  • "$json syntax"
  • "this.helpers.httpRequest" / "$helpers.httpRequest"
  • "DateTime luxon"
  • "code node error"
  • "webhook data code"
  • "return format code node"

Common scenarios:

  • Writing JavaScript code in Code nodes
  • Troubleshooting Code node errors
  • Making HTTP requests from code
  • Working with dates and times
  • Accessing webhook data
  • Choosing between All Items and Each Item mode

What You'll Learn

Quick Start

  • Mode selection (All Items vs Each Item)
  • Data access patterns ($input.all(), $input.first(), $input.item)
  • Correct return format: [{json: {...}}]
  • Webhook data structure (.body nesting)
  • Built-in functions overview

Data Access Mastery

  • $input.all() - Batch operations (most common)
  • $input.first() - Single item operations
  • $input.item - Each Item mode processing
  • $node - Reference other workflow nodes
  • Critical gotcha: Webhook data under .body

Common Patterns (Production-Tested)

  1. Multi-source Data Aggregation
  2. Regex Filtering & Pattern Matching
  3. Markdown Parsing & Structured Extraction
  4. JSON Comparison & Validation
  5. CRM Data Transformation
  6. Release Information Processing
  7. Array Transformation with Context
  8. Slack Block Kit Formatting
  9. Top N Filtering & Ranking
  10. String Aggregation & Reporting

Error Prevention

Top 5 errors to avoid:

  1. Empty code / missing return (38% of failures)
  2. Expression syntax confusion (using {{}} in code)
  3. Incorrect return format (missing array wrapper or json property)
  4. Unmatched brackets (string escaping issues)
  5. Missing null checks (crashes on undefined)

Built-in Functions

  • this.helpers.httpRequest() - Make HTTP requests (the bare $helpers global is undefined in the task-runner sandbox; prefer the HTTP Request node for anything beyond a trivial unauthenticated GET)
  • DateTime (Luxon) - Advanced date/time operations
  • $jmespath() - Query JSON structures
  • $getWorkflowStaticData() - Persistent storage
  • Standard JavaScript globals (Math, JSON, console)
  • Available Node.js modules (crypto, Buffer, URL)

File Structure

n8n-code-javascript/
├── SKILL.md
│   Overview, quick start, mode selection, best practices
│   - Mode selection guide (All Items vs Each Item)
│   - Data access patterns overview
│   - Return format requirements
│   - Critical webhook gotcha
│   - Error prevention overview
│   - Quick reference checklist
│
├── DATA_ACCESS.md
│   Complete data access patterns
│   - $input.all() - Most common (26% usage)
│   - $input.first() - Very common (25% usage)
│   - $input.item - Each Item mode (19% usage)
│   - $node - Reference other nodes
│   - Webhook data structure (.body nesting)
│   - Choosing the right pattern
│   - Common mistakes to avoid
│
├── COMMON_PATTERNS.md
│   10 production-tested patterns
│   - Pattern 1: Multi-source Aggregation
│   - Pattern 2: Regex Filtering
│   - Pattern 3: Markdown Parsing
│   - Pattern 4: JSON Comparison
│   - Pattern 5: CRM Transformation
│   - Pattern 6: Release Processing
│   - Pattern 7: Array Transformation
│   - Pattern 8: Slack Block Kit
│   - Pattern 9: Top N Filtering
│   - Pattern 10: String Aggregation
│   - Pattern selection guide
│
├── ERROR_PATTERNS.md
│   Top 5 errors with solutions
│   - Error #1: Empty Code / Missing Return (38%)
│   - Error #2: Expression Syntax Confusion (8%)
│   - Error #3: Incorrect Return Wrapper (5%)
│   - Error #4: Unmatched Brackets (6%)
│   - Error #5: Missing Null Checks
│   - Error prevention checklist
│   - Quick error reference
│   - Debugging tips
│
├── BUILTIN_FUNCTIONS.md
│   Complete built-in function reference
│   - this.helpers.httpRequest() API reference
│   - DateTime (Luxon) complete guide
│   - $jmespath() JSON querying
│   - $getWorkflowStaticData() persistent storage
│   - Standard JavaScript globals
│   - Available Node.js modules
│   - What's NOT available
│
└── README.md (this file)
    Skill metadata and overview

Total: 6 files


Coverage

Mode Selection

  • Run Once for All Items - Recommended for 95% of use cases
  • Run Once for Each Item - Specialized cases only
  • Decision guide and performance implications

Data Access

  • Most common patterns with usage statistics
  • Webhook data structure (critical .body gotcha)
  • Safe access patterns with null checks
  • When to use which pattern

Error Prevention

  • Top 5 errors covering 62%+ of all failures
  • Clear wrong vs right examples
  • Error prevention checklist
  • Debugging tips and console.log usage

Production Patterns

  • 10 patterns from real workflows
  • Complete working examples
  • Use cases and key techniques
  • Pattern selection guide

Built-in Functions

  • Complete this.helpers.httpRequest() reference
  • DateTime/Luxon operations (formatting, parsing, arithmetic)
  • $jmespath() for JSON queries
  • Persistent storage with $getWorkflowStaticData()
  • Standard JavaScript and Node.js modules

Critical Gotchas Highlighted

#1: Webhook Data Structure

MOST COMMON MISTAKE: Webhook data is under .body

// ❌ WRONG
const name = $json.name;

// ✅ CORRECT
const name = $json.body.name;

#2: Return Format

Prefer the canonical [{json: {...}}] — unambiguous in both execution modes. A bare object auto-wraps in Run Once for All Items mode, so it runs too; what actually fails is returning a primitive (string/number) or null.

// ⚠️ Auto-wrapped in All Items mode → [{json: {result: 'success'}}]. Runs, but prefer the array form.
return {json: {result: 'success'}};

// ✅ CANONICAL
return [{json: {result: 'success'}}];

#3: Expression Syntax

Don't use {{}} in Code nodes

// ❌ WRONG
const value = "{{ $json.field }}";

// ✅ CORRECT
const value = $json.field;

Integration with Other Skills

n8n Expression Syntax

  • Distinction: Expressions use {{}} in OTHER nodes
  • Code nodes: Use JavaScript directly (no {{}})
  • When to use each: Code vs expressions decision guide

n8n MCP Tools Expert

  • Find Code node: search_nodes({query: "code"})
  • Get configuration: get_node("nodes-base.code")
  • Validate code: validate_node()

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

When to Use Code Node

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


Success Metrics

Before this skill:

  • Users confused by mode selection
  • Frequent return format errors
  • Expression syntax mistakes
  • Webhook data access failures
  • Missing null check crashes

After this skill:

  • Clear mode selection guidance
  • Understanding of return format
  • JavaScript vs expression distinction
  • Correct webhook data access
  • Safe null-handling patterns
  • Production-ready code patterns

Quick Reference

Essential Rules

  1. Choose "All Items" mode (recommended)
  2. Access data: $input.all(), $input.first(), $input.item
  3. Return the canonical [{json: {...}}] (bare objects auto-wrap in All Items mode; primitives/null fail)
  4. Webhook data: Under .body property
  5. No {{}} syntax: Use JavaScript directly

Most Common Patterns

  • Batch processing → $input.all() + map/filter
  • Single item → $input.first()
  • Aggregation → reduce()
  • HTTP requests → this.helpers.httpRequest()
  • Date handling → DateTime (Luxon)

Error Prevention

  • Always return data
  • Check for null/undefined
  • Use try-catch for risky operations
  • Test with empty input
  • Use console.log() for debugging

Related Documentation


Evaluations

5 test scenarios covering:

  1. Webhook body gotcha (most common mistake)
  2. Return format error (missing array wrapper)
  3. HTTP request with this.helpers.httpRequest()
  4. Aggregation pattern with $input.all()
  5. Expression syntax confusion (using {{}})

Each evaluation tests skill activation, correct guidance, and reference to appropriate documentation files.


Version History

  • v1.0 (2025-01-20): Initial implementation
    • SKILL.md with comprehensive overview
    • DATA_ACCESS.md covering all access patterns
    • COMMON_PATTERNS.md with 10 production patterns
    • ERROR_PATTERNS.md covering top 5 errors
    • BUILTIN_FUNCTIONS.md complete reference
    • 5 evaluation scenarios

Author

Conceived by Romuald Członkowski - aiadvisors.pl/en

Part of the n8n-skills collection.

Source: SKILL.md on GitHub

No alerts15d5 checks · Risk SAFE
  • Gen Agent Trust Hub15d

    The skill provides comprehensive and secure guidance for writing JavaScript in n8n Code nodes. It includes high-quality technical advice on avoiding common security pitfalls, such as hardcoding secrets or bypassing authentication controls, and emphasizes the use of platform-native security features like the credential system and task runner sandbox.

  • Socket15d

    No alerts

  • Snyk15d

    Risk: LOW · No issues

  • Runlayer7mo

    1/6 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at b90ba12. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 weeks ago.

Activeupdated 3 months ago
  • n8n
  • javascript
  • code-node
  • workflow
  • data-transformation
  • aggregation
  • api-integration
  • batch-processing

README badge

README badge for czlonkowski/n8n-skills/n8n-code-javascript

Writes JavaScript code for n8n Code nodes, covering data access patterns ($input, $json, $node), the critical return format requirement, built-in helpers like $helpers.httpRequest() and DateTime, and common production patterns like aggregation, filtering, and batch processing. Includes mode selection (Run Once for All Items vs. Each Item) and top error patterns specific to n8n's Code node runtime.

Generated from the current SKILL.md.

What's the difference between 'Run Once for All Items' and 'Run Once for Each Item' mode?
'Run Once for All Items' executes the code once and accesses all input data via $input.all(); use this for 95% of cases including aggregations and batch processing. 'Run Once for Each Item' executes separately for each input item via $input.item; use only when items must be processed independently.
Why does my webhook data return undefined?
Webhook data is nested under the .body property. Access it via $json.body.fieldName or $input.first().json.body, not directly from $json.
What format must I return from a Code node?
Always return an array of objects with a json property: [{json: {field: value}}]. Returning a plain object, string, or data without the json wrapper will cause execution to fail.
Can I use require() to import external modules like axios or lodash?
Only if your n8n instance has allowlisted them via N8N_RUNNERS_ALLOWED_EXTERNAL_MODULES. Built-in functions like $helpers.httpRequest(), DateTime (Luxon), and $jmespath() are always available without require().
Is this skill for the Custom Code Tool used with AI Agents?
No. This skill covers standard Code nodes in workflows. For the Custom Code Tool (@n8n/n8n-nodes-langchain.toolCode) attached to AI Agents, use the n8n-code-tool skill instead—it has a different runtime contract.

Generated from the current SKILL.md. These answers refresh after source changes.