Skip to main content
QuickJS runs node and js in both SDKs. It supports ES2023 for small scripts and pipe transforms, with access to workspace files under the same permissions as shell commands. Node.js’s most used APIs are laid over the engine: fs, path, process, Buffer, require, the timers and Node’s console. It has no npm packages and no network access. Both SDKs run the same engine, the WASI build of quickjs-ng, so a script answers the same in either.

Installation

Download qjs-wasi.wasm from a quickjs-ng release and point the runtime’s home, or MIRAGE_QUICKJS_HOME, at its directory.

Usage

Declare an in_process machine that runs QuickJS to run node on it once its engine is found:

Memory

A run holds no more than its workspace’s max_memory has left, 96 MB by default. A program that needs more gets InternalError: out of memory.

JavaScript compatibility

std and os are globals, as qjs --std gives them. Examples assume a writable /data mount. Mount modes and permissions still apply.

std.open()

std.open(path, mode, errorObj) returns null on failure and sets errorObj.errno. A read-only mount refuses modes that write. The modes are r, w, a and x, with the b and + modifiers; for example, rb combines r and b.

File objects

readAsString(), read(), getline(), seek(), tell(), tello(), eof(), puts(), write(), printf(), flush() and close().

std

getenv(), setenv(), getenviron(), exit(), loadFile(), writeFile(), printf(), fdopen(), strerror() and Error. Environment variables come from the Mirage session. std.loadFile() returns null when reading fails.

Standard streams

std.in receives the command’s piped input as it arrives, and std.in, std.out and std.err are descriptors 0 to 2.

os

readdir(), stat(), mkdir(), remove(), utimes(), rename(), getcwd(), chdir(), open(), read(), write(), seek(), close(), sleep(), setTimeout() and platform.

Date

Date.now() and new Date() start at the workspace’s system time when it sets one, and files the program writes are stamped from the same clock. performance.now() and os.now() measure elapsed time.

console and printing

console.log(), console.info() and console.debug() write to stdout, and console.error() and console.warn() to stderr. They format values as Node’s util.inspect() does and take util.format() placeholders such as %s, %d and %j. print() writes the engine’s own way.

Node.js APIs

require() returns these modules, with or without the node: prefix, and loads files by path: JavaScript as CommonJS, and JSON parsed. process, Buffer, TextEncoder, TextDecoder, global, the timers and a script’s __filename and __dirname are globals.

Runtime globals

scriptArgs, the invocation name and arguments, and atob(), btoa() and queueMicrotask().

Unavailable host APIs

Unavailable in this runtime.

Notes

  • -e <code> runs the argument, a file runs a mounted script, and with neither, piped input is the program. A .mjs file runs as an ES module; .js and -e run as classic scripts, and -m forces module mode.
  • console.log and print add a newline; std.out.puts and std.err.puts write raw.
  • An error in async code fails the run.
  • QuickJS cannot reach host files or the network. command_limits bound its time and output. Run untrusted or heavy code on a machine.