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

Vite prefers module main field over exports browser field in library package.json #10103

Closed
7 tasks done
segevfiner opened this issue Sep 13, 2022 · 4 comments
Closed
7 tasks done

Comments

@segevfiner
Copy link
Contributor

Describe the bug

I have a library that defines:

{
  "name": "lib",
  "main": "./node.js",
  "module": "./node.mjs",
  "browser": "./browser.mjs",
  "exports": {
    "browser": "./browser.mjs",
    "import": "./node.mjs",
    "require": "./node.js"
  }
}

Contrary to the docs and expected behavior. Vite picks "module": "./node.mjs" and I get Node ESM printed. According to the docs, and Node.JS behavior, exports should be preferred and used when specified (P.S. Vite AFAIK doesn't use the browser main field).

Reproduction

https://stackblitz.com/edit/vitejs-vite-sh5wuy?file=lib/package.json

System Info

System:
    OS: macOS 12.5.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 135.59 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.17.0 - ~/.nvm/versions/node/v16.17.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.17.0/bin/yarn
    npm: 8.19.1 - ~/.nvm/versions/node/v16.17.0/bin/npm
  Browsers:
    Chrome: 105.0.5195.102
    Safari: 15.6.1

Used Package Manager

pnpm

Logs

Click to expand!
Node ESM

It should be Browser ESM.

Validations

@Drafter500
Copy link

I have the same issue, except that even module path from package.json is ignored, and main is always used instead. This happens for executing a build (vite-build command) and when running a dev server, it picks the right one.

@segevfiner
Copy link
Contributor Author

Ooofff the way Vite resolves entry points from package.json just seems all over the place and doesn't really match how Node or other bundlers work... Wish this could get some traction so I won't end up with so many weird cases of Vite trying to bundle Node code instead of browser. Heck, even the official @vue/devtools package is effected...

@bluwy
Copy link
Member

bluwy commented Mar 25, 2023

This should be fixed by #11595

@bluwy bluwy closed this as not planned Won't fix, can't repro, duplicate, stale Mar 25, 2023
@segevfiner
Copy link
Contributor Author

Well technically resolved. Not "not planned" but nevermind.

@bluwy bluwy closed this as completed Mar 25, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Apr 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants