-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
CommonJS dependencies crash SSR server #9710
Comments
It seems like Vite has not supported ssr pre bundle yet. See https://github.com/vitejs/vite/blob/main/packages/vite/src/node/optimizer/optimizer.ts#L724 |
There is now support for prebundling SSR deps during dev, which should give a way out of these issues. You need to manually enable it, and add these dependencies to the
|
It didn't work, did I miss something? @patak-dev
|
Just in case @sun0day, |
I just clone https://github.com/rtsao/vite-bug-repro-ssr-pnp mentioned above, and add
to |
I'm aware of a workaround to forcibly externalize these deps (as they should be): I think the heuristics for whether dependencies should be externalized is broken somehow.
Why bundle server dependencies? Isn't this just overhead? |
Put together a fix: #9763 TL;DR: the current externalization method of attempting to resolve all nested deps from root is broken with pnpm and Yarn PnP |
@rtsao Thank you. This also appears to affect nx workspaces, in my case with the package d3. |
Thank you, this actually worked for me as Overall, it took several hours to track down the cause here - thank you for your PR on the subject. |
Run into this issue. I have case 2 with pnpm: But for some reason adding |
This is still a bug as of |
I've rebased the fix for this issue: #9763 and is passing CI again |
I add this config for |
Describe the bug
CommonJS dependencies encountered during SSR can crash the server (e.g.
ReferenceError: module is not defined
orReferenceError: require is not defined
, depending on which is first) if workspaces are used in some capacity.I have identified two specific scenarios where this will occur:
app
→cjs-workspace
)app
→other-workspace
→cjs-npm-dependency
)While possibly confusing, the first scenario is arguably intentional behavior because workspaces are typically implemented with symlinks and this logic indicates that symlinked packages should never be externalized:
vite/packages/vite/src/node/plugins/resolve.ts
Lines 656 to 659 in c6870f3
However, the second scenario is almost certainly a bug.
Reproduction
https://github.com/rtsao/vite-bug-repro-cjs-workspaces (for scenario 1)
https://github.com/rtsao/vite-bug-repro-ssr-pnp and https://github.com/rtsao/vite-bug-repro-ssr-pnpm (for scenario 2)
System Info
Used Package Manager
yarn
Logs
Click to expand!
Validations
The text was updated successfully, but these errors were encountered: