DEV Community

Pirate Prentice
Pirate Prentice

Posted on

n8n Code node: run JavaScript (or Python) inside your workflows — free examples

The n8n Code node is one of the most powerful tools in n8n. It lets you run raw JavaScript (or Python) inside any workflow — perfect for data transformation, custom logic, or anything the built-in nodes cannot handle.

This guide covers everything you need to know: syntax, common patterns, real examples, and a free workflow JSON you can import and run today.


What the Code node does

The Code node gives you a full JavaScript (Node.js) or Python runtime inside your workflow. You can:

  • Transform or reshape data from a previous node
  • Write custom business logic that no built-in node covers
  • Build or parse structured objects before passing to the next node

It runs per item or once for all items — you choose.


Basic syntax

In Run Once for Each Item mode:

// $input.item.json is the current item data
const name = $input.item.json.name;
const email = $input.item.json.email;

return {
  json: {
    domain: email.split('@')[1],
    processedAt: new Date().toISOString()
  }
};
Enter fullscreen mode Exit fullscreen mode

In Run Once for All Items mode:

const items = $input.all();
const total = items.reduce((sum, item) => sum + item.json.amount, 0);
return [{ json: { total, count: items.length } }];
Enter fullscreen mode Exit fullscreen mode

Key rule: always return objects with a json key.


5 real-world Code node patterns

1. Date formatting

const d = new Date($input.item.json.created_at);
return { json: { ...$input.item.json, date_iso: d.toISOString().split('T')[0] } };
Enter fullscreen mode Exit fullscreen mode

2. Flatten a nested object

const raw = $input.item.json;
return { json: { id: raw.id, name: raw.customer.name, plan: raw.subscription.plan } };
Enter fullscreen mode Exit fullscreen mode

3. Filter array (Run Once for All Items)

const items = $input.all();
return items.filter(i => i.json.amount > 100).map(i => ({ json: { ...i.json, tag: 'high-value' } }));
Enter fullscreen mode Exit fullscreen mode

4. Generate a unique ID

const { randomUUID } = require('crypto');
return { json: { ...$input.item.json, id: randomUUID() } };
Enter fullscreen mode Exit fullscreen mode

5. Read data from a named node

const webhookData = $('Webhook').first().json;
const rows = $('Google Sheets').all().map(i => i.json);
Enter fullscreen mode Exit fullscreen mode

Common mistakes

Mistake Fix
Returning plain object instead of {json: ...} Always wrap in {json: ...}
Forgetting array in All Items mode Wrap return in [...]
Using require('axios') Use the HTTP Request node instead

Free workflow JSON

Drop a comment below and I will share a ready-to-import workflow JSON demonstrating all 5 patterns — runs with the Code node only, no credentials needed.

Or grab the full n8n Workflow Starter Pack ($29) with 3 production-ready automations: pirateprentice.gumroad.com


Summary

The Code node is your escape hatch when no built-in node does exactly what you need. Master the $input API, always return {json: ...}, and you can build virtually any transformation logic inside n8n.

What is your most-used Code node pattern? Drop it in the comments.

Top comments (1)

Collapse
 
pirateprentice profile image
Pirate Prentice

Here is the free Code node workflow JSON I promised -- import this into n8n (Settings > Import Workflow) to see all 5 patterns running live:

{
  "nodes": [
    { "name": "Manual Trigger", "type": "n8n-nodes-base.manualTrigger", "position": [240, 300] },
    {
      "name": "Code - Date Format",
      "type": "n8n-nodes-base.code",
      "parameters": { "jsCode": "const d = new Date($input.item.json.created_at || new Date());\nreturn { json: { ...$input.item.json, date_iso: d.toISOString().split('T')[0], date_friendly: d.toLocaleDateString('en-US', {month:'long',day:'numeric',year:'numeric'}) } };" },
      "position": [460, 300]
    },
    {
      "name": "Code - Generate UUID",
      "type": "n8n-nodes-base.code",
      "parameters": { "jsCode": "const { randomUUID } = require('crypto');\nreturn { json: { ...$input.item.json, id: randomUUID() } };" },
      "position": [680, 300]
    }
  ],
  "connections": {
    "Manual Trigger": { "main": [[{ "node": "Code - Date Format", "type": "main", "index": 0 }]] },
    "Code - Date Format": { "main": [[{ "node": "Code - Generate UUID", "type": "main", "index": 0 }]] }
  }
}
Enter fullscreen mode Exit fullscreen mode

For the full 3-workflow production pack (Lead CRM + Stripe receipt + Form to Sheets+Slack), grab it at pirateprentice.gumroad.com