-
-
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
Problem with Vite and CommonJS package in monorepo #5668
Comments
Just discovered that changing import statement to import * as lib from "@vite-mono/lib-cjs"; and then invocation to
allows me to build it, but during runtime it throws o('p', {
children: `Hello Vite ${ (void 0) () }!`
}), |
Ok, I was able to fix it by adding my cjs lib into import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
optimizeDeps: {
include: ["@vite-mono/lib-cjs"],
},
build: {
commonjsOptions: {
include: [/lib-cjs/, /node_modules/],
},
},
}); Definitely was not obvious. Maybe content of |
Thanks for the workaround @doomsower, I switched back from ESM to CommonJS inside my monorepo and hit this issue. I added a trailing slash
|
I still have issues with this workaround. If I change sources from monorepo dependencies, changes are not affected. It seems it bundles the dependency at server start, but doesn't bundle them again afterwards. |
I have a solution that fixes this. If someone here wants to sponsor me, I'll submit a PR. 😄 |
I am also running into this issue with a mono repo, I have a NestJS |
Could you tell us more about this solution? |
Done! :) |
@patak-dev can you explain why the documentation PR would mean this could be closed? |
I think @bluwy closed it with the doc PR because it showed a way to work around the issue, using: export default defineConfig({
optimizeDeps: {
include: ['linked-dep']
},
build: {
commonjsOptions: {
include: [/linked-dep/, /node_modules/]
}
}
}) @aleclarson, maybe you had a better idea to tackle this issue, please reopen it in that case. |
Yeah. I think the docs show more of how Vite expects local linked packages to be ESM. And if it's CJS instead, there is a "not so nice" way around it, mainly to push the ecosystem towards ESM. |
Thank you @fwouts, I will submit a PR sometime this week. 😄 |
Describe the bug
I'm trying to migrate CRA to Vite in a monorepo and couldn't make it work with shared cjs packages. So I built a minimal reproduction.
A basic monorepo with 3 packages
/packages/app
)/packages/lib-cjs
)/packages/lib-esm
)Vite app depends on both libraries and also a copy of CommonJS library is installed from github (https://github.com/doomsower/vite-cjs-test-lib).
Vite app builds and launches successfully if I use ESM library or CommonJS library from github.
Hovewer, with local cjs library (import statement)
vite build
fails with error:'test' is not exported by ../lib-cjs/dist/index.js, imported by src/App.tsx
From documentation it seems to me that just adding CommonJS package's name (
@vite-mono/lib-cjs
) tooptimizeDeps.include
should do it. I've searched around and couldn't find a definitive answer whether it's a bug or inteded behavior. Nor could I find any workaround.Full error log
Pnpm links
Reproduction
https://github.com/doomsower/vite-mono-test/
System Info
Used Package Manager
pnpm
Logs
Validations
The text was updated successfully, but these errors were encountered: