Skip to content
Latchkey

wasm-pack build --target: Build Rust to Wasm

wasm-pack build compiles a Rust crate to WebAssembly and runs wasm-bindgen to emit a ready-to-publish npm package.

wasm-pack is the standard path from a Rust crate to an npm-consumable wasm package. The --target you pick decides the glue code shape for bundlers, browsers, or Node.

What it does

wasm-pack build runs cargo build --target wasm32-unknown-unknown, then wasm-bindgen to produce JS/TS bindings, then optionally wasm-opt. Output lands in pkg/ with a package.json.

Common usage

Terminal
wasm-pack build --target web --release
wasm-pack build --target bundler
wasm-pack build --target nodejs --out-dir dist
wasm-pack build --dev --target web

Options

FlagWhat it does
--target <t>Output flavor: bundler (default), web, nodejs, no-modules, deno
--release / --dev / --profilingCargo profile to build with
--out-dir <dir>Where to write the generated package (default pkg)
--out-name <name>Base name for the generated files
--scope <scope>npm scope for the generated package.json

In CI

Install the wasm target first (rustup target add wasm32-unknown-unknown); wasm-pack does not add it for you on every setup. Cache ~/.cargo and target/. Pick --target web for native ES modules, bundler for webpack/Vite, nodejs for require().

Common errors in CI

"wasm-pack: command not found" means the tool is not installed (use cargo install wasm-pack or the setup action). "rust wasm32-unknown-unknown target not found" is fixed by rustup target add wasm32-unknown-unknown. "wasm-opt: command not found" means Binaryen is missing; install it or pass --no-opt. A wasm-bindgen "schema version mismatch" means the CLI and the wasm-bindgen crate versions differ.

Related guides

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