question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CJS dependencies are not being pre-bundled

See original GitHub issue

Describe 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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
bestickleycommented, Mar 10, 2022

@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!

0reactions
bluwycommented, Mar 10, 2022

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 prebundling gboost-ui, it encounters imports to JSX files, these files are considered “external”, so they are not prebundled and instead point to the raw path inside node_modules instead. So probably the best way forward now is to have gboost-ui export compiled JSX (into .js) files instead, unless plugin-react supports https://github.com/vitejs/vite/pull/6801.

Of course, I could be wrong too as I’m not using React a lot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency Pre-Bundling - Vite
If an existing cache is not found, Vite will crawl your source code and automatically discover dependency imports (i.e. "bare imports" that expect...
Read more >
Third-party dependencies - Shopify.dev
When bundling third-party dependencies, you might see errors in development or production related to the incorrect bundle type being used from the package....
Read more >
@rollup/plugin-commonjs - npm
Some modules contain dynamic require calls, or require modules that contain circular dependencies, which are not handled well by static imports.
Read more >
Snowpack v3.0
Pre-bundled streaming imports - Import any npm package, on-demand. ... and manages their JavaScript dependencies locally using a package ...
Read more >
A note on Vite, a very fast dev build tool (II) - inDepthDev
For dependencies which are supposed to run in the browser (non-dev dependencies), ... Vite being a node application, CJS distributions are welcomed for ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found