CJS dependencies are not being pre-bundled
See original GitHub issueDescribe the bug
I’ve got an example Vite app here that has an ESM library, gboost-ui, which has CJS dependencies and peerDependencies which are CJS. When I run pnpm dev
I get an error in the browser: The requested module '/node_modules/.pnpm/prop-types@15.8.1/node_modules/prop-types/index.js?v=f8148f89' does not provide an export named 'default'
. So I try adding prop-types
to optimizeDeps.include
but then I get: The requested module '/@fs/Users/stickb/Code/scratch/tdmp-edge-mgmt-ui/node_modules/.pnpm/zen-observable@0.8.15/node_modules/zen-observable/index.js?v=2906f0d0' does not provide an export named 'default'
. so I add zen-observable
to optimizeDeps.includes
. But then Uncaught SyntaxError: The requested module '/@fs/Users/stickb/Code/scratch/tdmp-edge-mgmt-ui/node_modules/.pnpm/url@0.11.0/node_modules/url/url.js?v=f14ea013' does not provide an export named 'format'
and so forth. I think this is happening for all CJS dependencies gboost-ui imports. Why isn’t vite pre-bundling these dependencies?
Strangely, if I clone gboost-ui and run pnpm add ../../path/to/gboost-ui
it works.
I saw the issue: https://github.com/vitejs/vite/issues/3024 where it says:
if it’s not in node_modules it does not get optimized
So I tried install with npm (since PNPM symlinks) but that didn’t work either 😕
Reproduction
https://github.com/bestickley/vite-gboost-ui-error
System Info
System:
OS: macOS 12.2.1
CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Memory: 58.18 MB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.13.2 - ~/Library/pnpm/node
npm: 8.1.2 - ~/Library/pnpm/npm
Browsers:
Chrome: 99.0.4844.51
Firefox: 91.6.0
Safari: 15.3
npmPackages:
@vitejs/plugin-react: ^1.0.7 => 1.2.0
vite: ^2.8.0 => 2.8.6
Used Package Manager
pnpm
Logs
vite:config bundled config file loaded in 207.83ms +0ms
vite:config using resolved config: {
vite:config plugins: [
vite:config 'vite:pre-alias',
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-proxy',
vite:config 'vite:css',
vite:config 'vite:esbuild',
vite:config 'vite:json',
vite:config 'vite:wasm',
vite:config 'vite:worker',
vite:config 'vite:worker-import-meta-url',
vite:config 'vite:asset',
vite:config 'vite:define',
vite:config 'vite:css-post',
vite:config 'vite:client-inject',
vite:config 'vite:import-analysis'
vite:config ],
vite:config server: {
vite:config preTransformRequests: true,
vite:config fs: { strict: true, allow: [Array], deny: [Array] }
vite:config },
vite:config resolve: { dedupe: [ 'react', 'react-dom' ], alias: [ [Object], [Object] ] },
vite:config optimizeDeps: {
vite:config include: [ 'react/jsx-dev-runtime' ],
vite:config esbuildOptions: { keepNames: undefined, preserveSymlinks: undefined }
vite:config },
vite:config configFile: '/Users/stickb/Code/scratch/vite-gboost-ui-error/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 server: {}
vite:config },
vite:config root: '/Users/stickb/Code/scratch/vite-gboost-ui-error',
vite:config base: '/',
vite:config publicDir: '/Users/stickb/Code/scratch/vite-gboost-ui-error/public',
vite:config cacheDir: '/Users/stickb/Code/scratch/vite-gboost-ui-error/node_modules/.vite',
vite:config command: 'serve',
vite:config mode: 'development',
vite:config isProduction: false,
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 preview: {
vite:config port: undefined,
vite:config strictPort: undefined,
vite:config host: undefined,
vite:config https: undefined,
vite:config open: undefined,
vite:config proxy: undefined,
vite:config cors: undefined,
vite:config headers: undefined
vite:config },
vite:config env: { BASE_URL: '/', MODE: 'development', DEV: true, PROD: false },
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 packageCache: Map(0) {},
vite:config createResolver: [Function: createResolver],
vite:config worker: {
vite:config format: 'iife',
vite:config plugins: [
vite:config [Object], [Object],
vite:config [Object], [Object],
vite:config [Object], [Object],
vite:config [Object], [Object],
vite:config [Object], [Object],
vite:config [Object], [Object],
vite:config [Object], [Object],
vite:config [Object], [Object]
vite:config ],
vite:config rollupOptions: {}
vite:config }
vite:config } +6ms
vite:deps Hash is consistent. Skipping. Use --force to override. +0ms
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/core 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:8 (1 by maintainers)
Top GitHub Comments
@bluwy, thank you so much for your insight. This explains everything. To change gboost-ui’s output to .js from .jsx was trivial. In tsconfig.json:
"jsx": "preserve" => "react-jsx"
Thanks, again!This looks like a similar issue as https://github.com/vitejs/vite/issues/3910. It seems that
gboost-ui
is exporting raw JSX files, and I’m not sure if that has been historically supported in Vite. The issue is that in the prebundling step, when prebundlinggboost-ui
, it encounters imports to JSX files, these files are considered “external”, so they are not prebundled and instead point to the raw path insidenode_modules
instead. So probably the best way forward now is to havegboost-ui
export compiled JSX (into.js
) files instead, unlessplugin-react
supports https://github.com/vitejs/vite/pull/6801.Of course, I could be wrong too as I’m not using React a lot.