"use of undeclared crate or module `imp`" on getrandom-0.2.3 when building for wasm32-unknown-unknown

Hello,

I'm trying to build my learning/toy project/repo (GitHub - smpurkis/numrs: A toy project to implement a basic array library in Rust, as part of learning it.) for wasm32-unknown-unknown.

However, I am getting the error:

error[E0433]: failed to resolve: use of undeclared crate or module `imp`
   --> /home/sam/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.3/src/lib.rs:246:5
    |
246 |     imp::getrandom_inner(dest)
    |     ^^^ use of undeclared crate or module `imp`

From similar issues (see below), it seems it is dependency related. But I can't figure it out.

my Cargo.toml is:

[package]
name = "numrs"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
num-traits = "0.2.14"
rand = "0.8.4"
rayon = "1.5.1"

I've found a few others who have had the same/similar issue:
(upgrade to 3.0) Target not supported: getrandom v0.2.2 · Issue #308 · paritytech/frontier · GitHub - not sure how to follow this fix
Cargo build failing when combining two dependencies - solved by new resolver, which I believe is now the default, so this doesn't fix my issue

and I found some other resources that talk about it:

However, I am unsure how to fix this issue.
Any help would be much appreciated!

If you are planning to run this in the browser, do their instructions under the Indirect Dependencies section work?

Indirect Dependencies

If getrandom is not a direct dependency of your crate, you can still enable any of the above fallback behaviors by enabling the relevant feature in your root crate’s Cargo.toml :

[dependencies]
getrandom = { version = "0.2", features = ["js"] }

Can't believe I missed that!
Worked like a charm, thank you!

1 Like