Run JavaScript Code
Controlled node
Overview
The Run JavaScript Code node executes JavaScript code in a serverless container environment. This node is useful for running custom logic, data transformations, or integrating with external APIs using Node.js. The code runs in an isolated environment with access to the Node.js standard library and any specified npm packages.
The node reads input data from inputs.json and expects results to be written to output.json. Standard output (stdout) and standard error (stderr) are captured and returned for debugging purposes.
Execution Environment
- Runtime: Node.js (serverless container)
- Timeout: 10 minutes (600 seconds)
- Max Retries: 600 polling attempts with 1-2 second delays
- File System: Access to
inputs.json(read) andoutput.json(write) - Package Manager: npm (packages specified in the Packages input)
Inputs
| Input | Type | Description | Default |
|---|---|---|---|
| Run | Event | Fires when the node starts running | - |
| Code | Code | The JavaScript code to execute. Read inputs from inputs.json and write results to output.json. | See default code below |
| Packages | Text | Comma-separated list of npm packages to install (e.g., lodash@1.1.2, axios, moment). Supports version specifiers. | - |
| Prebuild Script | Code | Shell commands to run before the main code executes (e.g., installing additional dependencies or setup). | - |
| Inputs | Data | Data object passed to the code execution, available in inputs.json. | {} |
Outputs
| Output | Type | Description |
|---|---|---|
| Done | Event | Fires when the node has finished executing |
| Result | Data | Object containing output (result from output.json), stdout (console logs), and stderr (error logs). If execution fails, contains error object with message, stdout, stderr, and optionally buildLogs. |
Runtime Behavior and Defaults
Default Code
When the node is created, it includes the following default code:
// Read inputs from inputs.json
const fs = require('fs');
const inputs = JSON.parse(fs.readFileSync('inputs.json', 'utf8'));
// Your code here
const result = {
message: 'Hello from JavaScript!',
data: inputs
};
// Write result to output.json (this becomes the node's output)
fs.writeFileSync('output.json', JSON.stringify(result));
// stdout is for logging only (visible in logs, not returned as output)
console.log('Processing completed successfully');
Execution Flow
- The node receives the
runevent and collects all inputs - If packages are specified, they are installed in the container
- The prebuild script runs (if provided)
- The main code executes with access to:
inputs.jsoncontaining the data from the Inputs socket- Node.js built-in modules (
fs,path,crypto, etc.) - Any installed npm packages
- The code must write results to
output.jsonto return data - The node captures stdout/stderr and returns them in the result
Error Handling
If the code execution fails:
- The
resultoutput will contain anerrorobject withmessage,stdout, andstderr - If package installation fails,
buildLogswill be included - The
doneevent still fires, but check theresult.errorfield for error details
Package Installation
Packages are installed using npm. You can specify:
- Package names:
lodash,axios - Versions:
lodash@4.17.21 - Multiple packages separated by commas:
lodash, axios, moment
Example
Basic Usage
Inputs:
- Code:
const fs = require('fs');
const inputs = JSON.parse(fs.readFileSync('inputs.json', 'utf8'));
// Process the input data
const processed = inputs.data.map(item => item * 2);
// Write result
fs.writeFileSync('output.json', JSON.stringify({
doubledValues: processed,
count: processed.length
}));
console.log('Processing complete');
- Inputs (data socket):
{ "data": [1, 2, 3, 4, 5] }
Result Output:
{
"output": {
"doubledValues": [2, 4, 6, 8, 10],
"count": 5
},
"stdout": "Processing complete\n",
"stderr": ""
}
Using External Packages
Inputs:
- Packages:
axios, lodash - Code:
const fs = require('fs');
const axios = require('axios');
const _ = require('lodash');
const inputs = JSON.parse(fs.readFileSync('inputs.json', 'utf8'));
async function fetchData() {
try {
const response = await axios.get(inputs.url);
const sorted = _.sortBy(response.data, 'name');
fs.writeFileSync('output.json', JSON.stringify({
items: sorted,
total: sorted.length
}));
console.log('Data fetched and sorted');
} catch (error) {
console.error('Error:', error.message);
// Still write output on error
fs.writeFileSync('output.json', JSON.stringify({ error: error.message }));
}
}
fetchData();
- Inputs:
{ "url": "https://api.example.com/data" }
Result Output:
{
"output": {
"items": [...],
"total": 50
},
"stdout": "Data fetched and sorted\n",
"stderr": ""
}
Error Handling Example
If the code throws an error or times out:
Result Output:
{
"error": {
"message": "Request failed with status code 404",
"stdout": "",
"stderr": "Error: Request failed with status code 404\n at ...stack trace..."
}
}