Nodejs Runner avatar

Nodejs Runner

Under maintenance
Try for free

No credit card required

Go to Store
This Actor is under maintenance.

This Actor may be unreliable while under maintenance. Would you like to try a similar Actor instead?

See alternative Actors
Nodejs Runner

Nodejs Runner

martin.forejt/nodejs-runner
Try for free

No credit card required

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.

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:

1{
2  "code": "<your JavaScript code here>"
3}

Input Fields

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

Example Input

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

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

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

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

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

Sample Output Logs

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

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:

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

Using API:

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

Actor Metrics

  • 1 monthly user

  • 2 stars

  • >99% runs succeeded

  • Created in Dec 2024

  • Modified a day ago