I got this code from stackoverflow link:
How to use WebAssembly from node.js?
Create a test.c file:
int add(int a, int b) {
return a + b;
}
Build the standalone .wasm file
emcc test.c -O2 -s WASM=1 -s SIDE_MODULE=1 -o test.wasm
Use the .wasm file in Node.js app:
const util = require('util');
const fs = require('fs');
var source = fs.readFileSync('./test.wasm');
const env = {
memoryBase: 0,
tableBase: 0,
memory: new WebAssembly.Memory({
initial: 256
}),
table: new WebAssembly.Table({
initial: 0,
element: 'anyfunc'
})
}
var typedArray = new Uint8Array(source);
WebAssembly.instantiate(typedArray, {
env: env
}).then(result => {
console.log(util.inspect(result, true, 0));
console.log(result.instance.exports._add(9, 9));
}).catch(e => {
// error caught
console.log(e);
});
Receiving error when starting Node.js server with
node Node.js
LinkError: WebAssembly Instantiation: Import #3 module="env" function="abort" error: function import requires a callable