Search code examples
javascripttypescriptwebassemblydeno

TypeScript: This expression is not callable. No constituent of type 'ExportValue' is callable. WebAssembly module


I got this problem when I run this code in index.ts with Deno.

const wasm = await Deno.readFile("./wasm_test/pkg/wasm_test_bg.wasm");
const wasmModule = new WebAssembly.Module(wasm);
const wasmInstance = new WebAssembly.Instance(wasmModule);
const wasmTest = wasmInstance.exports;
wasmTest.sum(1, 3); // Error

Error: This expression is not callable. No constituent of type 'ExportValue' is callable.

I get and error when calling sum, it should give me a 4 as a result. When I run it as index.js it works perfectly. I used wasm-pack to compile the Rust code.


Solution

  • The problem is that the name add is not known. Change the 4th line of your code as below:

    const wasm = await Deno.readFile("./add.wasm");
    const wasmModule = new WebAssembly.Module(wasm);
    const wasmInstance = new WebAssembly.Instance(wasmModule);
    const sum = wasmInstance.exports.sum as CallableFunction; // exports.add if you test with the below linked wasm.
    console.log(sum(1, 3))
    

    See documentation.

    For my test I found a wasm example with an add-function here.