vite: Build issues with v3. TypeError: Class extends value undefined is not a constructor or null
Describe the bug
After updating to version 3 we started getting the following error in our bundle script:
account_multisig.js:48 Uncaught TypeError: Class extends value undefined is not a constructor or null
at account_multisig.js:48:41
at requireAccount_multisig (account_multisig.js:453:2)
at common-index.js:34:31
at requireCommonIndex (common-index.js:53:136)
at requireRpc_errors (rpc_errors.js:10:24)
at json-rpc-provider.js:20:22
at requireJsonRpcProvider (json-rpc-provider.js:356:27)
at index.js:9:29
at requireProviders (index.js:12:131)
at requireAccount (account.js:10:21)
This does not occur in v2. It does also not happen while we are in “dev” mode in v3.
It appears to be an issue with how things are being bundled in the newer version- as the new code is unable to create a specific class in a library near-api-js as it used to, and the output code does look a little strange.
Reproduction
https://github.com/lostpebble/vite-class-undefined-error
System Info
System:
OS: Windows 10 10.0.19044
CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Memory: 4.29 GB / 15.79 GB
Binaries:
Node: 16.15.1 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.5 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 8.11.0 - C:\Program Files\nodejs\npm.CMD
Browsers:
Chrome: 104.0.5112.81
Edge: Spartan (44.19041.1266.0), Chromium (104.0.1293.54)
Internet Explorer: 11.0.19041.1566
npmPackages:
vite: ^3.0.8 => 3.0.8
Used Package Manager
yarn
Logs
No response
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 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.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 13
- Comments: 22 (4 by maintainers)
Links to this issue
Commits related to this issue
- update vite.config.js. vite 的 dev 与 build 的 default export module 输出不一致。 举例引入 quill-blot-formatter 包时, dev 输出为 commonJs 格式,build 输出为 es module 格式。 此提交为vite github issue 解决方案。相关issue如下: https://git... — committed to pig-mesh/pig-ui by deleted user a year ago
- Tweak vite config to fix prod "TypeError: Class extends value undefined is not a constructor or null" error was happening in prod after building. Take a look at this [vite issue](https://github.com/v... — committed to AquiGorka/loambuild-home by AquiGorka a year ago
It seems it’s related to @rollup/plugin-commonjs v22.
For a workaround, use Esbuild Deps Optimization at Build Time. Set
optimizeDeps.disabled = falseandbuild.commonjsOptions.include = [].I think you forgot to set
build.commonjsOptions.include = []which disables commonjs plugin v22 used inside Vite. https://stackblitz.com/edit/github-g2azcc?file=package.json,vite.config.tsThere’s some tree-shaking issues with it but we’re going to make it default in future. See https://vitejs.dev/blog/announcing-vite3.html#esbuild-deps-optimization-at-build-time-experimental
I had an absolute field day with the garbage that this error is, I’ll spare you with the details and tell you how to solve it.
Massive props to the Metaplex team for not adding it in the Vite starter example, but hiding it behind a commit that was done in November to the file itself, not the example. I’ll probably make a PR for them, considering they haven’t done anything about it since November, until then enjoy.
Also, for the Buffer problem here’s my complete config. But you most likely only need the
injectpart inrollupOptions, thebuffermodule installed.And inside the
index.htmlbefore your main bundle.Ok, I found the issue for my case. It’s because the project is using an old version of Vite (2.x), indirectly imported by quasar. Not sure why a newly-created quasar project will use an old version of quasar toolkit and vite. After manually updating their versions and an override (see here), the issue is gone.
Hope it’s useful for others.
got en error “Uncaught TypeError: EventEmitter is not a constructor” using vite.
anyway, fixed by run
npm install events. hope it will help someone.see https://github.com/mapbox/mapbox-gl-geocoder/issues/441#issuecomment-1021774132
Please check the import and export in the file. 1.check on which file it throwing error 2.console the import and check whether it is undefined or not if not then go ahead 3.remove { } from import statement.
@adueck Solution worked for me.
Checkout this full vite config incase you have any other issues that might be related https://gist.github.com/FbN/0e651105937c8000f10fefdf9ec9af3d
I had to add more config from the link.
Thanks so much @sapphi-red ! The
vite.config.tsat that stackblitz link is the one thing that got it working perfectly for me. Now I can import cjs stuff and stuff withglobaland it’s working no problem, using Vite v4. Now I can finally migrate tovitefromcreate-react-app. It would be nice if there was some kind of default that could handle this. That’s a pretty complex config file and hard to come by.@sapphi-red, is there a simpler/better way to do this in Vite v4?
Here’s the magic
vite.config.tsfor anyone with this problem:@val-samonte I have the same issue as you with Buffer becoming undefined. Any workaround?
Edit: I fixed the issue by setting
commonjsOptions: { transformMixedEsModules: true, }Source: https://github.com/chnejohnson/vue-dapp/issues/20#issuecomment-973698464