Describe the bug
I have a pnpm monorepo containing packages app and shared. app depends on shared.
My expectation is that
- Vite will bundle
shared in app's SSR build for HMR, because it is a linked dependency and therefore is not externalized.
shared's dependencies in shared/node_modules remain external.
What actually happens is that Vite bundles shared's dependencies into app's SSR build as well as shared itself. This breaks our app because shared pulls in a CJS module (require-in-the-middle) which explodes if it is loaded in an ESM context (nodejs/require-in-the-middle#110).
Reproduction
https://github.com/haines/vite-ssr-externals-repro
Steps to reproduce
Clone the repo and run pnpm run repro (which is equivalent to running pnpm install followed by pnpm run build and pnpm run preview in the app).
The SSR bundle packages/app/dist/server/entry-server.js will include require-in-the-middle rather than importing it from packages/shared/node_modules. This will cause the server to blow up with
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'vite-ssr-externals-repro/packages/app/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
System Info
System:
OS: macOS 15.3
CPU: (14) arm64 Apple M4 Pro
Memory: 252.14 MB / 48.00 GB
Shell: 3.7.1 - /opt/homebrew/bin/fish
Binaries:
Node: 22.13.1 - ~/.local/share/nvm/v22.13.1/bin/node
pnpm: 10.3.0 - ~/.local/share/nvm/v22.13.1/bin/pnpm
npmPackages:
vite: ^6.0.1 => 6.1.0
Used Package Manager
pnpm
Logs
No response
Validations
Describe the bug
I have a pnpm monorepo containing packages
appandshared.appdepends onshared.My expectation is that
sharedinapp's SSR build for HMR, because it is a linked dependency and therefore is not externalized.shared's dependencies inshared/node_modulesremain external.What actually happens is that Vite bundles
shared's dependencies intoapp's SSR build as well asshareditself. This breaks our app becausesharedpulls in a CJS module (require-in-the-middle) which explodes if it is loaded in an ESM context (nodejs/require-in-the-middle#110).Reproduction
https://github.com/haines/vite-ssr-externals-repro
Steps to reproduce
Clone the repo and run
pnpm run repro(which is equivalent to runningpnpm installfollowed bypnpm run buildandpnpm run previewin theapp).The SSR bundle
packages/app/dist/server/entry-server.jswill includerequire-in-the-middlerather than importing it frompackages/shared/node_modules. This will cause the server to blow up withSystem Info
System: OS: macOS 15.3 CPU: (14) arm64 Apple M4 Pro Memory: 252.14 MB / 48.00 GB Shell: 3.7.1 - /opt/homebrew/bin/fish Binaries: Node: 22.13.1 - ~/.local/share/nvm/v22.13.1/bin/node pnpm: 10.3.0 - ~/.local/share/nvm/v22.13.1/bin/pnpm npmPackages: vite: ^6.0.1 => 6.1.0Used Package Manager
pnpm
Logs
No response
Validations