Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Web] Error when using Web Workers on Next.js #22113

Open
illbexyz opened this issue Sep 17, 2024 · 4 comments
Open

[Web] Error when using Web Workers on Next.js #22113

illbexyz opened this issue Sep 17, 2024 · 4 comments
Labels
platform:web issues related to ONNX Runtime web; typically submitted using template

Comments

@illbexyz
Copy link

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 [email protected].

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 [email protected].

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)

@illbexyz illbexyz added the platform:web issues related to ONNX Runtime web; typically submitted using template label Sep 17, 2024
@satyajandhyala
Copy link
Contributor

satyajandhyala commented Sep 18, 2024

@illbexyz Can you try using onnxruntime 1.19 in a clean environment?

@illbexyz
Copy link
Author

@satyajandhyala This is already the most minimal reproduction I could give. It's just onnxruntime-web added to a bare Next.js project initialized with npx create-next-app@latest.

@fs-eire
Copy link
Contributor

fs-eire commented Sep 18, 2024

Script at 'file:///Users/alberto.nicoletti/node_modules/onnxruntime-web/dist/ort.bundle.min.mjs' cannot be accessed from origin 'http://localhost:3000'.

This is a known issue. If you use webpack loading onnxruntime-web as ESM, you need this line in your webpack config:

module: { parser: { javascript: { importMeta: false } } },

(see https://github.com/Microsoft/onnxruntime/blob/abdc31de401262bcb03f538423389c2eb264a0ce/js/web/test/e2e/webpack.config.esm-js.js)

Explaination: onnxruntime-web use import.meta.url at runtime to determine the script path so that it is able to create worker with correct path. However the default behavior of Webpack rewrites import.meta.url into a static local file path ( in your case file:///Users/alberto.nicoletti/node_modules/onnxruntime-web/dist/ort.bundle.min.mjs) and it won't work at runtime. the highlighted line in the config disabled this specific behavior of webpack.

May be a good idea to update our document/examples.

@illbexyz
Copy link
Author

Hey @fs-eire, thanks for your time!

I tried adding the following to next.config.mjs:

webpack: (config) => {
  config.module.parser.javascript.importMeta = false;

  return config;
},

But now I get a different error:

SyntaxError: Cannot use 'import.meta' outside a module

I updated the reproductions if you wanna have a look.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform:web issues related to ONNX Runtime web; typically submitted using template
Projects
None yet
Development

No branches or pull requests

3 participants