I'm generating WASM, an I get the following error:
[WASM] TypeError: wasm2.__wbindgen_start is not a function
This error is happening in the JavaScript interface code that's generated when I build my WASM. Here's the generated code:
import * as wasm from "./web_bg.wasm";
export * from "./web_bg.js";
import { __wbg_set_wasm } from "./web_bg.js";
__wbg_set_wasm(wasm);
wasm.__wbindgen_start(); /* here's the error */
The code loads just fine when I comment wasm.__wbindgen_start();
out. This happens regardless of whether or not I add a start
entry-point function to my Rust library. Here's my start
function:
#[wasm_bindgen(start)]
fn start() {
println!("Hello World, from Rust");
}
The code compiles fine, and I'm following the restrictions (no args, only return ()
or Result<()>
, etc).
What could this issue be? Also, is a start
function required to be defined, or can it be optional?
I'm also not using a bundler. I'm using bun
to manage dependencies for the web side, but all it does is spin up a basic HTTP server to serve my index.html
file.
EDIT: I figured out the issue for anyone else running into this. I was using wasm-pack build
to build my JavaScript interface, since that's what the Game of Life tutorial uses. It turns out, I needed to run wasm-pack build --target web
instead. The trick was specifying the web
target, which generates a much more intricate JavaScript interface in my /pkg
directory. I'm running into further issues, but that's out of the scope of this post.