$npx -y skills add czlonkowski/n8n-skills --skill n8n-code-javascriptWrite 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 do
| 1 | # JavaScript Code Node |
| 2 | |
| 3 | Expert guidance for writing JavaScript code in n8n Code nodes. |
| 4 | |
| 5 | --- |
| 6 | |
| 7 | ## Quick Start |
| 8 | |
| 9 | ```javascript |
| 10 | // Basic template for Code nodes |
| 11 | const items = $input.all(); |
| 12 | |
| 13 | // Process data |
| 14 | const processed = items.map(item => ({ |
| 15 | json: { |
| 16 | ...item.json, |
| 17 | processed: true, |
| 18 | timestamp: new Date().toISOString() |
| 19 | } |
| 20 | })); |
| 21 | |
| 22 | return processed; |
| 23 | ``` |
| 24 | |
| 25 | ### Essential Rules |
| 26 | |
| 27 | 1. **Choose "Run Once for All Items" mode** (recommended for most use cases) |
| 28 | 2. **Access data**: `$input.all()`, `$input.first()`, or `$input.item` |
| 29 | 3. **Return `[{json: {...}}]`** — the canonical, mode-portable form. In *Run Once for All Items* mode n8n also auto-wraps a bare `return {…}` object, so that runs too; what genuinely fails is returning a primitive (string/number) or `null`. |
| 30 | 4. **CRITICAL**: Webhook data is under `$json.body` (not `$json` directly) |
| 31 | 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`), 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. |
| 32 | 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. |
| 33 | 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. |
| 34 | |
| 35 | --- |
| 36 | |
| 37 | ## Mode Selection Guide |
| 38 | |
| 39 | The Code node offers two execution modes. Choose based on your use case: |
| 40 | |
| 41 | ### Run Once for All Items (Recommended - Default) |
| 42 | |
| 43 | **Use this mode for:** 95% of use cases |
| 44 | |
| 45 | - **How it works**: Code executes **once** regardless of input count |
| 46 | - **Data access**: `$input.all()` or `items` array |
| 47 | - **Best for**: Aggregation, filtering, batch processing, transformations, API calls with all data |
| 48 | - **Performance**: Faster for multiple items (single execution) |
| 49 | |
| 50 | ```javascript |
| 51 | // Example: Calculate total from all items |
| 52 | const allItems = $input.all(); |
| 53 | const total = allItems.reduce((sum, item) => sum + (item.json.amount || 0), 0); |
| 54 | |
| 55 | return [{ |
| 56 | json: { |
| 57 | total, |
| 58 | count: allItems.length, |
| 59 | average: total / allItems.length |
| 60 | } |
| 61 | }]; |
| 62 | ``` |
| 63 | |
| 64 | **When to use:** |
| 65 | - ✅ Comparing items across the dataset |
| 66 | - ✅ Calculating totals, averages, or statistics |
| 67 | - ✅ Sorting or ranking items |
| 68 | - ✅ Deduplication |
| 69 | - ✅ Building aggregated reports |
| 70 | - ✅ Combining data from multiple items |
| 71 | |
| 72 | ### Run Once for Each Item |
| 73 | |
| 74 | **Use this mode for:** Specialized cases only |
| 75 | |
| 76 | - **How it works**: Code executes **separately** for each input item |
| 77 | - **Data access**: `$input.item` or `$item` |
| 78 | - **Best for**: Item-specific logic, independent operations, per-item validation |
| 79 | - **Performance**: Slower for large datasets (multiple executions) |
| 80 | |
| 81 | ```javascript |
| 82 | // Example: Add processing timestamp to each item |
| 83 | const item = $input.item; |
| 84 | |
| 85 | return [{ |
| 86 | json: { |
| 87 | ...item.json, |
| 88 | processed: true, |
| 89 | processedAt: new Date().toISOString() |
| 90 | } |
| 91 | }]; |
| 92 | ``` |
| 93 | |
| 94 | **When to use:** |
| 95 | - ✅ Each item needs independent API call |
| 96 | - ✅ Per-item validation with different error handling |
| 97 | - ✅ Item-specific transformations based on item properties |
| 98 | - ✅ When items must be processed separately for business logic |
| 99 | |
| 100 | **Decision Shortcut:** |
| 101 | - **Need to look at multiple items?** → Use "All Items" mode |
| 102 | - **Each item completely independent?** → Use "Each Item" mode |