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

Most upvoted comments

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):

import { memory } from "wasm-game-of-life/wasm_game_of_life_bg.wasm";

Hello!

We are running into this as well. We have found a workaround that works for us, change the import like @alexcrichton said:

I believe you’ll need to import index_bg.wasm since index_bg.js also exists, but that should do the trick!

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.ts file to index_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.wasm since index_bg.js also 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’m using vite to start the project and the same problem arose😔

I am using the plugin vite-plugin-rsw. In the file main.ts, I can import the wasm file, and it provides code hinting. However, when I start it with pnpm run dev, the DOM raises error.

I solved the problem by reading the source code of rwasm, I should use memory like this:

import init from 'wasm-game-of-life'
const wasmInit = await init()
const memory = wasmInit.memory
// ...

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.wasm does 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.json generated by wasm-pack is missing filename.wasm.d.ts in the files section. You can add it by yourshelf before (p)npm install OR copy filename.wasm.d.ts into your node_modules and restart TS server.

Besides: I use vite and vite-plugin-wasm and i need build and preview, instead of dev server to get correct working.

@Liamolucko would you like me to add you to those?

Yes.