n8n Code Node Explained: Adding Custom Logic to Your Workflows
The Code Node in n8n is the Swiss Army knife of automation: a small, flexible place where you can drop a bit of JavaScript to solve a specific problem that no pre-built node handles cleanly. Use it for tiny custom transformations, calculations, or format changes—never to rebuild your entire workflow.
Why use the Code Node?
Visual nodes cover most automation needs. The Code Node shines when you need one precise tweak that would be awkward or impossible with standard nodes. It gives you:
- Flexibility to shape data exactly how you want it.
- Simplicity—only a few lines of JavaScript are often enough.
- Power to perform custom math, transform dates, or combine fields.
When to reach for the Code Node
- Combining first name and last name into a friendly full name.
- Reformatting a date or time string into a custom format.
- Running a small calculation that affects only part of a dataset.
- Cleaning or reshaping incoming JSON before sending it to another system.
How the Code Node works (the essentials)
The Code Node exposes the data from the previous node as an array of items. Each item usually looks like { json: { /* your fields */ } }. You read values from this structure, apply your logic in JavaScript, then return items for the next node.
Single-item example: build a full name
Suppose an earlier node provides firstName and lastName. The Code Node can combine them and return a new field fullName. Keep it short and clear:
const firstName = items[0].json.firstName || '';
const lastName = items[0].json.lastName || '';
const fullName = `${firstName} ${lastName}`.trim();
return [{ json: { fullName } }];This reads the first item, creates fullName, and returns a single item containing only that new field. The next node can use fullName directly—for example, in a personalized email.
Multi-item example: add fullName to every item
When the input contains multiple records, map over the items so each record is preserved and extended:
return items.map(item => {
const firstName = item.json.firstName || '';
const lastName = item.json.lastName || '';
item.json.fullName = `${firstName} ${lastName}`.trim();
return item;
});This pattern keeps all existing fields intact while adding your new one. It’s the most common approach when transforming collections.
Best practices
- Use code sparingly. Rely on visual nodes for the bulk of your workflow. The Code Node is for small, precise jobs.
- Guard against missing data. Use defaults (empty strings, zeros) and
||checks to avoid runtime errors. - Keep it readable. Comment short functions and name variables clearly.
- Test with multiple scenarios. Run sample inputs that include edge cases like empty fields, special characters, and unexpected types.
- Return the right shape. Most n8n nodes expect an array of items. Use
return itemsorreturn [{ json: {...} }]appropriately.
Common use cases beyond combining names
- Custom date and time formatting for different locales.
- Small arithmetic or currency conversions.
- Conditional field generation (e.g., set status based on multiple inputs).
- Cleaning and normalizing strings (strip special characters, trim whitespace).
Final thoughts
The Code Node removes the limits of prebuilt nodes without forcing you to become a full-time developer. Treat it as a precision tool: use visual nodes for 99% of the work and reach for the Code Node when a neat, custom tweak is needed. With a little JavaScript and the right patterns, you can solve almost any niche problem inside your n8n workflows.
Free Stuff!!! |
|
Check out this really cool thing |
| Click me |