LogoFreestyle

Deno

Add Deno runtime to your VMs.

The Deno integration installs Deno, a secure JavaScript/TypeScript runtime, and provides helper methods for running code and installing packages from both npm and JSR.

Installation

npm install @freestyle-sh/with-deno freestyle

Usage

import { freestyle, VmSpec } from "freestyle";
import { VmDeno } from "@freestyle-sh/with-deno";

const { vm } = await freestyle.vms.create(
  new VmSpec().with("deno", new VmDeno()),
);

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

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

Options

new VmDeno({
  version: "2.0.0", // Optional: specific Deno version (default: latest)
})
OptionTypeDefaultDescription
versionstringundefinedDeno version to install. If not specified, installs the latest version.

API

vm.deno.runCode({ code })

Executes JavaScript/TypeScript code using deno eval.

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

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

You can also pass arguments, environment variables, and a working directory:

const res = await vm.deno.runCode({
  code: `console.log(JSON.stringify(Deno.args));`,
  argv: ["hello", "world"],
  env: { MY_VAR: "value" },
  workdir: "/app",
});

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.deno.install(options?)

Installs packages using Deno. Bare package names are automatically prefixed with npm:. Use the jsr: prefix for JSR packages.

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

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

// Install npm packages (auto-prefixed with npm:)
await vm.deno.install({ deps: ["lodash-es", "express"] });

// Install JSR packages (use jsr: prefix)
await vm.deno.install({ deps: ["jsr:@std/path", "jsr:@std/fs"] });

// Install with specific versions
await vm.deno.install({ deps: { "lodash-es": "^4.0.0" } });

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

// Install globally
await vm.deno.install({ global: true, deps: ["jsr:@std/cli"] });

Returns: Promise<InstallResult>

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

JSR Packages

Deno has native support for JSR (JavaScript Registry), which hosts TypeScript-first packages including the Deno standard library.

// Install @std/path from JSR
await vm.deno.install({ deps: ["jsr:@std/path"] });

// Use it in code
const res = await vm.deno.runCode({
  code: `
    import * as path from "jsr:@std/path";
    console.log(JSON.stringify({
      join: path.join("foo", "bar", "baz"),
      basename: path.basename("/home/user/file.txt"),
    }));
  `,
});

console.log(res.result);
// { join: "foo/bar/baz", basename: "file.txt" }

Example: Install and Use npm Packages

import { freestyle, VmSpec } from "freestyle";
import { VmDeno } from "@freestyle-sh/with-deno";

const { vm } = await freestyle.vms.create(
  new VmSpec().with("deno", new VmDeno()),
);

// Install lodash-es
await vm.deno.install({ deps: ["lodash-es"] });

// Use it
const res = await vm.deno.runCode({
  code: `
    import _ from "npm:lodash-es";
    const result = {
      chunk: _.chunk([1, 2, 3, 4, 5, 6], 2),
      uniq: _.uniq([1, 1, 2, 2, 3]),
      sum: _.sum([1, 2, 3, 4]),
    };
    console.log(JSON.stringify(result));
  `,
});

console.log(res.result);
// { chunk: [[1,2],[3,4],[5,6]], uniq: [1,2,3], sum: 10 }

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.