Search code examples
node.jses6-moduleswasm-pack

Trying to load a library built with wasm pack in nodejs with ESM leads to an error


I published a library built using wasm-pack but when I try to use it i get the following error:

file:///mnt/user-data/jan/aztec2-internal/yarn-project/.pnp.loader.mjs:131
      throw new Error(`Unknown file extension ".wasm" for ${filepath}`);
            ^

Error: Unknown file extension ".wasm" for /mnt/user-data/jan/aztec2-internal/yarn-project/.yarn/cache/fhe-wasm-npm-0.1.4-159fe086bd-0b8014b75e.zip/node_modules/fhe-wasm/fhe_wasm_bg.wasm
    at getFileFormat (file:///mnt/user-data/jan/aztec2-internal/yarn-project/.pnp.loader.mjs:131:13)
    at load$1 (file:///mnt/user-data/jan/aztec2-internal/yarn-project/.pnp.loader.mjs:185:18)
    at nextLoad (node:internal/modules/esm/loader:163:28)
    at ESMLoader.load (node:internal/modules/esm/loader:605:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:22)
    at new ModuleJob (node:internal/modules/esm/module_job:63:26)
    at #createModuleJob (node:internal/modules/esm/loader:480:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:434:34)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:78:21)

Does anyone know what might be the issue? wasm-pack doesn't yet have a proper support for ESM with nodejs and for this reason I had to modify the typescript bindings (I submitted a feature request).


Solution

  • I was being very stupid and it turns out that ESM manages to import commonjs libraries just fine. For some reason I assumed they are fundamentally incompatible. For this reason I can just use the wasm-pack produced library without converting it to ESM.