Skip to content

Package exports eg @/pkg/foo not resolved locally #19292

@TeemuKoivisto

Description

@TeemuKoivisto

Describe the bug

So I have a local package @/lib in pnpm monorepo which works and I'm able to use it correctly. However, the second I add another "exports" definition to its package.json, Vite (or more specifically it seems Rollup) fails to import it.

error during build:
[vite]: Rollup failed to resolve import "@/lib/schemas" from "/Users/teemu/git_projects/omat/vite-module-not-found-bug/packages/bug/src/index.ts".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to

Using just @/lib works fine. It's the subpathing @/lib/schemas that makes Rollup/Vite go haywire. If I externalize it, as the log suggests it, this doesn't work:

      external: [
        ...Object.keys(pkg.dependencies || {}),
      ],

Because, as you should realize, the @/lib value does not externalize @/lib/schemas. So what you should do is:

      external: [
         '@/lib/schemas'
      ],

But this is in many ways extremely awkward and, I think, undefined behavior. @/lib package names are absolutely valid locally as they are as global npm packages. And "exports" should work as intended no matter what the package name is, as long as it valid. The fact that this specific pattern blows up the build with a vague error that gives no clue what's going on is terrible.

I might have encountered this bug before but probably thought it was going to be fixed one day.

Using my-lib as the package name with my-lib/schema export works flawlessly, so it's the @/lib pattern at fault here.

Reproduction

https://github.com/TeemuKoivisto/vite-module-not-found-bug

Steps to reproduce

git clone https://github.com/TeemuKoivisto/vite-module-not-found-bug
pnpm i
pnpm pnpm --filter lib --filter my-lib build
pnpm --filter bug build

System Info

System:
    OS: macOS 15.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 84.16 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.11.0 - ~/.nvm/versions/node/v22.11.0/bin/node
    npm: 10.9.0 - ~/.nvm/versions/node/v22.11.0/bin/npm
    pnpm: 9.12.3 - ~/Library/pnpm/pnpm
    bun: 1.1.31 - /opt/homebrew/bin/bun
  Browsers:
    Chrome: 131.0.6778.265
    Safari: 18.1
  npmPackages:
    vite: ^6.0.11 => 6.0.11

Used Package Manager

pnpm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    has workaroundp2-edge-caseBug, but has workaround or limited in scope (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions