N8N Code Javascript
Write powerful JavaScript for n8n workflows with confidence
✨ The solution you've been looking for
Write JavaScript code in n8n Code nodes. Use when writing JavaScript in n8n, using $input/$json/$node syntax, making HTTP requests with $helpers, working with dates using DateTime, troubleshooting Code node errors, or choosing between Code node modes.
See It In Action
Interactive preview & real-world examples
AI Conversation Simulator
See how users interact with this skill
User Prompt
I need to transform webhook data from a form submission and send it to a CRM API. The webhook sends nested data but the CRM expects a flat structure.
Skill Processing
Analyzing request...
Agent Response
JavaScript code that safely extracts webhook data from $json.body, flattens the structure, validates required fields, and returns properly formatted data for the next workflow step
Quick Start (3 Steps)
Get up and running in minutes
Install
claude-code skill install n8n-code-javascript
claude-code skill install n8n-code-javascriptConfig
First Trigger
@n8n-code-javascript helpCommands
| Command | Description | Required Args |
|---|---|---|
| @n8n-code-javascript transform-webhook-data-for-api-consumption | Process incoming webhook data and format it for external API calls with proper error handling | None |
| @n8n-code-javascript aggregate-and-filter-large-datasets | Process multiple data sources, filter relevant items, and calculate business metrics | None |
| @n8n-code-javascript handle-complex-api-response-parsing | Extract and transform data from complex nested API responses with conditional logic | None |
Typical Use Cases
Transform webhook data for API consumption
Process incoming webhook data and format it for external API calls with proper error handling
Aggregate and filter large datasets
Process multiple data sources, filter relevant items, and calculate business metrics
Handle complex API response parsing
Extract and transform data from complex nested API responses with conditional logic
Overview
JavaScript Code Node
Expert guidance for writing JavaScript code in n8n Code nodes.
Quick Start
1// Basic template for Code nodes
2const items = $input.all();
3
4// Process data
5const processed = items.map(item => ({
6 json: {
7 ...item.json,
8 processed: true,
9 timestamp: new Date().toISOString()
10 }
11}));
12
13return processed;
Essential Rules
- Choose “Run Once for All Items” mode (recommended for most use cases)
- Access data:
$input.all(),$input.first(), or$input.item - CRITICAL: Must return
[{json: {...}}]format - CRITICAL: Webhook data is under
$json.body(not$jsondirectly) - Built-ins available: $helpers.httpRequest(), DateTime (Luxon), $jmespath()
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()oritemsarray - Best for: Aggregation, filtering, batch processing, transformations, API calls with all data
- Performance: Faster for multiple items (single execution)
1// Example: Calculate total from all items
2const allItems = $input.all();
3const total = allItems.reduce((sum, item) => sum + (item.json.amount || 0), 0);
4
5return [{
6 json: {
7 total,
8 count: allItems.length,
9 average: total / allItems.length
10 }
11}];
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.itemor$item - Best for: Item-specific logic, independent operations, per-item validation
- Performance: Slower for large datasets (multiple executions)
1// Example: Add processing timestamp to each item
2const item = $input.item;
3
4return [{
5 json: {
6 ...item.json,
7 processed: true,
8 processedAt: new Date().toISOString()
9 }
10}];
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)
Data Access Patterns
Pattern 1: $input.all() - Most Common
Use when: Processing arrays, batch operations, aggregations
1// Get all items from previous node
2const allItems = $input.all();
3
4// Filter, map, reduce as needed
5const valid = allItems.filter(item => item.json.status === 'active');
6const mapped = valid.map(item => ({
7 json: {
8 id: item.json.id,
9 name: item.json.name
10 }
11}));
12
13return mapped;
Pattern 2: $input.first() - Very Common
Use when: Working with single objects, API responses, first-in-first-out
1// Get first item only
2const firstItem = $input.first();
3const data = firstItem.json;
4
5return [{
6 json: {
7 result: processData(data),
8 processedAt: new Date().toISOString()
9 }
10}];
Pattern 3: $input.item - Each Item Mode Only
Use when: In “Run Once for Each Item” mode
1// Current item in loop (Each Item mode only)
2const currentItem = $input.item;
3
4return [{
5 json: {
6 ...currentItem.json,
7 itemProcessed: true
8 }
9}];
Pattern 4: $node - Reference Other Nodes
Use when: Need data from specific nodes in workflow
1// Get output from specific node
2const webhookData = $node["Webhook"].json;
3const httpData = $node["HTTP Request"].json;
4
5return [{
6 json: {
7 combined: {
8 webhook: webhookData,
9 api: httpData
10 }
11 }
12}];
See: DATA_ACCESS.md for comprehensive guide
Critical: Webhook Data Structure
MOST COMMON MISTAKE: Webhook data is nested under .body
1// ❌ WRONG - Will return undefined
2const name = $json.name;
3const email = $json.email;
4
5// ✅ CORRECT - Webhook data is under .body
6const name = $json.body.name;
7const email = $json.body.email;
8
9// Or with $input
10const webhookData = $input.first().json.body;
11const 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
1// ✅ Single result
2return [{
3 json: {
4 field1: value1,
5 field2: value2
6 }
7}];
8
9// ✅ Multiple results
10return [
11 {json: {id: 1, data: 'first'}},
12 {json: {id: 2, data: 'second'}}
13];
14
15// ✅ Transformed array
16const transformed = $input.all()
17 .filter(item => item.json.valid)
18 .map(item => ({
19 json: {
20 id: item.json.id,
21 processed: true
22 }
23 }));
24return transformed;
25
26// ✅ Empty result (when no data to return)
27return [];
28
29// ✅ Conditional return
30if (shouldProcess) {
31 return [{json: processedData}];
32} else {
33 return [];
34}
Incorrect Return Formats
1// ❌ WRONG: Object without array wrapper
2return {
3 json: {field: value}
4};
5
6// ❌ WRONG: Array without json wrapper
7return [{field: value}];
8
9// ❌ WRONG: Plain string
10return "processed";
11
12// ❌ WRONG: Raw data without mapping
13return $input.all(); // Missing .map()
14
15// ❌ WRONG: Incomplete structure
16return [{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
Based on production workflows, here are the most useful patterns:
1. Multi-Source Data Aggregation
Combine data from multiple APIs, webhooks, or nodes
1const allItems = $input.all();
2const results = [];
3
4for (const item of allItems) {
5 const sourceName = item.json.name || 'Unknown';
6 // Parse source-specific structure
7 if (sourceName === 'API1' && item.json.data) {
8 results.push({
9 json: {
10 title: item.json.data.title,
11 source: 'API1'
12 }
13 });
14 }
15}
16
17return results;
2. Filtering with Regex
Extract patterns, mentions, or keywords from text
1const pattern = /\b([A-Z]{2,5})\b/g;
2const matches = {};
3
4for (const item of $input.all()) {
5 const text = item.json.text;
6 const found = text.match(pattern);
7
8 if (found) {
9 found.forEach(match => {
10 matches[match] = (matches[match] || 0) + 1;
11 });
12 }
13}
14
15return [{json: {matches}}];
3. Data Transformation & Enrichment
Map fields, normalize formats, add computed fields
1const items = $input.all();
2
3return items.map(item => {
4 const data = item.json;
5 const nameParts = data.name.split(' ');
6
7 return {
8 json: {
9 first_name: nameParts[0],
10 last_name: nameParts.slice(1).join(' '),
11 email: data.email,
12 created_at: new Date().toISOString()
13 }
14 };
15});
4. Top N Filtering & Ranking
Sort and limit results
1const items = $input.all();
2
3const topItems = items
4 .sort((a, b) => (b.json.score || 0) - (a.json.score || 0))
5 .slice(0, 10);
6
7return topItems.map(item => ({json: item.json}));
5. Aggregation & Reporting
Sum, count, group data
1const items = $input.all();
2const total = items.reduce((sum, item) => sum + (item.json.amount || 0), 0);
3
4return [{
5 json: {
6 total,
7 count: items.length,
8 average: total / items.length,
9 timestamp: new Date().toISOString()
10 }
11}];
See: COMMON_PATTERNS.md for 10 detailed production patterns
Error Prevention - Top 5 Mistakes
#1: Empty Code or Missing Return (Most Common)
1// ❌ WRONG: No return statement
2const items = $input.all();
3// ... processing code ...
4// Forgot to return!
5
6// ✅ CORRECT: Always return data
7const items = $input.all();
8// ... processing ...
9return items.map(item => ({json: item.json}));
#2: Expression Syntax Confusion
1// ❌ WRONG: Using n8n expression syntax in code
2const value = "{{ $json.field }}";
3
4// ✅ CORRECT: Use JavaScript template literals
5const value = `${$json.field}`;
6
7// ✅ CORRECT: Direct access
8const value = $input.first().json.field;
#3: Incorrect Return Wrapper
1// ❌ WRONG: Returning object instead of array
2return {json: {result: 'success'}};
3
4// ✅ CORRECT: Array wrapper required
5return [{json: {result: 'success'}}];
#4: Missing Null Checks
1// ❌ WRONG: Crashes if field doesn't exist
2const value = item.json.user.email;
3
4// ✅ CORRECT: Safe access with optional chaining
5const value = item.json?.user?.email || 'no-email@example.com';
6
7// ✅ CORRECT: Guard clause
8if (!item.json.user) {
9 return [];
10}
11const value = item.json.user.email;
#5: Webhook Body Nesting
1// ❌ WRONG: Direct access to webhook data
2const email = $json.email;
3
4// ✅ CORRECT: Webhook data under .body
5const email = $json.body.email;
See: ERROR_PATTERNS.md for comprehensive error guide
Built-in Functions & Helpers
$helpers.httpRequest()
Make HTTP requests from within code:
1const response = await $helpers.httpRequest({
2 method: 'GET',
3 url: 'https://api.example.com/data',
4 headers: {
5 'Authorization': 'Bearer token',
6 'Content-Type': 'application/json'
7 }
8});
9
10return [{json: {data: response}}];
DateTime (Luxon)
Date and time operations:
1// Current time
2const now = DateTime.now();
3
4// Format dates
5const formatted = now.toFormat('yyyy-MM-dd');
6const iso = now.toISO();
7
8// Date arithmetic
9const tomorrow = now.plus({days: 1});
10const lastWeek = now.minus({weeks: 1});
11
12return [{
13 json: {
14 today: formatted,
15 tomorrow: tomorrow.toFormat('yyyy-MM-dd')
16 }
17}];
$jmespath()
Query JSON structures:
1const data = $input.first().json;
2
3// Filter array
4const adults = $jmespath(data, 'users[?age >= `18`]');
5
6// Extract fields
7const names = $jmespath(data, 'users[*].name');
8
9return [{json: {adults, names}}];
See: BUILTIN_FUNCTIONS.md for complete reference
Best Practices
1. Always Validate Input Data
1const items = $input.all();
2
3// Check if data exists
4if (!items || items.length === 0) {
5 return [];
6}
7
8// Validate structure
9if (!items[0].json) {
10 return [{json: {error: 'Invalid input format'}}];
11}
12
13// Continue processing...
2. Use Try-Catch for Error Handling
1try {
2 const response = await $helpers.httpRequest({
3 url: 'https://api.example.com/data'
4 });
5
6 return [{json: {success: true, data: response}}];
7} catch (error) {
8 return [{
9 json: {
10 success: false,
11 error: error.message
12 }
13 }];
14}
3. Prefer Array Methods Over Loops
1// ✅ GOOD: Functional approach
2const processed = $input.all()
3 .filter(item => item.json.valid)
4 .map(item => ({json: {id: item.json.id}}));
5
6// ❌ SLOWER: Manual loop
7const processed = [];
8for (const item of $input.all()) {
9 if (item.json.valid) {
10 processed.push({json: {id: item.json.id}});
11 }
12}
4. Filter Early, Process Late
1// ✅ GOOD: Filter first to reduce processing
2const processed = $input.all()
3 .filter(item => item.json.status === 'active') // Reduce dataset first
4 .map(item => expensiveTransformation(item)); // Then transform
5
6// ❌ WASTEFUL: Transform everything, then filter
7const processed = $input.all()
8 .map(item => expensiveTransformation(item)) // Wastes CPU
9 .filter(item => item.json.status === 'active');
5. Use Descriptive Variable Names
1// ✅ GOOD: Clear intent
2const activeUsers = $input.all().filter(item => item.json.active);
3const totalRevenue = activeUsers.reduce((sum, user) => sum + user.json.revenue, 0);
4
5// ❌ BAD: Unclear purpose
6const a = $input.all().filter(item => item.json.active);
7const t = a.reduce((s, u) => s + u.json.revenue, 0);
6. Debug with console.log()
1// Debug statements appear in browser console
2const items = $input.all();
3console.log(`Processing ${items.length} items`);
4
5for (const item of items) {
6 console.log('Item data:', item.json);
7 // Process...
8}
9
10return result;
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
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_essentials("nodes-base.code") - Validate code:
validate_node_operation()
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
.bodyif 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.
What Users Are Saying
Real feedback from the community
Environment Matrix
Dependencies
Framework Support
Context Window
Security & Privacy
Information
- Author
- davila7
- Updated
- 2026-01-30
- Category
- productivity-tools
Related Skills
N8N Code Javascript
Write JavaScript code in n8n Code nodes. Use when writing JavaScript in n8n, using …
View Details →N8N Code Python
Write Python code in n8n Code nodes. Use when writing Python in n8n, using _input/_json/_node …
View Details →N8N Code Python
Write Python code in n8n Code nodes. Use when writing Python in n8n, using _input/_json/_node …
View Details →