Rust wasm32 Build Failures - Native Deps & getrandom in CI
A build for a wasm32 target failed because a dependency relies on native code or OS facilities that don’t exist in wasm - the classic being getrandom having no entropy source on wasm32-unknown-unknown without the js backend enabled.
What this error means
A --target wasm32-unknown-unknown (or wasm32-wasi) build fails with a compile error from a dependency about an unsupported target, missing OS APIs, or getrandom not being supported. The same crate builds fine for a native target.
error: the wasm32-unknown-unknown targets are not supported by default; you may
need to enable the "js" feature. For more information see:
https://docs.rs/getrandom/#webassembly-support
--> getrandom/src/lib.rsCommon causes
A dependency needs a wasm-specific backend
getrandom (pulled in by rand, uuid, etc.) has no entropy source on bare wasm32-unknown-unknown. It needs the js feature (browser) or a custom backend, or the build fails.
Native-only code in the dependency graph
A crate using C bindings, threads, filesystem, or other OS facilities not available on wasm can’t compile for the target unless those parts are feature-gated off.
How to fix it
Enable the wasm backend for getrandom
For browser-targeted wasm, turn on the js feature so getrandom has an entropy source.
[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"] }Gate native dependencies off for wasm
- Use a
cfg(not(target_arch = "wasm32"))target table for native-only crates. - Disable default features that pull in OS/native code on the wasm target.
- Use
wasm32-wasiinstead ofunknown-unknownwhen you need a fuller std/OS surface.
How to prevent it
- Configure
getrandom/RNG backends per the wasm target before building. - Target-gate native-only dependencies with
cfg(target_arch = ...). - Build the wasm target in CI so unsupported deps surface on every PR.