Wasm-bindgen: Could not find `console` in `web_sys`

Created on 18 Nov 2018  路  10Comments  路  Source: rustwasm/wasm-bindgen

Hello I've been following the wasm-bindgen tutorial, specifically adding console logging with web-sys, and could not find the root to my issue building this project.

  75 |         web_sys::console::log_1(&"Hello, world!".into());
|    |                  ^^^^^^^ Could not find `console` in `web_sys`

My Cargo.toml:

[dependencies]
cfg-if = "0.1.2"
wasm-bindgen = "0.2"
# JAVASCRIPT STUFF
js-sys = "0.2"
web-sys = "0.3.5"

How it's being used (very basic):

extern crate cfg_if;
extern crate wasm_bindgen;
// JAVASCRIPT
extern crate js_sys;
extern crate web_sys;

// --- snip ---

/// Public methods, exported to JavaScript.
#[wasm_bindgen]
impl Universe {
    pub fn new() -> Universe {
        web_sys::console::log_1(&"Hello, world!".into());
// ...

I've tried both wasm-bindgen build --debug and:

[profile.release]
debug = true

Still no luck.

If I add the part about [dependencies.web-sys] to my toml (and remove web-sys = "0.3.5" to avoid duplicates) like the tutorial says I get:

Feature `default` includes `console_error_panic_hook` which is neither a dependency nor another feature

Possible I'm missing some obvious and crucial step here? I've never had trouble importing crates before so I don't know what I could be doing wrong.

Thank you.

Most helpful comment

web-sys doesn't expose any APIs unless you enable cargo features: https://rustwasm.github.io/wasm-bindgen/web-sys/cargo-features.html

duplicate key: web-sys for key dependencies

This is because you were declaring a dependency on web-sys twice: once with features and once without features.

Feature default includes console_error_panic_hook which is neither a dependency nor another feature

I suspect that you need to make sure that the [dependencies.web-sys] key is not in the middle of the dependencies list. If you are using the wasm-pack-template make sure it is after the wee_alloc and console_error_panic_hook entries.

All 10 comments

[dependencies.web-sys]
version = "0.3"
features = [ "console" ]  # Do you have this line in your Cargo.toml?

So if my Cargo.toml looks like this:

[dependencies]
# ...
web-sys = "0.3.5"

[dependencies.web-sys]
version = "0.3"
features = [ "console" ]

I get

duplicate key: `web-sys` for key `dependencies`

But if I remove web-sys from my dependencies:

[dependencies]
# ...
# web-sys = "0.3.5"

[dependencies.web-sys]
version = "0.3"
features = [ "console" ]

I get

Feature `default` includes `console_error_panic_hook` which is neither a dependency nor another feature

web-sys doesn't expose any APIs unless you enable cargo features: https://rustwasm.github.io/wasm-bindgen/web-sys/cargo-features.html

duplicate key: web-sys for key dependencies

This is because you were declaring a dependency on web-sys twice: once with features and once without features.

Feature default includes console_error_panic_hook which is neither a dependency nor another feature

I suspect that you need to make sure that the [dependencies.web-sys] key is not in the middle of the dependencies list. If you are using the wasm-pack-template make sure it is after the wee_alloc and console_error_panic_hook entries.

Thank you @fitzgen and @limira, moving the [dependencies.web-sys] tag fixed the issue for me!

I hadn't realized the APIs were not exposed by default. Maybe it's because I'm new to Rust and Cargo, but should the placement of the [dependencies.web-sys] tag be better described in the tutorial for wasm-bindgen? I can make a pull request to update the tutorial if you'd like.

@fitzgen Why does the order of dependencies matter? As far as I know that's generally not how Cargo dependencies work, so this is very surprising for users.

I think @fitzgen say about this:

[dependencies]
some-crate = "x.y.z"

[dependencies.web-sys]
version = "0.3"

some-other-crate = "k.l.m" 

should be:

[dependencies]
some-crate = "x.y.z"
some-other-crate = "k.l.m" 

[dependencies.web-sys]
version = "0.3"

@limira Ahh, I see, that makes sense, thanks!

I hadn't realized the APIs were not exposed by default. Maybe it's because I'm new to Rust and Cargo, but should the placement of the [dependencies.web-sys] tag be better described in the tutorial for wasm-bindgen? I can make a pull request to update the tutorial if you'd like.

Sure thing, that would be great!

I think @fitzgen say about this:

Exactly.

[dependencies]
wasm-bindgen = "0.2"
web-sys = { version="0.3.5", features=[ "console" ] }

It good!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gitmko0 picture gitmko0  路  3Comments

bantic picture bantic  路  4Comments

poccariswet picture poccariswet  路  3Comments

MarcAntoine-Arnaud picture MarcAntoine-Arnaud  路  3Comments

fitzgen picture fitzgen  路  4Comments