Skip to main content

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) and output.json (write)
  • Package Manager: npm (packages specified in the Packages input)

Inputs

InputTypeDescriptionDefault
RunEventFires when the node starts running-
CodeCodeThe JavaScript code to execute. Read inputs from inputs.json and write results to output.json.See default code below
PackagesTextComma-separated list of npm packages to install (e.g., lodash@1.1.2, axios, moment). Supports version specifiers.-
Prebuild ScriptCodeShell commands to run before the main code executes (e.g., installing additional dependencies or setup).-
InputsDataData object passed to the code execution, available in inputs.json.{}

Outputs

OutputTypeDescription
DoneEventFires when the node has finished executing
ResultDataObject 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

  1. The node receives the run event and collects all inputs
  2. If packages are specified, they are installed in the container
  3. The prebuild script runs (if provided)
  4. The main code executes with access to:
    • inputs.json containing the data from the Inputs socket
    • Node.js built-in modules (fs, path, crypto, etc.)
    • Any installed npm packages
  5. The code must write results to output.json to return data
  6. The node captures stdout/stderr and returns them in the result

Error Handling

If the code execution fails:

  • The result output will contain an error object with message, stdout, and stderr
  • If package installation fails, buildLogs will be included
  • The done event still fires, but check the result.error field 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..."
}
}