Vibengine
Code Interpreter

JavaScript Execution

Execute JavaScript and Node.js code in the sandbox

JavaScript Execution

Run JavaScript and Node.js code inside the Code Interpreter sandbox. The runtime is Node.js 20 with full access to the npm ecosystem. You can install packages, use async/await, and work with JSON data natively.

Basic Execution

Specify the language as javascript when running code.

import { CodeInterpreter } from "vibengine";

const sandbox = await CodeInterpreter.create({
  apiKey: process.env.VE_API_KEY,
});

const execution = await sandbox.runCode(
  `
const greeting = "Hello from Node.js";
console.log(greeting);
console.log(process.version);
`,
  { language: "javascript" }
);

console.log(execution.text);
// "Hello from Node.js"
// "v20.x.x"

await sandbox.kill();
import os
from vibengine import CodeInterpreter

sandbox = CodeInterpreter.create(api_key=os.environ["VE_API_KEY"])

execution = sandbox.run_code("""
const greeting = "Hello from Node.js";
console.log(greeting);
console.log(process.version);
""", language="javascript")

print(execution.text)
# "Hello from Node.js"
# "v20.x.x"

sandbox.kill()

By default, runCode / run_code executes Python. Pass language: "javascript" (JS SDK) or language="javascript" (Python SDK) to run JavaScript instead.

Installing npm Packages

Install packages from npm at runtime using dynamic imports or the built-in package installation.

const execution = await sandbox.runCode(
  `
const _ = require("lodash");

const data = [
  { name: "Alice", department: "Engineering" },
  { name: "Bob", department: "Marketing" },
  { name: "Charlie", department: "Engineering" },
  { name: "Diana", department: "Marketing" },
];

const grouped = _.groupBy(data, "department");
console.log(JSON.stringify(grouped, null, 2));
`,
  { language: "javascript" }
);

console.log(execution.text);
execution = sandbox.run_code("""
const _ = require("lodash");

const data = [
  { name: "Alice", department: "Engineering" },
  { name: "Bob", department: "Marketing" },
  { name: "Charlie", department: "Engineering" },
  { name: "Diana", department: "Marketing" },
];

const grouped = _.groupBy(data, "department");
console.log(JSON.stringify(grouped, null, 2));
""", language="javascript")

print(execution.text)

Async/Await Support

Top-level await is supported out of the box. Write asynchronous code naturally without wrapping it in an async function.

const execution = await sandbox.runCode(
  `
async function fetchData(url) {
  const response = await fetch(url);
  return response.json();
}

const data = await fetchData("https://jsonplaceholder.typicode.com/todos/1");
console.log("Title:", data.title);
console.log("Completed:", data.completed);
`,
  { language: "javascript" }
);

console.log(execution.text);
// "Title: delectus aut autem"
// "Completed: false"
execution = sandbox.run_code("""
async function fetchData(url) {
  const response = await fetch(url);
  return response.json();
}

const data = await fetchData("https://jsonplaceholder.typicode.com/todos/1");
console.log("Title:", data.title);
console.log("Completed:", data.completed);
""", language="javascript")

print(execution.text)
# "Title: delectus aut autem"
# "Completed: false"

Working with JSON Data

JavaScript is a natural fit for JSON manipulation. Parse, transform, and output structured data easily.

const execution = await sandbox.runCode(
  `
const sales = [
  { product: "Widget A", quantity: 150, price: 9.99 },
  { product: "Widget B", quantity: 85, price: 24.99 },
  { product: "Widget C", quantity: 210, price: 4.99 },
];

const report = sales.map((item) => ({
  product: item.product,
  revenue: (item.quantity * item.price).toFixed(2),
}));

const totalRevenue = report.reduce((sum, r) => sum + parseFloat(r.revenue), 0);

console.log("Sales Report:");
report.forEach((r) => console.log(\`  \${r.product}: $\${r.revenue}\`));
console.log(\`Total Revenue: $\${totalRevenue.toFixed(2)}\`);
`,
  { language: "javascript" }
);

console.log(execution.text);
// Sales Report:
//   Widget A: $1498.50
//   Widget B: $2124.15
//   Widget C: $1047.90
// Total Revenue: $4670.55
execution = sandbox.run_code("""
const sales = [
  { product: "Widget A", quantity: 150, price: 9.99 },
  { product: "Widget B", quantity: 85, price: 24.99 },
  { product: "Widget C", quantity: 210, price: 4.99 },
];

const report = sales.map((item) => ({
  product: item.product,
  revenue: (item.quantity * item.price).toFixed(2),
}));

const totalRevenue = report.reduce((sum, r) => sum + parseFloat(r.revenue), 0);

console.log("Sales Report:");
report.forEach((r) => console.log(`  ${r.product}: $${r.revenue}`));
console.log(`Total Revenue: $${totalRevenue.toFixed(2)}`);
""", language="javascript")

print(execution.text)

Error Handling

JavaScript errors are captured the same way as Python errors.

const execution = await sandbox.runCode(
  `
const obj = undefined;
console.log(obj.property);
`,
  { language: "javascript" }
);

if (execution.error) {
  console.log(execution.error.name);    // "TypeError"
  console.log(execution.error.message); // "Cannot read properties of undefined"
}
execution = sandbox.run_code("""
const obj = undefined;
console.log(obj.property);
""", language="javascript")

if execution.error:
    print(execution.error.name)    # "TypeError"
    print(execution.error.message) # "Cannot read properties of undefined"

JavaScript execution does not share state with Python execution. Each language has its own isolated runtime within the sandbox.

On this page