您可以构建没有JS胶水文件的.wasm文件(独立)。
创建一个test.c文件:
int add(int a, int b) { return a + b;}生成独立的.wasm文件:
emcc test.c -O2 -s WASM=1 -s SIDE_MODULE=1 -o test.wasm
在Node.js应用中使用.wasm文件:
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);});关键部分是WebAssembly.instantiate()的第二个参数。没有它,您将收到错误消息:
TypeError:WebAssembly实例化:imports参数必须存在,并且在启动时位于Function.Module.runMain(module.js:695:11)的process._tickCallback(internal
/ process /
next_tick.js:188:7)处(bootstrap_node.js:191:16)在bootstrap_node.js:612:3



