wasm-bindgen: Undefined `__wbindgen_malloc` when running WASM binary

Describe the Bug

We’ve recently hit a snag from compiling the same code and getting different results a few weeks apart.

2 weeks ago, we published this bundle: https://unpkg.com/browse//bitmask-core@0.5.0-rc.6/

It was built from this repo and commit: https://github.com/diba-io/bitmask-core/commit/5cf7f4d64354e4979b9d5d74c8c83871f1627e83

Using this command: wasm-pack build --release --target bundler

Back then, it produced a binary that runs in our Chrome extension, but now it produces something different, where running any function results in this error:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '__wbindgen_malloc')

I’m not sure what steps I can try to investigate this further.

Examples of more recent tries:

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 36 (5 by maintainers)

Commits related to this issue

Most upvoted comments

I don’t think there is an init function when using bundler, only when targeting web

So either you have (with wasm-pack build --target web):

import init, { function } from './pkg/xxx.js';
// Without async/await
init().then(() => {
    function();
  });
// With async await
await init();
function();

Or (with wasm-pack build --target bundler):

import { function } from './pkg/xxx.js';
function();

Edit: Here is an example with --target web: wasm.html

I don’t know sorry 😕

experiments: {
        asyncWebAssembly: true,
        topLevelAwait: true
       }
import * as wasm from "./pkg"

await wasm
wasm.greet("hello")

and the “Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘__wbindgen_malloc’)” error goes away…

@FlareFlo What version of Webpack are you using? I’m running webpack 5.75.0, apparently it doesn’t need such loaders anymore, instead, we’ve got these options turned on:

    experiments: {
      asyncWebAssembly: true,
      syncWebAssembly: true,
    },

I have these options set using 5.75.0

I’m using vite with this config (entire)

import { defineConfig } from 'vite';

import wasm from 'vite-plugin-wasm';

export default defineConfig({
    plugins: [wasm()]
});

I believe even in webpack you’ll need to touch the config, but I don’t know webpack

Yes absolutely. You can read up on it here: https://rustwasm.github.io/docs/wasm-bindgen/examples/without-a-bundler.html.

I can only assume that wasm-pack kinda does this for you, but in the example that was posted above a custom HTML was used that doesn’t call init(). Again, I have no clue about wasm-pack, just speculating.

I’m glad I’m not the only one seeing this! It’s driving me crazy 😭

My development branch has been offline for 2 weeks, I thought I had just misconfigured the webserver somehow.

I’m getting either Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '__wbindgen_add_to_stack_pointer') or Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'main_js') where main_js is a rust fn annotated with #[wasm_bindgen(start)]. Occurs both on windows and Linux.