LogoFreestyle

Bun

Add Bun runtime to your VMs.

The Bun integration installs Bun, a fast JavaScript runtime and toolkit, and provides helper methods for running code and installing packages.

Installation

npm install @freestyle-sh/with-bun freestyle-sandboxes

Usage

import { freestyle } from "freestyle-sandboxes";
import { VmBun } from "@freestyle-sh/with-bun";

const { vm } = await freestyle.vms.create({
  with: {
    js: new VmBun(),
  },
});

const res = await vm.js.runCode({
  code: "console.log(JSON.stringify({ hello: 'world' }));"
});

console.log(res);
// { result: { hello: 'world' }, stdout: '{"hello":"world"}\n', statusCode: 0 }

Options

new VmBun({
  version: "1.1.0", // Optional: specific Bun version (default: latest)
})
OptionTypeDefaultDescription
versionstringundefinedBun version to install. If not specified, installs the latest version.

API

vm.js.runCode({ code })

Executes JavaScript/TypeScript code in the Bun runtime.

const res = await vm.js.runCode({
  code: `
    const data = { sum: 1 + 2, timestamp: Date.now() };
    console.log(JSON.stringify(data));
  `
});

console.log(res.result); // { sum: 3, timestamp: 1234567890 }

Returns: Promise<RunCodeResponse>

type RunCodeResponse<Result> = {
  result: Result;      // Parsed JSON from stdout (if valid JSON)
  stdout?: string;     // Raw stdout output
  stderr?: string;     // Raw stderr output
  statusCode?: number; // Exit code
};

vm.js.install(options?)

Installs packages using Bun.

// Install from package.json in current directory
await vm.js.install();

// Install from package.json in specific directory
await vm.js.install({ directory: "/app" });

// Install specific packages
await vm.js.install({ deps: ["lodash", "express"] });

// Install with specific versions
await vm.js.install({ deps: { "lodash": "^4.0.0", "express": "~5.0.0" } });

// Install as dev dependencies
await vm.js.install({ deps: ["typescript"], dev: true });

// Install globally
await vm.js.install({ global: true, deps: ["typescript"] });

Returns: Promise<InstallResult>

type InstallResult = {
  success: boolean;
  stdout?: string;
  stderr?: string;
};

Why Bun?

Bun is significantly faster than Node.js for many operations:

  • Faster startup - Bun starts up to 4x faster than Node.js
  • Faster installs - bun install is much faster than npm install
  • Built-in bundler - No need for webpack or esbuild
  • TypeScript support - Run .ts files directly without compilation

Example: Run Code and Get Result

import { freestyle } from "freestyle-sandboxes";
import { VmBun } from "@freestyle-sh/with-bun";

const { vm } = await freestyle.vms.create({
  with: {
    js: new VmBun(),
  },
});

// Run TypeScript code directly
const res = await vm.js.runCode({
  code: `
    interface User { name: string; age: number }
    const user: User = { name: "Alice", age: 30 };
    console.log(JSON.stringify(user));
  `
});

console.log(res.result); // { name: "Alice", age: 30 }

On this page

Freestyle AI

Documentation assistant

Experimental: AI responses may not always be accurate—please verify important details with the official documentation.

How can I help?

Ask me about Freestyle while you browse the docs.