askill
n8n-code-javascript

n8n-code-javascriptSafety 95Repository

Write JavaScript code in n8n Code nodes. Use when writing custom JavaScript, accessing data in Code nodes, transforming data programmatically, handling multiple items, or debugging Code node errors.

0 stars
1.2k downloads
Updated 1/27/2026

Package Files

Loading files...
SKILL.md

n8n Code JavaScript

Expert guidance for writing JavaScript in n8n Code nodes.

Based on n8n-skills by Romuald Członkowski

Code Node Modes

Run Once for All Items

Process all items together. Most common mode.

// Access all input items
const items = $input.all();

// Process and return
return items.map(item => ({
  json: {
    ...item.json,
    processed: true
  }
}));

Run Once for Each Item

Process each item separately.

// Access current item
const data = $input.item.json;

// Return single item
return {
  json: {
    ...data,
    processed: true
  }
};

Accessing Data

Current Node Input

// All items
const items = $input.all();

// First item
const first = $input.first();

// Current item (in "each item" mode)
const current = $input.item;

// JSON data
const data = $input.first().json;

Previous Nodes

// From specific node
const nodeData = $node["Node Name"].all();
const firstItem = $node["HTTP Request"].first().json;

// From input (same as previous node)
const inputItems = $input.all();

Execution Context

// Workflow info
const workflowId = $workflow.id;
const workflowName = $workflow.name;

// Execution info
const executionId = $execution.id;

// Environment variables
const apiKey = $env.API_KEY;

// n8n variables
const myVar = $vars.myVariable;

Common Patterns

Transform All Items

const items = $input.all();

return items.map(item => ({
  json: {
    id: item.json.id,
    name: item.json.name.toUpperCase(),
    createdAt: new Date().toISOString()
  }
}));

Filter Items

const items = $input.all();

return items.filter(item => item.json.status === 'active');

Aggregate Data

const items = $input.all();

const total = items.reduce((sum, item) => sum + item.json.amount, 0);
const count = items.length;

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

Group By

const items = $input.all();

const grouped = {};
for (const item of items) {
  const key = item.json.category;
  if (!grouped[key]) grouped[key] = [];
  grouped[key].push(item.json);
}

return Object.entries(grouped).map(([category, items]) => ({
  json: { category, items, count: items.length }
}));

Fetch External Data

const response = await fetch('https://api.example.com/data', {
  headers: { 'Authorization': `Bearer ${$env.API_TOKEN}` }
});

const data = await response.json();

return [{ json: data }];

Error Handling

const items = $input.all();

return items.map(item => {
  try {
    const parsed = JSON.parse(item.json.rawData);
    return { json: { success: true, data: parsed } };
  } catch (error) {
    return { json: { success: false, error: error.message } };
  }
});

Webhook Data Access

CRITICAL: In Code nodes, webhook data is under .body:

// ❌ Wrong
const message = $input.first().json.message;

// ✅ Correct - webhook data is in body
const message = $input.first().json.body.message;
const headers = $input.first().json.headers;
const query = $input.first().json.query;

Date/Time with Luxon

n8n includes Luxon for dates:

const { DateTime } = require('luxon');

// Current time
const now = DateTime.now();

// Parse date
const date = DateTime.fromISO('2024-01-15');

// Format
const formatted = now.toFormat('yyyy-MM-dd HH:mm:ss');

// Add/subtract
const nextWeek = now.plus({ days: 7 });
const yesterday = now.minus({ days: 1 });

// Compare
const isAfter = now > date;
const diff = now.diff(date, 'days').days;

Binary Data

Access Binary

const items = $input.all();

for (const item of items) {
  if (item.binary?.data) {
    const buffer = await item.binary.data.toBuffer();
    const base64 = buffer.toString('base64');
    // Process binary...
  }
}

Create Binary

const data = { key: 'value' };
const jsonString = JSON.stringify(data, null, 2);
const buffer = Buffer.from(jsonString);

return [{
  json: { fileName: 'data.json' },
  binary: {
    data: await this.helpers.prepareBinaryData(buffer, 'data.json', 'application/json')
  }
}];

Common Mistakes

1. Wrong Return Format

// ❌ Wrong - must return array of objects with json property
return { name: 'John' };

// ✅ Correct
return [{ json: { name: 'John' } }];

2. Not Returning All Items

// ❌ Wrong - returns only first item
const item = $input.first();
return [{ json: item.json }];

// ✅ Correct - process all items
return $input.all().map(item => ({ json: item.json }));

3. Accessing Wrong Data Level

// ❌ Wrong - items don't have .data
const data = items[0].data;

// ✅ Correct - use .json
const data = items[0].json;

4. Missing await for Async

// ❌ Wrong - fetch is async
const response = fetch(url);

// ✅ Correct
const response = await fetch(url);
const data = await response.json();

Available Libraries

Built-in libraries available in Code node:

  • luxon - Date/time handling
  • lodash - Utility functions
  • crypto - Cryptographic functions
const { DateTime } = require('luxon');
const _ = require('lodash');
const crypto = require('crypto');

// Use lodash
const grouped = _.groupBy(items, 'category');

// Use crypto
const hash = crypto.createHash('sha256').update(data).digest('hex');

Best Practices

  1. Always return array of { json: {...} } objects
  2. Use try/catch for error-prone operations
  3. Access webhook data via .body
  4. Use Luxon for dates, not raw Date()
  5. Validate input data before processing
  6. Log sparingly - console.log works but adds overhead

Quick Reference

NeedCode
All items$input.all()
First item$input.first()
Current item$input.item (each mode)
Item JSONitem.json
Webhook bodyitem.json.body
Other node$node["Name"].first().json
Env var$env.VAR_NAME
Return item{ json: {...} }
Return many[{ json: {...} }, ...]

Based on n8n-skills by Romuald Członkowski • Adapted for Moltbot by fazer.ai

Install

Download ZIP
Requires askill CLI v1.0+

AI Quality Score

88/100Analyzed 2/19/2026

High-quality technical reference skill for writing JavaScript in n8n Code nodes. Comprehensive coverage of modes, data access, transformations, common patterns, and gotchas. Excellent code examples with correct/incorrect comparisons and a quick reference table. Highly actionable and reusable for any n8n workflow. Slight deduction for some advanced topics not covered (credentials handling, detailed performance tips). Overall excellent skill with clear structure and practical value."

95
90
85
78
92

Metadata

Licenseunknown
Version-
Updated1/27/2026
Publisherfazer-ai

Tags

apigithubgithub-actions