节点与集成
Code
The Code node lets you run your own JavaScript or Python inside a workflow, so you can transform data or add logic the built-in nodes don't cover.
Use the Code node to write custom JavaScript or Python and run it as a step in your workflow.
Usage
How to use the Code node.
Choose a mode
There are two modes:
- Run Once for All Items: this is the default. When your workflow runs, the code in the code node executes once, regardless of how many input items there are.
- Run Once for Each Item: choose this if you want your code to run for every input item.
JavaScript
The Code node supports Node.js.
Supported JavaScript features
The Code node supports:
- Promises. Instead of returning the items directly, you can return a promise which resolves accordingly.
- Writing to your browser console using
console.log. This is useful for debugging and troubleshooting your workflows.
External libraries
If you self-host n8n, you can import and use built-in and external npm modules in the Code node. To learn how to enable external modules, refer to the Enable modules in Code node guide.
If you use n8n Cloud, you can't import external npm modules. n8n makes two modules available for you:
Built-in methods and variables
n8n provides built-in methods and variables for working with data and accessing n8n data. Refer to Built-in methods and variables for more information.
The syntax to use the built-in methods and variables is $variableName or $methodName(). Type $ in the Code node or expressions editor to see a list of suggested methods and variables.
Keyboard shortcuts
The Code node editing environment supports time-saving and useful keyboard shortcuts for a range of operations from autocompletion to code-folding and using multiple-cursors. See the full list of keyboard shortcuts.
Python (Pyodide - legacy)
Pyodide is a legacy feature. n8n v2 no longer supports this feature.
n8n added Python support in version 1.0. It doesn't include a Python executable. Instead, n8n provides Python support using Pyodide, which is a port of CPython to WebAssembly. This limits the available Python packages to the Packages included with Pyodide. n8n downloads the package automatically the first time you use it.
Built-in methods and variables
n8n provides built-in methods and variables for working with data and accessing n8n data. Refer to Built-in methods and variables for more information.
The syntax to use the built-in methods and variables is _variableName or _methodName(). Type _ in the Code node to see a list of suggested methods and variables.
Keyboard shortcuts
The Code node editing environment supports time-saving and useful keyboard shortcuts for a range of operations from autocompletion to code-folding and using multiple-cursors. See the full list of keyboard shortcuts.
File system and HTTP requests
You can't access the file system or make HTTP requests. Use the following nodes instead:
Python (Native)
n8n added native Python support using task runners in version 1.111.0. This feature is stable as of n8n v2.
Main differences from Pyodide:
- Native Python supports only
_itemsin all-items mode and_itemin per-item mode. It doesn't support other n8n built-in methods and variables. - On self-hosted, native Python supports importing native Python modules from the standard library and from third-parties, if the
n8nio/runnersimage includes them and explicitly allowlists them. See adding extra dependencies for task runners for more details. - Native Python denies insecure built-ins by default. See task runners environment variables for more details.
- Unlike Pyodide, which accepts dot access notation, for example,
item.json.myNewField, native Python only accepts bracket access notation, for example,item["json"]["my_new_field"]. There may be other minor syntax differences where Pyodide accepts constructs that aren't legal in native Python. - On n8n cloud, the Python option for the Code node doesn't allow users to import any Python libraries — whether from the standard library or third-party packages. Self-hosting users can find setup instructions to include external libraries here. In the long term, the n8n team is committed to allowing users to securely execute arbitrary Python code with any first- and third-party libraries using task runners.
Upgrading to native Python is a breaking change, so you may need to adjust your Python scripts to use the native Python runner.
Coding in n8n
There are two places where you can use code in n8n: the Code node and the expressions editor. When using either area, there are some key concepts you need to know, as well as some built-in methods and variables to help with common tasks.
Key concepts
When working with the Code node, you need to understand the following concepts:
- Data structure: understand the data you receive in the Code node, and requirements for outputting data from the node.
- Item linking: learn how data items work, and how to link to items from previous nodes. You need to handle item linking in your code when the number of input and output items doesn't match.
Built-in methods and variables
n8n includes built-in methods and variables. These provide support for:
- Accessing specific item data
- Accessing data about workflows, executions, and your n8n environment
- Convenience variables to help with data and time
Refer to Built-in methods and variables for more information.
Use AI in the Code node
Common issues
For common questions or issues and suggested solutions, refer to Common Issues.
官方原文和授权
本页来自 N8N 英文官方网站固定快照,并转换成 xueai 静态页面。内容以 N8N 持续更新的官方页面为准。