LinuxCommandLibrary

wasm-pack

TLDR

Build Rust project for WebAssembly

$ wasm-pack build
copy
Build for npm publishing
$ wasm-pack build --target bundler
copy
Build for web browser
$ wasm-pack build --target web
copy
Build for Node.js
$ wasm-pack build --target nodejs
copy
Build with custom output directory
$ wasm-pack build --out-dir [dist]
copy
Build in development mode
$ wasm-pack build --dev
copy
Publish to npm
$ wasm-pack publish
copy
Create new project from template
$ wasm-pack new [project-name]
copy

SYNOPSIS

wasm-pack [options] command [args]

DESCRIPTION

wasm-pack is a build tool for Rust-generated WebAssembly. It compiles Rust code to WebAssembly, generates JavaScript bindings using wasm-bindgen, and produces npm-ready packages.
The build process compiles Rust to the wasm32-unknown-unknown target, runs wasm-bindgen to generate JavaScript glue code, and optionally runs wasm-opt for optimization. The output includes .wasm files, TypeScript definitions, and package.json for npm.
Different targets generate different JavaScript module formats: bundler for webpack/rollup, web for ES modules in browsers, nodejs for CommonJS, and no-modules for script tags.
Install via cargo: `cargo install wasm-pack`

PARAMETERS

--target target

Output target: bundler (default), web, nodejs, deno, no-modules.
--out-dir dir
Output directory (default: pkg).
--out-name name
Output file basename.
--dev
Development build with debug info.
--profiling
Release build with debug info.
--release
Optimized release build (default).
--scope scope
npm scope for publishing.
--mode mode
Build mode: normal, no-install, force.
--verbose
Enable verbose output.
--quiet
Suppress output.
-h, --help
Display help.
-V, --version
Display version.

COMMANDS

build: Compile to WebAssembly and generate bindings.
new: Create new project from template.
pack: Create tarball for npm publishing.
publish: Publish package to npm registry.
login: Login to npm registry.
test: Run wasm-pack tests.

CAVEATS

Requires Rust toolchain with wasm32-unknown-unknown target. First build may download wasm-bindgen and wasm-opt. Some Rust crates may not compile to WebAssembly. WASI support requires different tooling.

HISTORY

wasm-pack was created by the Rust and WebAssembly Working Group to simplify publishing Rust-generated WebAssembly to npm. It streamlines the workflow of compiling, binding generation, and package publishing into a single tool.

SEE ALSO

cargo(1), wasm-bindgen(1), wasmtime(1), npm(1)

Copied to clipboard