---
title: "how to format javascript in n8n code nodes"
url: https://codefmt.dev/blog/format-javascript-n8n-code-node
sources:
  - https://codefmt.dev/blog/format-javascript-n8n-code-node
licence: "© codefmt. Cite with attribution to https://codefmt.dev."
---

# how to format javascript in n8n code nodes

published June 9, 2026

to format javascript in an [n8n Code node](/n8n), paste your code into codefmt and choose n8n. it handles the node's async wrapper, declares globals like `$input`, `$json`, and `items` so they are never flagged undefined, then hands back clean, linted code. typescript Code nodes work the same way.

## why eslint and prettier trip on n8n code

n8n Code nodes run your javascript or typescript with a set of injected globals and inside an async wrapper. that is why `await` works at the top level of your node. a generic formatter parsing the body as a complete program can choke on that, and a generic linter has no idea what `$input` or `$json` are, so it reports them as undefined.

codefmt declares all of n8n's globals before running oxlint and handles the async wrapper during formatting, so the false positives disappear and the real issues still surface.

## how to format an n8n code node

1. copy the code out of your n8n Code node.
2. paste it into the [codefmt n8n formatter](/n8n) and choose javascript or typescript.
3. codefmt formats with biome, lints with oxlint, and keeps your return shape intact.
4. copy the formatted result back into the Code node.

## return shape and execution modes

an n8n Code node returns an array of objects, each with a `json` key: `return [{ json: { name: 'value' } }]`. the node runs in one of two modes, and codefmt recognizes the globals available in each.

| mode                   | access input via    | return                         |
| ---------------------- | ------------------- | ------------------------------ |
| run once for all items | $input.all()        | an array of items              |
| run once for each item | $input.item / $json | one item for the current input |

## n8n globals codefmt knows

| availability  | identifiers                                                                                                   |
| ------------- | ------------------------------------------------------------------------------------------------------------- |
| available     | $input, $json, $ (node accessor), $node, $workflow, $execution, $env, $vars, items, DateTime (luxon), console |
| not available | fetch, $http, $itemIndex, $parameter, $position (use the HTTP Request node for outbound calls)                |

compare formatting against the [Prettier playground](/vs-prettier-playground).

[Format your n8n code now →](/n8n) drop in a Code node and get clean, linted javascript or typescript back with n8n's globals recognized.

## frequently asked questions

how do I format javascript in an n8n Code node?

paste your Code node javascript into codefmt and choose n8n. it handles the async wrapper, declares n8n's globals like $input, $json, and items so they aren't flagged undefined, and returns clean, linted code. it formats typescript Code nodes the same way.

why does ESLint flag $input and $json as undefined in n8n code?

standard linters don't know about n8n's injected globals. they treat $input, $json, $node, and $workflow as undefined variables. codefmt declares all of n8n's globals before running oxlint, so you get accurate linting without those false positives.

how do I return data from an n8n Code node?

return an array of objects with a json property, for example return \[{ json: { name: 'value' } }\]. codefmt formats this pattern correctly and preserves the expected return structure.

what is the difference between run once for all items and run once for each item?

run once for all items gives you every input item via $input.all() and you return an array. run once for each item runs your code once per item with $input.item or $json for the current item. codefmt recognizes the globals specific to each mode.

can I use fetch in an n8n Code node?

no. fetch, $http, $itemIndex, $parameter, and $position are not available inside the n8n Code node. use n8n's dedicated HTTP Request node for outbound requests. codefmt knows which globals are and aren't available so it won't suggest the wrong ones.

primary source: [n8n: Code node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.code/)
