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.

Development mode transpiles correctly while build doesn't.

See original GitHub issue

Describe the bug

I am using the following dependency: https://github.com/bitcoinjs/bip39

This loads bip39 and works perfectly when running yarn dev(I did have to use some work arounds to get it running), when building it breaks.

I’d like to understand why this works under development but not when I build the entire project. It seems to be related to commonjs because bip39 uses require. This is perfectly transpiled when using development but breaks when build.

Reproduction

Miminal reproduction: https://github.com/jimbertools/vite-bip39 This loads bip39 and works perfectly when running yarn dev(I did have to use some work arounds to get it running), when building it breaks.

System Info

╰─❯ npx envinfo --system --npmPackages '{vite,@vitejs/*}' --binaries --browsers


npx: installed 1 in 0.904s

  System:
    OS: Linux 5.13 Pop!_OS 20.04 LTS
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Memory: 23.09 GB / 30.98 GB
    Container: Yes
    Shell: 5.8 - /usr/bin/zsh
  Binaries:
    Node: 14.18.1 - /usr/bin/node
    Yarn: 1.22.15 - ~/.yarn/bin/yarn
    npm: 6.14.15 - /usr/bin/npm
  Browsers:
    Chrome: 96.0.4664.45
    Firefox: 94.0

Used Package Manager

yarn

Logs

No response

Validations

Issue Analytics

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

github_iconTop GitHub Comments

6reactions
FPG-Alancommented, Feb 10, 2022

Similarly, I have encountered the same problem. In my case, the package that is causing this issue is: dagre which is a legacy commonJS package and is no longer maintained.

My app works perfectly in development mode but breaks in production when it is build by rollup with the following error:

Uncaught TypeError: Cannot read properties of undefined (reading 'Graph')
    at vendor.dec94905.js:503

This issue only appears in version 2.6.3 and up. It does not appear in version 2.6.2. I believe this is related to this pull request: #5173

Rollup commonjs plugin set ignoreTryCatch default as true at version 21.0.0(use safe default value for ignoreTryCatch), and vite upgrade commonjs plugin to this version at it’s 2.6.3 version(#5173), maybe this is the cause of the problem.

Try to set ignoreTryCatch to false in vite.config and the problem maybe solved:

defineConfig({
  ...
  build: {
    commonjsOptions: {
      ignoreTryCatch: false,
    }
  }
  ...
})


Some libraries, including the one I use, may use require to import other libraries and wrap them in try catch statements, commonjs plugin under version 21.0.0 will translate this code to ESM by default but the new version will not.

3reactions
Mahdi-Esbaticommented, Nov 23, 2021

Similarly, I have encountered the same problem. In my case, the package that is causing this issue is: dagre which is a legacy commonJS package and is no longer maintained.

My app works perfectly in development mode but breaks in production when it is build by rollup with the following error:

Uncaught TypeError: Cannot read properties of undefined (reading 'Graph')
    at vendor.dec94905.js:503

This issue only appears in version 2.6.3 and up. It does not appear in version 2.6.2. I believe this is related to this pull request: #5173

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript transpile errors appear only on production server
I am cloning my repo during the deploy process so I can't do this unless I include my build in my repo. Switch...
Read more >
Troubleshooting - Turborepo
My dependency isn't being built correctly. Are you properly bundling and transpiling the dependency before building the application?
Read more >
How to transpile ES modules with webpack and Node.js
Learn how webpack interacts with and supports ES modules in this deep dive tutorial on transpilation in Node.js.
Read more >
TypeScript Compiling with Visual Studio Code
TypeScript is a typed superset of JavaScript that transpiles to plain JavaScript. It offers classes, modules, and interfaces to help you build robust ......
Read more >
Comparing the New Generation of Build Tools - CSS-Tricks
It doesn't provide many of the developer conveniences you might find ... to start as server and run esbuild's watch mode at the...
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