Wasm in Node.js, Deno and Bun
Bun supports WebAssembly like Node and has experimental WASI support.
Use cases: native-speed npm packages without native addons (e.g., esbuild-wasm, @swc/wasm, sql.js, sharp alternatives, @resvg/resvg-wasm), portable CLI tools, sandboxed plugins.
// Node.js (ESM)
import { readFile } from "node:fs/promises";
const bytes = await readFile(new URL("./add.wasm", import.meta.url));
const { instance } = await WebAssembly.instantiate(bytes, {});
console.log(instance.exports.add(2, 3));
// Node.js WASI (experimental module: node:wasi)
import { WASI } from "node:wasi";
import { argv, env } from "node:process";
const wasi = new WASI({ version: "preview1", args: argv, env, preopens: { "/sandbox": "./data" } });
const wasiMod = await WebAssembly.compile(await readFile("./app.wasm"));
const wasiInst = await WebAssembly.instantiate(wasiMod, wasi.getImportObject());
wasi.start(wasiInst);// Deno: Wasm can be imported directly (Deno 2.1+ supports Wasm module imports)
import { add } from "./add.wasm";
console.log(add(1, 2));