onnxruntime: [Web] Error when using Web Workers on Next.js
Describe the issue
I get the following error when using onnxruntime-web on Next.js with web workers:
Error: no available backend found. ERR: [wasm] TypeError: Failed to fetch, [cpu] Error: multiple calls to 'initWasm()' detected.
I had no issues with onnxruntime-web@1.18.0.
I believe the issue is that Next.js is trying to load the file from the filesystem:
- See Request URL in the following screenshot
file:///Users/alberto.nicoletti/node_modules/onnxruntime-web/dist/ort.bundle.min.mjs
Additional notes:
- I have no issues when not using web workers
- I obtain a slightly different error when disabling the proxy but setting
onnx.env.wasm.numThreads = 2Error: no available backend found. ERR: [wasm] SecurityError: Failed to construct 'Worker': Script at 'file:///Users/alberto.nicoletti/node_modules/onnxruntime-web/dist/ort.bundle.min.mjs' cannot be accessed from origin 'http://localhost:3000'., [cpu] Error: previous call to 'initWasm()' failed..- In this case there are no network requests being made but I believe these issues are likely related.
Network requests screenshot:
To reproduce
The following reproductions are a bare Next.js 14 starter repository with onnxruntime@1.19.2.
- Proxy error reproduction here: https://github.com/illbexyz/nextjs-onnxruntime/tree/wasm-proxy
- See https://github.com/illbexyz/nextjs-onnxruntime/blob/wasm-proxy/src/pages/components/OnnxButton.tsx where I use a web worker by enabling
onnx.env.wasm.proxy = true.
- See https://github.com/illbexyz/nextjs-onnxruntime/blob/wasm-proxy/src/pages/components/OnnxButton.tsx where I use a web worker by enabling
- Multi-thread reproduction here: https://github.com/illbexyz/nextjs-onnxruntime/tree/wasm-threads
- See https://github.com/illbexyz/nextjs-onnxruntime/blob/wasm-threads/src/pages/components/OnnxButton.tsx where I use
onnx.env.wasm.numThreads = 2. - In this reproduction I also added
crossOriginIsolationheaders here: https://github.com/illbexyz/nextjs-onnxruntime/blob/wasm-threads/next.config.mjs
- See https://github.com/illbexyz/nextjs-onnxruntime/blob/wasm-threads/src/pages/components/OnnxButton.tsx where I use
Urgency
Kinda urgent: every Next.js project is impacted.
ONNX Runtime Installation
Released Package
ONNX Runtime Version or Commit ID
1.19.2
Execution Provider
‘wasm’/‘cpu’ (WebAssembly CPU)
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 4
- Comments: 26 (13 by maintainers)
I resolved the issue after building the project. @fs-eire
@illbexyz if you are OK with loading the .mjs file and .wasm file dynamically from CDN, here is the steps:
.bundlefile. you can either patch the package.json of onnxruntime-web, or add a resolve rule in your webpack config to achieve this. Or you can import onnxruntime-web as UMD instead of ESM.ort.env.wasm.wasmPaths:@fs-eire I’m also facing similar issue. Were you able to create a fix for it?
I made a PR #22196. Hope to workaround this issue without having to modify webpack config from user side.