12

I've read many articles about running wasm files in node.js. Whenever I test the code, It throws this error

[TypeError: WebAssembly.instantiate(): Import #0 module="wasi_snapshot_preview1" error: module is not an object or function]

and then it does not show anything in the result. I am using this code:

const sp = {
  env: {
    memoryBase: 0,
    tableBase: 0,
    memory: new WebAssembly.Memory({
      initial: 256
    }),
    table: new WebAssembly.Table({
      initial: 0,
      element: 'anyfunc'
    })
  },
  imports: {
    imported_func: arg => {
      console.log(arg);
    }
  }
}

const fs = require('fs')
, wasm = WebAssembly.instantiate(new Uint8Array(fs.readFileSync('./test.wasm')), sp)
.then(result => console.log(result));

This code is throwing the above error.

Is there anything I am doing wrong?

SOLUTION:

There was nothing wrong with my code, rather here was something wrong with the way I was compiling my code. Instead of using

em++ test.cpp -o test.wasm

I should've used:

em++ -O1 test.cpp -o test.wasm -s WASM=1
13
  • But did you search Stackoverflow before posting? Because I searched by copy pasting your error message, and found stackoverflow.com/a/50734386/740553 (as well as two other questions). Also: always check MDN when using web APIs: the error literally tells you that you need an imports argument, and the documentation agrees.Your code only passes in a single argument instead of two. Commented Jan 14, 2020 at 1:54
  • 2
    Yes I tried that but it is throwing an error: imports is not defined Commented Jan 14, 2020 at 1:59
  • 1
    I think that the question you are referencing used browser javascript, while I am using node Commented Jan 14, 2020 at 2:02
  • 1
    In my code, I put the second argument, which is the env. I got that from this tutorial Commented Jan 14, 2020 at 2:08
  • 1
    I have updated my code. Sorry for the confusion Commented Jan 14, 2020 at 2:11

4 Answers 4

8

Change test.wasm to test.js should work as well:

em++ -O1 test.cpp -o test.js -s WASM=1

Using .wasm as the output type or -s STANDALONE_WASM requires a runtime with wasi_snapshot_preview1 support.

Sign up to request clarification or add additional context in comments.

2 Comments

What does a runtime with wasi_snapshot_preview1 support a mean?
Wasmtime, browser, or Node.js that supports WASI.
2

The error reported is as follows:

[TypeError: WebAssembly.instantiate(): Import #0 module="wasi_snapshot_preview1" error: module is not an object or function]

This indicates that your WebAssembly module, test.wasm, is expecting an import named wasi_snapshot_preview1, which is required in order to instantiate it. This is nothing to do with the Node environment, you would see the same error in the browser.

How are you building and compiling your WebAssembly module? This import suggests you are using WASI.

I would recommend starting with a much simpler WebAssembly example.

1 Comment

all that i am doing is returning 0 inside a main function in my wasm code. I am using this command to build my wasm: em++ test.cpp -o test.wasm
1

Using this as second argument for WebAssembly instantiate worked for me:

const importObject = { wasi_snapshot_preview1: wasi.exports };
//more code
const instance = await WebAssembly.instantiate(wasm, importObject);

2 Comments

Where does the wasi object come from ?
It depends on target environment. For example, wasi object is coming from a instance constructed with WASI for Node.js target environment. You can checkout WASI documentation for more details.@user1300214
0

I was able to get rid of wasi_snapshot_preview1 error by recompiling with flags

emcc test.c -o test.wasm --no-entry -s EXPORTED_FUNCTIONS=_sum

Parameters: --no-entry means no main alike, sum is the function to be exported from test.c.

suggested by (long) blog post [1]

[1] https://habr.com/en/companies/otus/articles/693572/ (In Russian)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.