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

Transitive dependencies of linked dependencies are bundled in SSR build #19403

Closed
7 tasks done
haines opened this issue Feb 11, 2025 · 3 comments
Closed
7 tasks done

Transitive dependencies of linked dependencies are bundled in SSR build #19403

haines opened this issue Feb 11, 2025 · 3 comments

Comments

@haines
Copy link

haines commented Feb 11, 2025

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

@botafi
Copy link

botafi commented Feb 14, 2025

I have this issue when importing Sentry with Nuxt as it requires require-in-the-middle

@hi-ogawa
Copy link
Collaborator

This is somewhat expected since externalizing transitive dependency won't be necessary visible from the bundled application directory. You have to manually mark ssr.external and also install the transitive dependency as application dependency.

This is not the case for SSR dev, so this is technically dev/build inconsistency, but if dev is aligned with build, then it still need to require having externalized transitive dependency explicitly as application dependency. Last time I checked, that's basically what Next.js does and they have a warning for that case. Here is a reference if anyone is interested https://github.com/hi-ogawa/reproductions/tree/main/next-external-import.

 ⚠ ./node_modules/.pnpm/@vitejs+test-external-indirect@file+deps+external-indirect/node_modules/@vitejs/test-external-indirect
Package @node-rs/bcrypt can't be external
The request @node-rs/bcrypt matches serverExternalPackages (or the default list).
The request could not be resolved by Node.js from the project directory.
Packages that should be external need to be installed in the project directory, so they can be resolved from the output files.
Try to install it into the project directory by running npm install @node-rs/bcrypt from the project directory.

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Feb 25, 2025

It looks like OP has addressed the issue on require-in-the-middle side nodejs/require-in-the-middle#111, so let me close this for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants