/n8n-tutorials

How to use the Code node in n8n?

Learn how to use the Code node in n8n with simple steps, examples, and tips to streamline automation and enhance your workflows.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free consultation

How to use the Code node in n8n?

The Code node in n8n lets you run small, controlled pieces of JavaScript inside a workflow so you can transform data, build custom logic, or prepare API payloads. You drop it into a workflow like any other node, and it receives the JSON from previous nodes and outputs JSON for the next ones. Think of it as a tiny serverless function living inside your automation, good for logic that’s too complex for other nodes but still small enough that it doesn’t belong in a full external service.

 

What the Code Node Actually Is

 

The Code node is a JavaScript execution step. It runs in a sandbox, synchronously, during the workflow execution. You write JavaScript in the editor section called Run Once for Each Item (default mode) or Run Once (explicit mode if you switch it), depending on how you want it to behave.

  • Run Once for Each Item: n8n feeds each input item (a JSON object) to your code one by one. You return one item or multiple items per input.
  • Run Once: Your code receives the entire input array and returns a new array. Good for combining, filtering, or aggregating results.

You always output an array of objects containing a json property. This is what the next node receives.

 

The Direct, Practical Way to Use It

 

Drop a Code node into your workflow → open it → write JavaScript inside → make sure you return an array of objects with a json key. That’s the minimum you need to use it correctly.

 

Understanding What Comes Into the Node

 

Inside a Code node, the input data is accessible via special variables provided by n8n:

  • items: An array of all input items, each item having json (primary data) and sometimes binary (files).
  • item: In Run Once for Each Item mode, this refers to the current item.
  • $input: Helper object that lets you access items more explicitly (e.g., $input.item.json).

You don't create these — they’re injected by n8n.

 

A Real, Working Example (Run Once for Each Item)

 

This transforms a user's full name into first and last name. It receives something like { "name": "Ada Lovelace" } and outputs structured data.

// Split the name into two parts
const [first, last] = item.json.name.split(' ');

// Always return an array of objects with json:
return [
  {
    json: {
      firstName: first,
      lastName: last,
    }
  }
];

 

Example for Run Once Mode (multiple input items → single output array)

 

This example collects all email addresses from previous nodes and outputs a combined list in one item.

// items = array of all input items
const emails = items.map(i => i.json.email);

// Return one item that contains all emails
return [
  {
    json: {
      emails: emails
    }
  }
];

 

Common Production Uses

 

  • Transforming payloads before sending them to an API.
  • Cleaning or restructuring JSON received from webhooks.
  • Applying conditional logic that’s too complex for Switch or Set nodes.
  • Merging or flattening data arrays.
  • Generating signatures or encryption for authenticated API calls (within reason).

 

Production Tips (from real n8n environments)

 

  • Keep logic small. If it turns into many lines or feels like an actual program, move it out to an external API or Function service.
  • No async/await needed unless you enable it. The Code node behaves synchronously unless you're using the “Code (async)” node variant.
  • Always return an array. If you forget this, the node fails.
  • Don’t mutate items in place unless you mean to. Better to create new objects.
  • Watch for large arrays. The node runs in memory; giant arrays slow down workflows.
  • No external imports. You cannot import npm packages. Only vanilla JavaScript.

 

How to Think About the Code Node

 

The Code node is your escape hatch for logic that’s awkward with UI nodes but too lightweight to build a microservice for. It’s best used for transformations, validations, and small custom branching logic. It is not a full programming runtime, so treating it as such leads to slow workflows, timeouts, and painful debugging. Keep it small and purposeful, and test with sample data as you build.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022