import customized Document from library causes 'Constructor invoked without 'new'' error
See original GitHub issueBug report
- Make a library which included a customized Document(_document.js).
- import the customized Document into another project. throws
TypeError: Class constructor Document cannot be invoked without 'new'
error.
There’s no error before next9
To Reproduce
https://github.com/thundermiracle/next-lib-bug
Screenshots
System information
- OS: [Windows]
- Browser (if applies) [chrome,]
- Version of Next.js: [9.0.2 and canary]
Issue Analytics
- State:
- Created 4 years ago
- Reactions:11
- Comments:21 (13 by maintainers)
Top Results From Across the Web
import customized Document from library causes 'Constructor ...
js). import the customized Document into another project. throws TypeError: Class constructor Document cannot be invoked without 'new' error.
Read more >Javascript ES6 TypeError: Class constructor Client cannot be ...
The problem is that the class extends native ES6 class and is transpiled to ES5 with Babel. Transpiled classes cannot extend native classes, ......
Read more >Errors | Node.js v19.3.0 Documentation
The error.cause property is typically set by calling new Error(message, { cause }) . It is not set by the constructor if the...
Read more >new operator - JavaScript - MDN Web Docs - Mozilla
When a function is called with the new keyword, the function will be used as a constructor. new will do the following things:...
Read more >ES6 Class Mocks - Jest
Jest can be used to mock ES6 classes that are imported into files you want to test. ES6 classes are constructor functions with...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I just took a deeper look at this.
The reason this issue is happening is because
next
must be compiled by webpack.When you import it indirectly in
next-mui-helper
(which is not bundled, as it’s innode_modules
), webpack doesn’t get the chance to bundle or compilenext/**
. This package then just gets handled with typical Node imports.To solve this, you’ll need to customize your webpack configuration to ensure that
next-mui-helper
is compiled by webpack and not externalized, such thatnext
imports are properly seen by the build.Do note that this extension of Document is not a supported behavior and is very prone to break release-by-release.
Note: I had this error this morning when upgrading to 9.0.3, but removed
@babel/preset-env
frompresets
in my .babelrc file and I am no longer receiving this error.In retrospect, not really sure why we had
@babel/preset-env
in there. EDIT: Our project has a custom_document
and custom_app
, but we don’t export it to other projects.