onnxruntime: [Web] Previously used .wasm binaries missing in 1.19.0 (using web bundler with CopyPlugin)

Describe the issue

Using a onnxruntime-web with a web bundler like CopyPlugin I am now getting Package path ./webgpu is not exported from package. When I manually try to bundle the .wasm’s I get Unable to locate '..node_modules/onnxruntime-web/dist/ort-wasm.wasm' glob.

I assume the names changed with this update as I see the web bundling changed. I see now bundling was introduced with https://github.com/microsoft/onnxruntime/commit/dd805ff77d896e9ff5457818c49d2103d6ba6248. I looked for some migration docs but couldn’t find any.

To reproduce

Can follow the current quick-start https://github.com/microsoft/onnxruntime-inference-examples/tree/main/js/quick-start_onnxruntime-web-bundler

or

  1. Use next.js or similar web bundler/pack framework
  2. Use webpack copy plugin to load onnxruntime-web. Example for next.js:
/** @type {import('next').NextConfig} */
import CopyPlugin from "copy-webpack-plugin";

const nextConfig = {
  webpack: (config) => {
    config.resolve.extensions.push(".ts", ".tsx");
    config.resolve.fallback = { fs: false };

    config.plugins.push(
      new CopyPlugin({
        patterns: [
          {
            from: './node_modules/onnxruntime-web/dist/ort-wasm.wasm',
            to: 'static/chunks/pages',
          },
          {
            from: './node_modules/onnxruntime-web/dist/ort-wasm-simd.wasm',
            to: 'static/chunks/pages',
          },
          { 
            from: './node_modules/onnxruntime-web/dist/ort-wasm-simd.jsep.wasm',
            to: 'static/chunks/pages',
          },
          ],
        }),
    );

    return config;
  } 
}

export default nextConfig;
  1. Import onnxruntime-web/webgpu with import * as ort from 'onnxruntime-web/webgpu';
  2. Run the dev server, example: bun run dev

I get the error:

Module not found: Package path ./webgpu is not exported from package .../node_modules/onnxruntime-web

Urgency

Not urgent, rolled back to 1.18.0 for now.

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.19.0

Execution Provider

‘webgpu’ (WebGPU)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 19 (8 by maintainers)

Most upvoted comments

@illbexyz I verified the solution mentioned in https://github.com/vercel/next.js/issues/70296#issuecomment-2365441435 works.

Fix: https://github.com/fs-eire/nextjs-onnxruntime/commit/b7adf6f0367c81532e5173206f44e0f06da30bbe

  • use 'use client'; at the top of the CSR component
  • use dynamic import and use if (typeof windows !== 'undefined') to wrap the dynamic import so that it will not be compiled as Node.js usage.

the original idea of adding "node": null, is to avoid misuse in Node.js. It seems by removing them indeed helps to use onnxruntime-web in Next.js… if not using web worker. (#22113 is the issue of next step if trying to enable web worker)

I made a reproduction here: illbexyz/nextjs-onnxruntime.

It’s missing onnxruntime-web in the dependencies, and both next dev and next build work for me after adding it.

Yes, you are right. It’s missing in the dependencies and need to run npm install onnxruntime-web@dev. But I think @illbexyz is complaining about cannot import from “onnxruntime-web/webgpu” (while the repo uses “onnxruntime-web” and it works, modifying the line in OnnxButton.tsx will show that error)

Yes sorry my bad, I provided a quite sloppy reproduction 😅 In case it might be useful to someone, I updated the reproduction to:

  • declare onnxruntime-web@dev in package.json
  • use onnxruntime-web/webgpu in OnnxButton.tsx

The error is because Next.js module resolver picks export for “node” not “import”/“require”(web) in your repo. I don’t know exactly how to change this behavior, but if Next.js picks the “import”/“require”(web) export, the issue should be fixed.

Yes I agree with this conclusion, but I have no idea on how to configure next.js to pick the “import”/“require”(web) export.

@fs-eire Thank you! I can confirm that the solution works correctly on Next 14. Unfortunately it doesn’t work on Next <= 12 where 'use client' wasn’t introduced yet, but imho it’s not a big deal.

If @e-simpson agrees I think we can close the issue 🙂

I’m having the same issue. I made sure to load onnxruntime-web/webgpu dynamically to filter out SSR errors, but the error seems to be triggered at build time.

I made a reproduction here: https://github.com/illbexyz/nextjs-onnxruntime. See https://github.com/illbexyz/nextjs-onnxruntime/blob/main/src/pages/index.tsx where I import <OnnxButton /> (which contains the import to onnxruntime-web/webgpu) dynamically with ssr: false.

There are multiple changes for onnxruntime-web exports.

a TL;DR version:

  • if you use WebGPU, then
    • import * as ort from 'onnxruntime-web/webgpu';
    • copy ort-wasm-simd-threaded.jsep.wasm
  • if you use CPU only, then
    • import * as ort from 'onnxruntime-web';
    • copy ort-wasm-simd-threaded.wasm

Just 2 .wasm files. copy the one you uses. See this link to check out options for importing.

Let me know if you have further questions

The solution to fix this, can be npm install onnxruntime-web@1.14.0, all fix the package.json, lock the onnxruntime-web to 1.14.0 version.