The Code Node is arguably n8n's most powerful feature for developers. While visual nodes cover most common integrations, the Code Node gives you direct access to a sandboxed Node.js environment where you can write any JavaScript logic — from simple string formatting to complex algorithmic transformations.

Code Node Modes

The Code Node operates in two processing modes:

  • Run Once for All Items: The code runs once with access to all incoming items as an array. Best for batch processing.
  • Run Once for Each Item: The code runs separately for each incoming item. Best for per-record transformations.

Basic Input/Output Structure

Inside the Code Node, input data is accessed via $input.all() (all items) or $input.first() (first item). The node expects you to return an array of item objects:

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

// Transform each item
return items.map(item => {
  const fullName = `${item.json.first_name} ${item.json.last_name}`;
  return {
    json: {
      ...item.json,
      full_name: fullName,
      processed_at: new Date().toISOString()
    }
  };
});

Available Built-in Helpers

The Code Node sandbox includes several pre-imported utilities:

  • $json — Current item's JSON data
  • $node — Reference previous nodes by name: $node['NodeName'].json
  • $workflow — Access workflow metadata: $workflow.id
  • DateTime — Luxon DateTime library for date manipulation
  • _ — Lodash utility library for collection helpers

Filtering and Deduplicating Arrays

A common task is removing duplicates from a dataset before inserting records into a database. Here's a clean pattern using the Map object:

const items = $input.all();

// Deduplicate by email address
const seen = new Map();
const uniqueItems = items.filter(item => {
  const email = item.json.email;
  if (seen.has(email)) return false;
  seen.set(email, true);
  return true;
});

return uniqueItems;

⚠️ Sandbox Limitation: The Code Node does not have access to the filesystem or native Node.js modules like fs or path. For file operations, use n8n's dedicated Read/Write Files from Disk nodes instead.

Making HTTP Requests from Code Node

You can fetch external data using the built-in $http helper without leaving the Code Node:

// Fetch currency conversion rate
const response = await $http.request({
  method: 'GET',
  url: 'https://api.exchangerate-api.com/v4/latest/USD',
});

const inrRate = response.rates.INR;

return $input.all().map(item => ({
  json: {
    ...item.json,
    price_inr: item.json.price_usd * inrRate
  }
}));