knoten / builtin / core
JavaScript Code
Transform items with a few lines of JavaScript, in a sandbox with no network
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
Write a small JavaScript transform when no other step does what you need. It starts fast and runs in a sandbox: no network, no page access, no timers.
What it does
Section titled “What it does”JavaScript Code runs your code in QuickJS, a small JavaScript engine compiled to WebAssembly, inside its own worker for each run of the step. The engine loads the first time the step runs.
At the top of Setup, a green card says what the step can reach: What it touches: nothing outside this step. No internet (use HTTP Request for calls), no web pages, tabs or your files, no connections or stored data, and how long it may run (Stops after …), with Change limits (the step’s Time limit). In Ready to run and on a Marketplace listing, the step shows as a calm grey Runs code in a sandbox row, not a warning.
Pick how it runs with Run:
- Once for each item (the default):
itemis the current item and$itemIndexits position (from 0). Return the new item. Each item gets a fresh engine, so nothing leaks from one item to the next. - Once for all items:
itemsis the whole list. Return the new list; each element becomes one item.
Both modes also get $workflow (id, name) and $execution (id, mode). The data you read is frozen JSON: change a copy ({ ...item }), not the original. Async code works: a returned promise is awaited.
What comes out:
- An object is the item. Any other value becomes
{ result: value }. Returning nothing gives an empty item. - In Once for all items, the code must return a list, or the step fails with
"Once for all items" code must return a list.
console.log() lines go to the run’s activity log (the first 200; after that, one line says the rest are hidden). console.warn() and console.error() show as warnings and errors.
When to use it
Section titled “When to use it”- A calculation or a reshaping that would take several steps otherwise.
- Filtering or sorting with a rule that is easier to write than to click.
- When you know JavaScript better than Python. Python Code does the same job in Python, but starts slower.
For counts, sums and averages per group, try Summarize items first: no code needed.
Inputs and settings
Section titled “Inputs and settings”| Einstellung | Typ | Was es bewirkt |
|---|---|---|
| Run | Choice | Options: Once for each item, Once for all items. Standard: Once for each item |
| Code (erforderlich) | Code | Read data with item / items. {{ }} isn’t replaced in code. |
| Time limit | Number | Default 10 s, up to 60 s. |
| Memory limit (MB) | Number | Default 64 MB, up to 256 MB. |
// item is the current item; return the new onereturn { ...item, total: item.qty * item.price };// items is every item; return the new listreturn items.filter((i) => i.active).map((i) => ({ name: i.name }));Limits:
- Time limit: 10 seconds by default, up to 60 seconds. A run that goes over stops with
Stopped after 10 s (time limit). - Memory limit: 64 MB by default, up to 256 MB. Going over stops with
Used more than 64 MB (memory limit). - Stop in the app ends the code at once.
Outputs
Section titled “Outputs”-
Output
The items your code returned, one per object.
Dependencies and credentials
Section titled “Dependencies and credentials”None. This node needs no connection and has no slots for other nodes.
The sandbox has no fetch, XMLHttpRequest, WebSocket, timers or imports. To call an API, put an HTTP Request step before or after this one.
Example workflow
Section titled “Example workflow”- HTTP Request fetches a list of orders (one item per order).
- JavaScript Code, Once for each item:
return { ...item, total: item.qty * item.price }; - Summarize items sums
totalperregion.
Common issues
Section titled “Common issues”Line 3, item 2: TypeError: …: the error names the line of your code and the item (counted from 1). Open the step’s Input to see that item.'fetch' is not defined: the sandbox has no network. Use HTTP Request for calls.- The output still shows
{{ … }}: expressions are not replaced in code. Read the value fromitem. - “Once for all items” fails: return a list, for example
return items;.
Related nodes
Section titled “Related nodes”- Python Code: the same idea in Python.
- Summarize items: totals and counts per group, no code.
- Edit Fields: add, rename or set fields without code.