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
Downloadqjs-wasi.wasm from a
quickjs-ng release and
point the runtime’s home, or MIRAGE_QUICKJS_HOME, at its directory.
- Python
- TypeScript
Usage
Declare anin_process machine that runs QuickJS to run node on it once its engine is found:
- Python
- TypeScript
- YAML
Memory
A run holds no more than its workspace’smax_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.
Example: append to a workspace file
Example: append to a workspace file
Limitations
Limitations
File objects
readAsString(), read(), getline(), seek(), tell(), tello(),
eof(), puts(), write(), printf(), flush() and close().
Example: write, seek and read a file
Example: write, seek and read a file
Limitations
Limitations
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.
Example: read a session environment variable
Example: read a session environment variable
Limitations
Limitations
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.
Example: forward piped input
Example: forward piped input
Limitations
Limitations
os
readdir(), stat(), mkdir(), remove(), utimes(), rename(),
getcwd(), chdir(), open(), read(), write(), seek(), close(),
sleep(), setTimeout() and platform.
Example: list a workspace directory
Example: list a workspace directory
Limitations
Limitations
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.
Example: print the current time
Example: print the current time
Limitations
Limitations
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.
Example: print a result
Example: print a result
Limitations
Limitations
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.
Example: filter a JSON file
Example: filter a JSON file
Example: read piped input by line
Example: read piped input by line
Support
Support
Limitations
Limitations
Runtime globals
scriptArgs, the invocation name and arguments, and atob(), btoa() and
queueMicrotask().
Example: read arguments and encode text
Example: read arguments and encode text
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.mjsfile runs as an ES module;.jsand-erun as classic scripts, and-mforces module mode.console.logandprintadd a newline;std.out.putsandstd.err.putswrite raw.- An error in async code fails the run.
- QuickJS cannot reach host files or the network.
command_limitsbound its time and output. Run untrusted or heavy code on a machine.