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

Next.js does not pick the correct export of dependency #70296

Closed
fs-eire opened this issue Sep 20, 2024 · 3 comments
Closed

Next.js does not pick the correct export of dependency #70296

fs-eire opened this issue Sep 20, 2024 · 3 comments
Labels
bug Issue was opened via the bug report template. locked Module Resolution Module resolution (CJS / ESM, module resolving).

Comments

@fs-eire
Copy link

fs-eire commented Sep 20, 2024

Link to the code that reproduces this issue

https://github.com/fs-eire/nextjs-onnxruntime

To Reproduce

  1. npm ci and npm run dev
  2. navigate to localhost:3000 and you will see the compile error.

Current vs. Expected behavior

Current:

./src/pages/components/OnnxButton.tsx:3:1
Module not found: Package path ./webgpu is not exported from package D:\code\nextjs-onnxruntime\node_modules\onnxruntime-web (see exports field in D:\code\nextjs-onnxruntime\node_modules\onnxruntime-web\package.json)

Expected: should be no error.

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Home
  Available memory (MB): 32510
  Available CPU cores: 32
Binaries:
  Node: 18.18.2
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.2.11 // There is a newer version (14.2.13) available, upgrade recommended!
  eslint-config-next: 14.2.11
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.6.2
Next.js Config:
  output: N/A
 ⚠ There is a newer version (14.2.13) available, upgrade recommended!
   Please try the latest canary version (`npm install next@canary`) to confirm the issue still exists before creating a new issue.
   Read more - https://nextjs.org/docs/messages/opening-an-issue

Which area(s) are affected? (Select all that apply)

Module Resolution

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

This example project uses a library "onnxruntime-web", which only runs on browser. In the field "exports" in its package.json, "node" is explicitly disabled to avoid misusage of running in Node.js.

// (partial) file content of ./node_modules/onnxruntime-web/package.json

  "exports": {
    ...
    "./webgpu": {
      "node": null,
      "import": "./dist/ort.webgpu.bundle.min.mjs",
      "require": "./dist/ort.webgpu.min.js",
      "types": "./types.d.ts"
    }
  }

It should be working well in a browser environment for import * as onnx from 'onnxruntime-web/webgpu';. However it tries to read the "node" export field and fails, even if I put 'use client'; in the top of the file.

Related issue: microsoft/onnxruntime#21811

@fs-eire fs-eire added the bug Issue was opened via the bug report template. label Sep 20, 2024
@github-actions github-actions bot added the Module Resolution Module resolution (CJS / ESM, module resolving). label Sep 20, 2024
@naaa760
Copy link

naaa760 commented Sep 22, 2024

Hello! @fs-eire :)

fix: #70296

To fix the bug, updatenext.config.jsto ensure Webpack resolves the correct browser module by adding an alias foronnxruntime-web/webgpu. Also, place 'use client';at the top of your component and use dynamic imports to load the library only in the browser environment withif (typeof window !== 'undefined'). Finally, upgrade to the latest Next.js version using npm install next@latestto ensure compatibility.

@fs-eire
Copy link
Author

fs-eire commented Sep 23, 2024

Hello! @fs-eire :)

fix: #70296

To fix the bug, updatenext.config.jsto ensure Webpack resolves the correct browser module by adding an alias foronnxruntime-web/webgpu. Also, place 'use client';at the top of your component and use dynamic imports to load the library only in the browser environment withif (typeof window !== 'undefined'). Finally, upgrade to the latest Next.js version using npm install next@latestto ensure compatibility.

Thank you so much for the solution! The combination of the following makes it work:

  • use 'use client';
  • use if (typeof windows !== 'undefined')

Copy link
Contributor

github-actions bot commented Oct 7, 2024

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot added the locked label Oct 7, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked Module Resolution Module resolution (CJS / ESM, module resolving).
Projects
None yet
Development

No branches or pull requests

2 participants