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
imports
argument, and the documentation agrees.Your code only passes in a single argument instead of two. – ArcuateWebAssembly.instantiate()
correctly. – Arcuate.instantiate()
. – Arcuatewasi_snapshot_preview1
, somehow (either your own code, or because your build chain added that). So the details about test.wasm and how its built are now important. – Arcuate