Skip to content
Latchkey

wasm-bindgen: Generate JS Bindings for Rust Wasm

wasm-bindgen post-processes a Rust wasm binary to emit JavaScript and TypeScript bindings that call into it.

wasm-pack wraps it, but you can call wasm-bindgen directly. The single most important CI rule is that the CLI version must match the wasm-bindgen crate version in your Cargo.lock.

What it does

wasm-bindgen reads the wasm produced by cargo build --target wasm32-unknown-unknown, generates JS/TS glue plus a trimmed wasm, and writes them to --out-dir. The --target decides the module style (web, bundler, nodejs, no-modules, deno).

Common usage

Terminal
cargo build --target wasm32-unknown-unknown --release
wasm-bindgen target/wasm32-unknown-unknown/release/app.wasm \
  --out-dir pkg --target web
wasm-bindgen app.wasm --out-dir pkg --target nodejs --typescript

Options

FlagWhat it does
--out-dir <dir>Where to write generated JS/TS and wasm
--target <t>Module style: web, bundler, nodejs, no-modules, deno
--out-name <name>Base name for the generated files
--typescript / --no-typescriptToggle .d.ts generation
--weak-refs / --reference-typesEnable runtime features when supported

In CI

Pin the CLI to the crate version. Installing wasm-bindgen-cli without a version pulls the latest, which often differs from the crate in Cargo.lock and breaks the build. Install with cargo install wasm-bindgen-cli --version <X> matching the crate, or let wasm-pack manage it.

Common errors in CI

The classic is "it looks like the Rust project used to create this wasm file was linked against version X of wasm-bindgen but this binary is version Y"; pin the CLI to the crate version. "rust wasm32-unknown-unknown target not found" is fixed by rustup target add wasm32-unknown-unknown. A missing binary gives "wasm-bindgen: command not found".

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →