Skip to content

[Web] When the "node" condition path in exports is null vite (+svelte) build will wrongly throw a "No known conditions for" error #22361

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

Closed
bn-l opened this issue Oct 9, 2024 · 3 comments
Labels
api:Javascript issues related to the Javascript API ep:WebGPU ort-web webgpu provider ep:Xnnpack issues related to XNNPACK EP platform:web issues related to ONNX Runtime web; typically submitted using template

Comments

@bn-l
Copy link

bn-l commented Oct 9, 2024

Describe the issue

I am trying to build a project with this line. I've used webgl here but this applies to any import path (/wasm, /all, /webgpu, etc). Just importing "onnxruntime-web" without a path works though.

import * as ort from "onnxruntime-web/webgl";

when I run vite build I get this error:

error during build:
[commonjs--resolver] No known conditions for "./webl" specifier in "onnxruntime-web" package

The issue is with the exports of this particular package.json (excerpt from original).

!!! Note that the node condition for webgl is null: !!!

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

Vite will error if node:'s value is null. If I change it to any working path (e.g. "/types.d.ts") then it won't blow up and will correctly resolve to "import": "./dist/ort.webgl.min.mjs",. So this does work:

{
  "exports": {
      ".": {
        "node": {
          "import": "./dist/ort.node.min.mjs",
          "require": "./dist/ort.node.min.js"
        },
      "./webgl": {
        "node": "./types.d.ts",  <----| Changed to any valid path!
        "import": "./dist/ort.webgl.min.mjs",
        "require": "./dist/ort.webgl.min.js",
        "types": "./types.d.ts"
      }
}

Also swapping the order works also (no build error). Note that "import": "./dist/ort.webgl.min.mjs", is now before "node": null:

{
  "exports": {
      ".": {
        "node": {
          "import": "./dist/ort.node.min.mjs",
          "require": "./dist/ort.node.min.js"
        },
      "./webgl": {
        "import": "./dist/ort.webgl.min.mjs",  <-----|  Swapped!
        "node": null,                          <-----|
        "require": "./dist/ort.webgl.min.js",
        "types": "./types.d.ts"
      }
}

Further info

From here: https://nodejs.org/api/packages.html#package-entry-points, it can be seen that null should not be used like this.

System Info

System:
    OS: Windows 10 
  Binaries:
    Node: 22.7.0

To reproduce

Create a new vite project
npm create vite@latest

Add the package:
npm add -D onnxruntime-web

In index.ts:
import * as ort from "onnxruntime-web/webgl";

Then run the default build script from vite.

Urgency

I have a script that corrects this package.json so not urgent.

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.19.2

Execution Provider

'webgl' (WebGL), 'wasm'/'cpu' (WebAssembly CPU), 'xnnpack' (WebAssembly XNNPACK), 'webgpu' (WebGPU), Other / Unknown

@bn-l bn-l added the platform:web issues related to ONNX Runtime web; typically submitted using template label Oct 9, 2024
@github-actions github-actions bot added api:Javascript issues related to the Javascript API ep:WebGPU ort-web webgpu provider ep:Xnnpack issues related to XNNPACK EP labels Oct 9, 2024
@fs-eire
Copy link
Contributor

fs-eire commented Oct 9, 2024

I added this "node": null, in the export table to explicitly disallow import for node.js. This is for obvious reason: I don't expect webgl APIs working on Node.js. So it is expected that you will run into error if you try to import * as ort from "onnxruntime-web/webgl"; in a Node.js context.

@bn-l
Copy link
Author

bn-l commented Oct 10, 2024

From: vitejs/vite#18015 (posted issue with vite initially--this was a mistake):

Indeed, for web app, I don't think Vite would use node condition. I couldn't repro from the template and both dev / build seems fine. https://stackblitz.com/edit/vitejs-vite-usxpr5?file=main.js

> vite build

vite v5.4.8 building for production...
node_modules/onnxruntime-web/dist/ort.webgl.min.mjs (6:94998): Use of eval in "node_modules/onnxruntime-web/dist/ort.webgl.min.mjs" is strongly discouraged as it poses security risks and may cause issues with minification.
✓ 4 modules transformed.
dist/index.html                  0.32 kB │ gzip:  0.24 kB
dist/assets/index-wx6rlKjT.js  418.37 kB │ gzip: 93.44 kB
✓ built in 1.54s

@bn-l Can you provide reproductions?

@hi-ogawa Couldn't reproduce using plain vite because I am using sveltekit on top of vite which builds the server side rendering part no matter what (see here: sveltejs/kit#2937). So even though I don't use the package in a node environment and have SSR disabled, it's still trying to build for node.

This is a sveltekit issue so I will close this. Sorry to bother you unnecessarily.

@bn-l bn-l closed this as completed Oct 10, 2024
@ollema
Copy link

ollema commented Oct 26, 2024

@fs-eire I wonder if this can be reopened and there to be a different solution to this problem?

I have also run into this problem, and for SvelteKit there is simply no workaround at the moment or in the near future (as far as I can tell).

Even when you disable server side rendering (SSR) in SvelteKit, all files are still analyzed (imports are done, this package fails) on the server during build time, for example to look for the export const ssr = flag to disable SSR. See sveltejs/kit#12580 for more details

I understand that this should not work on node, and that is a very valid stance, but maybe restricting it so that it's not even possible to import is a bit too strict?

It would be very helpful if it was at least possible to import this on the server without getting the No known conditions error - even if you have no intentions to ever run it on the server, just because how SSR meta frameworks like SvelteKit works.

Eventually I hope that SvelteKit will allow for disabling of the SSR analyze/build step for client side only apps. but even then it might be a problem for hybrid apps? not sure.


TL;DR:

Would you be open to removing "node": null and replacing it with something that will:

  • not error when it's being imported in node
  • but instead error when it's being ran in node?

This will make it possible to import e.g. the WebGPU version during static analysis on the server, but it would still prevent users from running e.g. WebGPU code on the server.

Thanks, would love to hear your thoughts on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api:Javascript issues related to the Javascript API ep:WebGPU ort-web webgpu provider ep:Xnnpack issues related to XNNPACK EP platform:web issues related to ONNX Runtime web; typically submitted using template
Projects
None yet
Development

No branches or pull requests

3 participants