Now inviting automation leaders: publish your playbook as a skill, under your own name. Publish yours →
Automation Skills
All skills/n8n code nodes official/DECISION_TREE
Reference · n8n code nodes official

DECISION_TREE

Part of n8n code nodes official. Install the skill and your agent reads this page when it needs the depth.

Decision tree

The bar for using a Code node is high. This file walks through verifying that simpler alternatives (expression, Edit Fields arrow function) don't work.

Stage 1: try an expression

For any single-field transform, ask: can this fit in {{...}}?

Expressions express:

If it fits one line, even a long one, do it as an expression. Multi-line indented form is supported. See the n8n-expressions-official skill.

Test: can you describe it as "take A and produce B" without intermediate variables? If yes, expression.

// Expression: "take items, return joined names"
{{ $json.items.map(item => item.name).join(', ') }}

// Expression: "take created_at, return YYYY-MM-DD"
{{ DateTime.fromISO($json.created_at).toFormat('yyyy-MM-dd') }}

// Expression: "take items, return their total"
{{ $json.items.reduce((sum, item) => sum + item.price, 0) }}

Stage 2: try Edit Fields with an arrow function

When the transform needs intermediate variables, multi-step logic, or doesn't fit one line, use Edit Fields with an inline arrow function.

Pattern: an immediately-invoked arrow function inside the expression.

{{ (() => {
    const items = $json.items
    const total = items.reduce((sum, item) => sum + item.price, 0)
    const tax = total * 0.08
    return `Total: $${(total + tax).toFixed(2)}`
})() }}

This buys you:

What you give up vs. a Code node:

For more, see ARROW_FUNCTIONS_IN_EDIT_FIELDS.md.

Test: does the logic operate on a single input item ($json)? If yes, Edit Fields. If you need to combine items, see Stage 3.

Stage 3: justify the Code node

Stages 1 and 2 are ruled out. Remaining valid reasons:

Reason A: External libraries

JS Code supports a curated allowlist (lodash, crypto, luxon). If you genuinely need lodash.groupBy or one of the others, Code is necessary. No HTTP client is bundled, use the HTTP Request node for any outbound calls.

Counter-question: do you actually need lodash? Modern JS has Array.prototype.reduce and Object.entries.

Reason B: Multi-source aggregation across the whole dataset

Most common valid case. When a node needs to:

Code, full stop. Don't split across many Edit Fields.

Splitting an analytics rollup into 10+ smaller nodes is harder to read, debug, and iterate than one 80-line Code node with clear sections.

But: ask if part can be a built-in (Aggregate, Summarize, Sort, Group). Check via search_nodes. If a built-in covers half, do that half there.

Reason B-adjacent: Statistical operations across many items

Median, percentile, ratio-of-ratios, normalized scores: anything needing all items to compute an answer for any one. Single-item tools can't express this.

Reason C: Crypto operations

HMAC signing, JWT verification, custom encryption. JS has require('crypto'). Python has hashlib and hmac.

Counter-question: is this for auth? See n8n-credentials-and-security-official. httpCustomAuth may handle it without code.

Reason D: User has existing JS/Python they want to drop in

Worth pushing back on. n8n workflows are visual, and dropping a 200-line script defeats most of the value. Options:

When the user insists on a Code node

Their call. Build it, but:

Examples by category

"Format a date"

"Filter items by criteria"

"Build an email body"

"Transform API response into target shape"

"Validate input data"

The honest answer

Code feels productive because writing JS is what you know. But the workflow gets read by someone (maybe future-you) who has to understand each node at a glance, modify one piece without breaking others, and debug from runtime errors.

Visual nodes with descriptive names beat a Code block on every one. 30 seconds saved now costs hours later.

When in doubt: not a Code node.

Publish your skill

Copy this prompt into Claude Code, Codex or any agent. It walks you through your first skill in about 20 minutes, with you as the author.

Click to copy.