Unable to import wasm from `wasm-bindgen` in Cloudflare
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 11.2k
- Forks
- 899
- Avg merge
- 2d 24m
- Merged PRs (30d)
- 40
Description
Environment
maxi@Maxi:~/personal/nitro-nimiq-core-wasm$ node --version
v22.13.1
maxi@Maxi:~/personal/nitro-nimiq-core-wasm$ pnpm list
Legend: production dependency, optional only, dev only
/home/maxi/personal/nitro-nimiq-core-wasm (PRIVATE)
dependencies:
@nimiq/core 2.0.6
devDependencies:
nitropack 2.10.4
Reproduction
- Stackblitz: stackblitz.com/~/github.com/onmax/nitro-nimiq-core-wasm
- Github: onmax/nitro-nimiq-core-wasm
Describe the bug
When loading a WASM module compiled with wasm-bindgen in a Nitro or NuxtHub project on cloudflare_pages, the module does not start correctly.
My nitro config is as follows:
// nitro.config.ts
export default defineNitroConfig({
srcDir: "server",
compatibilityDate: "2025-02-12",
experimental: { wasm: true },
wasm: { lazy:true, esmImport: true },
preset: "cloudflare_pages",
externals: {
inline: ['@nimiq/core'] // I was having issues with weird relative paths
}
});
Dynamic wasm import. Using lazy event handler.
This is inspired by this test and this stackblitz. The code can be found in attempt-1.ts
import init, {PublicKey} from '@nimiq/core/web'
export default defineLazyEventHandler(async () => {
const mod = await import('./nimiq.wasm?module' as string)
return eventHandler(async () => {
await init(mod)
const publicKey = '82d80b86d9bf1906832e9f0ba4fa69018792f59190075c396b0e85aeac444e55'
const key = PublicKey.fromHex(publicKey);
return key.toHex()
})
});
This produces the following error:
WebAssembly.instantiate(): Argument 0 must be a buffer source
at __wbg_load (./.nitro/dev/index.mjs:2799:44)
at __wbg_init (./.nitro/dev/index.mjs:3158:40)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async Object.handler (./.nitro/dev/index.mjs:3196:5)
Static wasm import.
Inspired by this test. The code can be found in attempt-2.ts
import init, { PublicKey } from '@nimiq/core/web'
// @ts-expect-error this is ok
import mod from './nimiq.wasm'
export default defineEventHandler(async () => {
await init()
const publicKey = '82d80b86d9bf1906832e9f0ba4fa69018792f59190075c396b0e85aeac444e55'
const key = PublicKey.fromHex(publicKey)
return key.toHex()
})
I get an infinite loading screen and this error is displayed:
[nitro] [unhandledRejection] TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".wasm" for /home/maxi/personal/nitro-nimiq-core-wasm/.nitro/dev/wasm/nimiq-9e6ed8aecd4a58a7.wasm
at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:219:9)
at defaultGetFormat (node:internal/modules/esm/get_format:245:36)
at defaultLoad (node:internal/modules/esm/load:120:22)
at async ModuleLoader.loadAndTranslate (node:internal/modules/esm/loader:483:32)
at async ModuleJob._link (node:internal/modules/esm/module_job:115:19) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
Maybe related to this issue: https://github.com/harlan-zw/nuxt-seo/issues/160
Using with NuxtHub
The previous reproductions are errors that I have in my dev server. However, in my other reproduction using NuxtHub I was able to run the code in my dev server. The problem was that when I tried to deploy in a Cloudflare Worker I got problems. Please see the issue in https://github.com/nuxt-hub/core/issues/467
Additional context
Other reproductions
- Minimal reproduction of the issue is available using only wrangler. The reproduction repository can be found here and it was successfully deployed at this URL. This reproduction works.
- Minimal reproduction using NuxtHub
Related issues
This issue was first open in https://github.com/nuxt-hub/core/issues/467
Patching @nimiq/core@2.0.6
You need to patch @nimiq/core in order to fix the import for web sub-module to be able to import it. See the patch. This will be fixed in 2.0.7.
Logs
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with nitro.config.ts and the server/routes/attempt-1.ts and attempt-2.ts reproductions, then compare them with test/fixture/routes/wasm/dynamic-import.ts and static-import.ts. Run the linked StackBlitz or reproduction projects using the cloudflare_pages preset and verify that both dynamic and static wasm imports initialize without the reported buffer-source or unknown-extension errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- nuxt, typescript, wasm
- Domain
- backend, cloud
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100