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 = 2
    • Error: 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:

image

To reproduce

The following reproductions are a bare Next.js 14 starter repository with onnxruntime@1.19.2.

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)

Most upvoted comments

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:

  • try to import the non .bundle file. 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.
  • set ort.env.wasm.wasmPaths:
    ort.env.wasm.wasmPaths = {
      mjs: 'https://cdn.jsdelivr.net/npm/onnxruntime-web@1.20.1/dist/ort-wasm-simd-threaded.mjs',
      wasm: 'https://cdn.jsdelivr.net/npm/onnxruntime-web@1.20.1/dist/ort-wasm-simd-threaded.wasm'
    };
    

@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.