Nodejs Runner avatar
Nodejs Runner

Pricing

Pay per usage

Go to Apify Store
Nodejs Runner

Nodejs Runner

This Actor allows you to quickly run arbitrary JavaScript code in a real Node.js environment, making it ideal for testing, debugging, or executing small scripts without setting up a local Node.js instance. The actor spawns a separate Node.js process to run the provided code and captures the logs.

Pricing

Pay per usage

Rating

5.0

(1)

Developer

Martin Forejt

Martin Forejt

Maintained by Community

Actor stats

5

Bookmarked

102

Total users

10

Monthly active users

34 minutes ago

Last modified

Categories

Share

Node.js Code Runner

This Apify actor allows you to quickly run arbitrary JavaScript code in a real Node.js environment, making it ideal for testing, debugging, or executing small scripts without setting up a local Node.js instance. The actor spawns a separate Node.js process to run the provided code, captures the logs (stdout and stderr), and saves them to Apify's Key-Value Store for later retrieval.

Use Case

  • Quick Testing: Instantly run JavaScript snippets in a Node.js environment.
  • Dynamic Execution: Dynamically execute and debug code from your workflows.
  • Environment Replication: Run code in an isolated environment that matches Node.js runtime configurations.

How It Works

  1. The actor receives the JavaScript code to run via its input.
  2. It spawns a child Node.js process to execute the code.
  3. Captures all logs (stdout and stderr) during execution.
  4. Saves the logs to the Apify Key-Value Store for easy access.

Input

The input should be a JSON object with the following structure:

{
"code": "<your JavaScript code here>"
}

Input Fields

  • code (string, required): The JavaScript code to execute.

Example Input

{
"code": "console.log('Hello, world!');"
}

Output

The output logs from the executed code are saved in the Key-Value Store under the key LOGS. The logs include both stdout and stderr streams, as well as the exit code of the process.

Example Logs

[
"STDOUT: Hello, world!\n",
"Child process exited with code 0"
]

Example Usage

Running the Actor

  1. Deploy the actor to the Apify platform.
  2. Provide the JavaScript code as input.
  3. Run the actor.

Sample Input

{
"code": "console.log('The current date is:', new Date().toISOString());"
}

Sample Output Logs

[
"STDOUT: The current date is: 2024-12-12T10:00:00.000Z\n",
"Child process exited with code 0"
]

Development Notes

Key Features

  • Logs Collection: Automatically collects and stores all logs for easy retrieval.
  • Error Handling: Captures errors during execution and stores them for debugging.
  • Environment Replication: Runs in a real Node.js process, ensuring consistent behavior with a local environment.

How to Retrieve Logs

After the actor runs, you can fetch the logs using the Apify SDK or API:

In Apify Console:

In the actor run details, navigate to the Key-Value Store section and find the LOGS key.

Using SDK:

const logs = await Actor.getValue('LOGS');
console.log('Retrieved Logs:', logs);

Using API:

$curl -X GET "https://api.apify.com/v2/key-value-stores/{storeId}/records/LOGS"