wasm-bindgen: memory import undefined
I’ve been going through the rust wasm book and I’ve gotten stuck on a problem.
Summary
I’m have an issue where memory
import {memory} from "wasm-rust/index_bg";
is undefined when I import it. So when I try to access buffer, it ofc errors out. Are there any known situations that would cause this?
Additional Details
I’m using the Webpack Wasm-pack plugin. The project is can be found here: https://github.com/jregistr/wasm-game-of-life
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 4
- Comments: 20 (7 by maintainers)
Commits related to this issue
- fixup - https://github.com/rustwasm/wasm-bindgen/issues/2200 — committed to boweiliu/wasm-game-of-life by boweiliu 2 years ago
I had the same issue. It’s quite annoying when you’re making your first steps with Rust and wasm. I followed everything in the tutorial and it just doesn’t work. In my case I fixed it liked this (adding .wasm to wasm_game_of_life_bg):
Hello!
We are running into this as well. We have found a workaround that works for us, change the import like @alexcrichton said:
But if we only do that typescript will complain that it does not find the module, we fix that by renaming the
index_bg.d.tsfile toindex_bg.wasm.d.ts.I am interested in contributing a fix to this issue if it would be merged. 🙂
I believe you’ll need to import
index_bg.wasmsinceindex_bg.jsalso exists, but that should do the trick!I had the same issue, but the inital example I followed used
wasm-bindgen = "0.2.63", so the fix was not there. Updated it to the most recent version .69 and it now works.I solved the problem by reading the source code of rwasm, I should use memory like this:
I’m running into a similar (possibly related?) issue, reported at https://github.com/rustwasm/wasm-pack/issues/1030.
I’m running into a similar issue in a project, and importing
*_bg.wasmdoes indeed do the trick in vanilla JS. I’d like to have my project in Typescript though, and when I try to import with the .wasm extension (import { memory } from "duna_frontend/duna_frontend_bg.wasm"), it complains about being unable to find the module or its type definitions - if I try to import without the .wasm extension (import { memory } from "duna_frontend/duna_frontend_bg"), then webpack builds but warns that"export 'memory' was not found in 'duna_frontend/duna_frontend_bg'. I’m having trouble determining where on my toolchain the issue is coming from (webpack? yarn? typescript? wasm-pack? Cargo config?), so I’m wondering if anyone has any insight into this.For what it’s worth, my code can be found here: https://github.com/noloerino/duna/tree/reg-display/duna_frontend (JS stuff is in
www/).package.jsongenerated bywasm-packis missingfilename.wasm.d.tsin the files section. You can add it by yourshelf before(p)npm installOR copyfilename.wasm.d.tsinto your node_modules and restart TS server.Besides: I use
viteandvite-plugin-wasmand i needbuildandpreview, instead of dev server to get correct working.Yes.