error[E0463]: can't find crate for `core` while building rust project for wasm32-unknown-unknown

Viewed 3681

I get the following error message:

error[E0463]: can't find crate for `core`
  |
  = note: the `wasm32-unknown-unknown` target may not be installed

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
error: could not compile `cfg-if`

To learn more, run the command again with --verbose.

When I run this command:

cargo build --target wasm32-unknown-unknown
1 Answers

There is a specialty about crates like core and std that is, since they are ubiquitous, they are used in some pre-compiled version instead of being build from source each time like other crates are. However, this only becomes apparent if you are cross compiling (i.e. using the --target flag).

There are essentially two options to solve this, and they are also explicitly stated in rustc --explain E0463 (online version):

  • [if] You are cross-compiling for a target which doesn’t have std [or core] prepackaged. Consider one of the following:
    • Adding a pre-compiled version of std [or core] with rustup target add
    • Building std [or core] from source with cargo build -Z build-std

The first option (also already pointed-out by @Ivan and @Jmb) is probably the obvious one:

rustup target add wasm32-unknown-unknown
cargo build --target wasm32-unknown-unknown

The second option only works with a Nightly compiler and requires the rust-src component. But might be useful if you don't want to install each target, or if you are compiling against a custom target (i.e. --target with some JSON-file):

rustup +nightly component add rust-src
cargo +nightly build --target wasm32-unknown-unknown -Z build-std=core

As a side note, the error message has improved in the current Nightly Rust version as opposed to the current stable version (1.52). A Nightly compiler will print:

error[E0463]: can't find crate for `core`
  |
  = note: the `wasm32-unknown-unknown` target may not be installed
  = help: consider downloading the target with `rustup target add wasm32-unknown-unknown`
  = help: consider building the standard library from source with `cargo build -Zbuild-std`
Related