Skip to content

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

@segevfiner

Description

@segevfiner

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions