You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I am trying to compile my program into WebAssembly using wasm-bindgen, but I got the following errors:
error: the wasm32-unknown-unknown target is not supported by default, you may need to enable the "js" feature. For more information see: https://docs.rs/getrandom/#webassembly-support
--> /Users/xxx/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.7/src/lib.rs:235:9
|
235 | / compile_error!("the wasm32-unknown-unknown target is not supported by \
236 | | default, you may need to enable the \"js\" feature. \
237 | | For more information see: \
238 | | https://docs.rs/getrandom/#webassembly-support");
| |________________________________________________________________________^
error[E0433]: failed to resolve: use of undeclared crate or module `imp`
--> /Users/xxx/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.7/src/lib.rs:262:5
|
262 | imp::getrandom_inner(dest)
| ^^^ use of undeclared crate or module `imp`
For more information about this error, try `rustc --explain E0433`.
error: could not compile `getrandom` due to 2 previous errors
You want to enable the "js" feature on the getrandom crate itself. Even if your crate doesn't directly depend on it, you'll want to add a line like getrandom = { version = "0.2", features = ["js"] }
@vdods is correct. We explain this in our docs for the custom feature, but we should also explain how to do it for the js feature, as that is more common.
Leaving this here for the next dummy who thought he applied both changes in the docs and goes searching for more magical incantations, but actually did these two changes wrong :)
I depend on a crate that depends on rand that depends on getrandom.
I am now trying to build for wasm32-unknown-unknown instead of my usual linux build
Initially I ran into the error message that points to the docs. After reading the docs I have
modified my Cargo.toml to set the feature
modified .cargo/config.toml to set the rustflags
# Cargo.toml# A dependency _somewhere_ that does not play nicely with wasm.# https://docs.rs/getrandom/latest/getrandom/#webassembly-support# It needs the feature here, but also RUSTFLAGS='--cfg getrandom_backend="wasm_js"'getrandom = { version = "0.3", features = ["wasm_js"] }
# .cargo/config.toml# cargo install wasm-server-runner# then this config makes cargo run use the server-runner if --target wasm32-unknown-unknown is specified# https://bevy-cheatbook.github.io/platforms/wasm.html
[target.wasm32-unknown-unknown]
runner = "wasm-server-runner"# The dependency getrandom needs this flag set, but only when the target is wasm32-unknown-unknown.# https://docs.rs/getrandom/latest/getrandom/#opt-in-backendsrustflags = ['--cfg', 'getrandom_backend="linux_getrandom"',]
These changes changed got me past the previous error to a different one.
When I ran the command (without explicitly adding the rustflags)
RUSTFLAGS='--cfg getrandom_backend="wasm_js"' cargo run --target wasm32-unknown-unknown
I ran into
error: `linux_getrandom` backend can be enabled only for Linux/Android targets!
--> /home/generic/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/getrandom-0.3.1/src/backends/linux_android.rs:11:1
|
11 | compile_error!("`linux_getrandom` backend can be enabled only for Linux/Android targets!");
Activity
vdods commentedon Jun 24, 2022
You want to enable the "js" feature on the getrandom crate itself. Even if your crate doesn't directly depend on it, you'll want to add a line like
getrandom = { version = "0.2", features = ["js"] }josephlr commentedon Jun 24, 2022
@vdods is correct. We explain this in our docs for the
customfeature, but we should also explain how to do it for thejsfeature, as that is more common.newpavlov commentedon Aug 30, 2022
I think we close this issue considering #280 is merged.
lucidBrot commentedon Mar 16, 2025
Leaving this here for the next dummy who thought he applied both changes in the docs and goes searching for more magical incantations, but actually did these two changes wrong :)
randthat depends ongetrandom.wasm32-unknown-unknowninstead of my usual linux buildCargo.tomlto set the feature.cargo/config.tomlto set the rustflagsThese changes changed got me past the previous error to a different one.
When I ran the command (without explicitly adding the rustflags)
RUSTFLAGS='--cfg getrandom_backend="wasm_js"' cargo run --target wasm32-unknown-unknownI ran into
Well, the reason was simply that I somehow happened to specify
linux_getrandominstead ofwasm_js, because I copy-pasted without thinking from https://docs.rs/getrandom/latest/getrandom/#opt-in-backends