Problem with Vite and CommonJS package in monorepo
See original GitHub issueDescribe 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
- Vite app (
/packages/app) - CommonJS library (
/packages/lib-cjs) - ESM Library (
/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) to optimizeDeps.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 build 20:56:16
> @vite-mono/app@0.0.0 build /Volumes/Projects/_temp/vite-mono/packages/app
> tsc && vite build
vite v2.6.14 building for production...
transforming (30) ../../node_modules/.pnpm/scheduler@0.20.2/node_modules/scheduler/cjs/scheduler.production.min.jsError when using sourcemap for reporting an error: Can't resolve original location of error.
✓ 34 modules transformed.
'test' is not exported by ../lib-cjs/dist/index.js, imported by src/App.tsx
file: /Volumes/Projects/_temp/vite-mono/packages/app/src/App.tsx:4:9
2: import logo from "./logo.svg";
3: import "./App.css";
4: import { test } from "@vite-mono/lib-cjs";
^
5: import { jsx as _jsx } from "react/jsx-runtime";
6: import { jsxs as _jsxs } from "react/jsx-runtime";
error during build:
Error: 'test' is not exported by ../lib-cjs/dist/index.js, imported by src/App.tsx
at error (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:158:30)
at Module.error (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:12382:16)
at Module.traceVariable (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:12767:29)
at ModuleScope.findVariable (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:11559:39)
at FunctionScope.findVariable (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:6930:38)
at ChildScope.findVariable (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:6930:38)
at Identifier.bind (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:6419:40)
at CallExpression.bind (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:5025:23)
at CallExpression.bind (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:9396:15)
at TemplateLiteral.bind (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:5021:31)
ELIFECYCLE Command failed with exit code 1.
Pnpm links
ls -la node_modules/@vite-mono 20:49:33
total 24
drwxr-xr-x 5 doomsower admin 170 Nov 13 20:47 .
drwxr-xr-x 11 doomsower admin 374 Nov 13 20:47 ..
lrwxr-xr-x 1 doomsower admin 16 Nov 13 19:37 lib-cjs -> ../../../lib-cjs
lrwxr-xr-x 1 doomsower admin 149 Nov 13 20:47 lib-cjs-remote -> ../../../../node_modules/.pnpm/github.com+doomsower+vite-cjs-test-lib@8c6e40dc2c8098d23a84f498dd1022d30f03ef3c/node_modules/@vite-mono/lib-cjs-remote
lrwxr-xr-x 1 doomsower admin 16 Nov 13 19:37 lib-esm -> ../../../lib-esm
Reproduction
https://github.com/doomsower/vite-mono-test/
System Info
System:
OS: macOS 11.5.1
CPU: (16) x64 Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
Memory: 910.94 MB / 32.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 14.18.1 - ~/.nvm/versions/node/v14.18.1/bin/node
Yarn: 1.22.17 - ~/.nvm/versions/node/v14.18.1/bin/yarn
npm: 8.1.3 - ~/.nvm/versions/node/v14.18.1/bin/npm
Watchman: 2021.10.11.00 - /usr/local/bin/watchman
Browsers:
Brave Browser: 95.1.31.88
Chrome: 95.0.4638.69
Firefox: 93.0
Firefox Developer Edition: 95.0
Safari: 14.1.2
npmPackages:
@vitejs/plugin-react: ^1.0.0 => 1.0.8
vite: ^2.6.4 => 2.6.14
Used Package Manager
pnpm
Logs
pnpm vite build --debug 127 ↵ 21:34:59
vite:config bundled config file loaded in 112.36ms +0ms
vite:config using resolved config: {
vite:config plugins: [
vite:config 'alias',
vite:config 'vite:react-babel',
vite:config 'vite:react-refresh',
vite:config 'vite:react-jsx',
vite:config 'vite:modulepreload-polyfill',
vite:config 'vite:resolve',
vite:config 'vite:html-inline-script-proxy',
vite:config 'vite:css',
vite:config 'vite:esbuild',
vite:config 'vite:json',
vite:config 'vite:wasm',
vite:config 'vite:worker',
vite:config 'vite:asset',
vite:config 'vite:define',
vite:config 'vite:css-post',
vite:config 'vite:build-html',
vite:config 'commonjs',
vite:config 'vite:data-uri',
vite:config 'rollup-plugin-dynamic-import-variables',
vite:config 'vite:asset-import-meta-url',
vite:config 'vite:build-import-analysis',
vite:config 'vite:esbuild-transpile',
vite:config 'vite:reporter',
vite:config 'vite:load-fallback'
vite:config ],
vite:config optimizeDeps: {
vite:config include: [ '@vite-mono/lib-cjs', 'react/jsx-dev-runtime' ],
vite:config esbuildOptions: { keepNames: undefined, preserveSymlinks: undefined }
vite:config },
vite:config build: {
vite:config target: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
vite:config polyfillModulePreload: true,
vite:config outDir: 'dist',
vite:config assetsDir: 'assets',
vite:config assetsInlineLimit: 4096,
vite:config cssCodeSplit: true,
vite:config cssTarget: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
vite:config sourcemap: false,
vite:config rollupOptions: {},
vite:config minify: 'esbuild',
vite:config terserOptions: {},
vite:config write: true,
vite:config emptyOutDir: null,
vite:config manifest: false,
vite:config lib: false,
vite:config ssr: false,
vite:config ssrManifest: false,
vite:config reportCompressedSize: true,
vite:config chunkSizeWarningLimit: 500,
vite:config watch: null,
vite:config commonjsOptions: { include: [Array], extensions: [Array] },
vite:config dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] }
vite:config },
vite:config resolve: { dedupe: [ 'react', 'react-dom' ], alias: [ [Object], [Object] ] },
vite:config configFile: '/Volumes/Projects/_temp/vite-mono/packages/app/vite.config.ts',
vite:config configFileDependencies: [ 'vite.config.ts' ],
vite:config inlineConfig: {
vite:config root: undefined,
vite:config base: undefined,
vite:config mode: undefined,
vite:config configFile: undefined,
vite:config logLevel: undefined,
vite:config clearScreen: undefined,
vite:config build: {}
vite:config },
vite:config root: '/Volumes/Projects/_temp/vite-mono/packages/app',
vite:config base: '/',
vite:config publicDir: '/Volumes/Projects/_temp/vite-mono/packages/app/public',
vite:config cacheDir: '/Volumes/Projects/_temp/vite-mono/packages/app/node_modules/.vite',
vite:config command: 'build',
vite:config mode: 'production',
vite:config isProduction: true,
vite:config server: { fs: { strict: undefined, allow: [Array] } },
vite:config env: { BASE_URL: '/', MODE: 'production', DEV: false, PROD: true },
vite:config assetsInclude: [Function: assetsInclude],
vite:config logger: {
vite:config hasWarned: false,
vite:config info: [Function: info],
vite:config warn: [Function: warn],
vite:config warnOnce: [Function: warnOnce],
vite:config error: [Function: error],
vite:config clearScreen: [Function: clearScreen],
vite:config hasErrorLogged: [Function: hasErrorLogged]
vite:config },
vite:config createResolver: [Function: createResolver]
vite:config } +5ms
vite v2.6.14 building for production...
transforming (23) ../../node_modules/.pnpm/object-assign@4.1.1/node_modules/object-assign/index.jsError when using sourcemap for reporting an error: Can't resolve original location of error.
✓ 34 modules transformed.
'test' is not exported by ../lib-cjs/dist/index.js, imported by src/App.tsx
file: /Volumes/Projects/_temp/vite-mono/packages/app/src/App.tsx:4:9
2: import logo from "./logo.svg";
3: import "./App.css";
4: import { test } from "@vite-mono/lib-cjs";
^
5: import { jsx as _jsx } from "react/jsx-runtime";
6: import { jsxs as _jsxs } from "react/jsx-runtime";
error during build:
Error: 'test' is not exported by ../lib-cjs/dist/index.js, imported by src/App.tsx
at error (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:158:30)
at Module.error (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:12382:16)
at Module.traceVariable (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:12767:29)
at ModuleScope.findVariable (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:11559:39)
at FunctionScope.findVariable (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:6930:38)
at ChildScope.findVariable (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:6930:38)
at Identifier.bind (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:6419:40)
at CallExpression.bind (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:5025:23)
at CallExpression.bind (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:9396:15)
at TemplateLiteral.bind (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:5021:31)
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (5 by maintainers)
Top Related StackOverflow Question
I have a solution that fixes this. If someone here wants to sponsor me, I’ll submit a PR. 😄
Ok, I was able to fix it by adding my cjs lib into
build.commonjsOptions.include:Definitely was not obvious. Maybe content of
optimizeDeps.includeshould be automatically added tobuild.commonjsOptions.include?