Task Runners (JS & Python)
Relevant source files
The following files were used as context for generating this wiki page:
- packages/@n8n/task-runner/package.json
- packages/frontend/editor-ui/src/constants.ts
- packages/nodes-base/nodes/Function/Function.node.json
- packages/nodes-base/nodes/HtmlExtract/test/workflow.dotNotation.json
- packages/frontend/editor-ui/src/utils/templates/samples/agents/task_management_agent_with_google_sheets.json
The n8n platform provides robust capabilities for executing custom code within workflows, allowing for advanced data manipulation, custom logic, and integration patterns that go beyond the standard node functionality. This is primarily facilitated by a system of isolated task runners that execute user-provided code securely.
The core of this system for JavaScript is the Code node, which leverages the @n8n/task-runner package to run code in a separate process. This architecture ensures that user code does not interfere with the main n8n application, enhancing stability and security. While this document focuses on the JavaScript execution capabilities detailed in the provided sources, it's important to note that information regarding a Python task runner is not available within these files.
Architecture Overview
The execution of custom code involves several components: the user-facing Code node, the main workflow engine, and an isolated task runner process. When a workflow executes a Code node, the engine sends the code and its input data to the dedicated task runner, which processes the request and returns the output.
The following diagram illustrates the conceptual data flow for a JavaScript Code node execution.
This diagram shows that the Code Node's logic is not run in the main process. Instead, it is delegated to a separate @n8n/task-runner process. The use of the ws library in the task runner's dependencies suggests this communication happens over WebSockets.
Sources: packages/@n8n/task-runner/package.json, packages/nodes-base/nodes/Function/Function.node.json
The Code Node
The primary interface for executing custom JavaScript is the Code node (internally n8n-nodes-base.code or n8n-nodes-base.function). It allows users to write and run arbitrary JavaScript code as a step in a workflow.
Sources: packages/frontend/editor-ui/src/constants.ts:130, packages/nodes-base/nodes/Function/Function.node.json:2
Purpose and Configuration
According to its definition, the Code node is designed to "execute JavaScript in your workflow" and is particularly useful for "data transformation where you want to manipulate the number of items being outputted by the node".
The node has several aliases, including Code, Javascript, Custom Code, and Script.
Sources: packages/nodes-base/nodes/Function/Function.node.json:4-5, packages/nodes-base/nodes/Function/Function.node.json:48
A concrete example from a test workflow shows the node's structure, where the JavaScript to be executed is passed in the jsCode parameter.
{
"parameters": {
"jsCode": "return [\\n {\\n \"headers\": {\\n \"host\": \"n8n.mydomain.live\",\\n \"user-agent\": \"axios/1.2.1\",\\n \"content-length\": \"4405\",\\n \"accept\": \"application/json, text/plain, */*\",\\n \"accept-encoding\": \"gzip, compress, deflate, br\",\\n \"content-type\": \"application/json\",\\n \"x-forwarded-for\": \"85.203.36.52\",\\n \"x-forwarded-host\": \"n8n.mydomain.live\",\\n \"x-forwarded-port\": \"443\",\\n \"x-forwarded-proto\": \"https\",\\n \"x-forwarded-server\": \"8bd0a4ed6c1a\",\\n \"x-real-ip\": \"85.203.36.52\"\\n },\\n \"params\": {},\\n \"query\": {},\\n \"body\": {\\n \"html\": \"<html><head></head><body bgcolor=\\\"FFFFFf\\\" link=\\\"006666\\\" alink=\\\"8B4513\\\" vlink=\\\"006666\\\"><hmtl> <title>webpage1</title> <table width=\\\"75%\\\" align=\\\"center\\\"> <tbody><tr> <td> <div align=\\\"center\\\"><h1>STARTING . . . </h1></div> <div align=\\\"justify\\\"><p>There are lots of ways to create web pages using already coded programmes. These lessons will teach you how to use the underlying HyperText Markup Language - HTML. <br> </p><p>HTML isn't computer code, but is a language that uses US English to enable texts (words, images, sounds) to be inserted and formatting such as colo(u)r and centre/ering to be written in. The process is fairly simple; the main difficulties often lie in small mistakes - if you slip up while word processing your reader may pick up your typos, but the page will still be legible. However, if your HTML is inaccurate the page may not appear - writing web pages is, at the least, very good practice for proof reading!</p> <p>Learning HTML will enable you to: </p><ul> <li>create your own simple pages </li><li>read and appreciate pages created by others </li><li>develop an understanding of the creative and literary implications of web-texts </li><li>have the confidence to branch out into more complex web design </li></ul><p></p> <p>A HTML web page is made up of tags. Tags are placed in brackets like this <b>< tag > </b>. A tag tells the browser how to display information. Most tags need to be opened < tag > and closed < /tag >. </p><p> To make a simple web page you need to know only four tags: </p><ul> <li>< HTML > tells the browser your page is written in HTML format </li><li>< HEAD > this is a kind of preface of vital information that doesn't appear on the screen. </li><li>< TITLE >Write the title of the web page here - this is the information that viewers see on the upper bar of their screen. (I've given this page the title 'webpage1'). </li><li>< BODY >This is where you put the content of your page, the words and pictures that people read on the screen. </li></ul> <p>All these tags need to be closed. </p><h4>EXERCISE</h4> <p>Write a simple web page.</p> <p> Copy out exactly the HTML below, using a WP program such as Notepad.<br> Information in <i>italics</i> indicates where you can insert your own text, other information is HTML and needs to be exact. However, make sure there are no spaces between the tag brackets and the text inside.<br> (Find Notepad by going to the START menu PROGRAMS ACCESSORIES NOTEPAD). </p><p> < HTML ><br> < HEAD ><br> < TITLE ><i> title of page</i>< /TITLE ><br> < /HEAD ><br> < BODY><br> <i> write what you like here: 'my first web page', or a piece about what you are reading, or a few thoughts on the course, or copy out a few words from a book or cornflake packet. Just type in your words using no extras such as bold, or italics, as these have special HTML tags, although you may use upper and lower case letters and single spaces. </i><br> < /BODY ><br> < /HTML ><br> </p><p>Save the file as 'first.html' (ie. call the file anything at all) It's useful if you start a folder - just as you would for word-processing - and call it something like WEBPAGES, and put your first.html file in the folder. </p><p>NOW - open your browser.<br> On Netscape the process is: <br> Top menu; FILE OPEN PAGE CHOOSE FILE<br> Click on your WEBPAGES folder FIRST file<br> Click 'open' and your page should appear. </p><p>On Internet Explorer: <br> Top menu; FILE OPEN BROWSE <br> Click on your WEBPAGES folder FIRST file<br> Click 'open' and your page should appear.<br> </p><p>If the page doesn't open, go back over your notepad typing and make sure that all the HTML tags are correct. Check there are no spaces between tags and internal text; check that all tags are closed; check that you haven't written < HTLM > or < BDDY >. Your page will work eventually. </p><p> Make another page. Call it somethingdifferent.html and place it in the same WEBPAGES folder as detailed above. </p><p>start formatting in <a href=\\\"webpage2.html\\\">lesson two</a> <br><a href=\\\"col3.html\\\">back to wws index</a> </p> <p></p> </div> </td> </tr> </tbody></table> </hmtl></body></html>\"\\n }\\n }\\n];"
},
"id": "88a269af-c315-491c-b897-578cfb81268a",
"name": "Mock data",
"type": "n8n-nodes-base.code",
"typeVersion": 1,
"position": [520, 580]
}
Sources: packages/nodes-base/nodes/HtmlExtract/test/workflow.dotNotation.json:11-40
JavaScript Task Runner Package
The @n8n/task-runner package is responsible for handling the execution of JavaScript code. Its dependencies provide insight into its functionality.
| Dependency | Version | Inferred Purpose |
|---|---|---|
ws | ^8.18.0 | Enables WebSocket communication, likely with a central task broker. |
acorn | 8.14.0 | A modern JavaScript parser, likely used for code validation or analysis before execution. |
acorn-walk | 8.3.4 | A utility to walk the Abstract Syntax Tree (AST) generated by Acorn. |
n8n-core | workspace:* | Provides access to core n8n functionalities. |
n8n-workflow | workspace:* | Provides access to workflow-related data structures and types. |
Sources: packages/@n8n/task-runner/package.json:30-40
Python Task Runner
While the topic for this page includes Python, the provided source files do not contain any information about a Python-specific task runner. The existing files, such as the package.json for @n8n/task-runner and the Code node definition, are exclusively related to JavaScript execution. There is no evidence of a Python equivalent in the analyzed sources.
Embedded Expressions
In addition to the full-featured Code node, n8n also supports JavaScript-based expressions directly within the parameters of any node. This allows for dynamic configuration of values at execution time.
These expressions are identifiable by the ={{ ... }} syntax. An example from a workflow template shows this in action:
"columns": {
"mappingMode": "defineBelow",
"value": {
"Task": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Task', ``, 'string') }}",
"Description": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Description', ``, 'string') }}",
"Deadline": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Deadline', ``, 'string') }}",
"Status": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Status', ``, 'string') }}"
},
The expression context provides several helper variables to access workflow data.
| Variable | Description |
|---|---|
$binary | Access binary data of an item. |
$data | Access the complete incoming data of the node. |
$env | Access environment variables. |
$item | A helper to simplify access to item data. |
$json | Access the JSON data of an item. |
$node | Access properties of the current node. |
$workflow | Access properties of the current workflow. |
Sources: packages/frontend/editor-ui/src/constants.ts:401-419