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.

import customized Document from library causes 'Constructor invoked without 'new'' error

See original GitHub issue

Bug report

  1. Make a library which included a customized Document(_document.js).
  2. 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

image

System information

  • OS: [Windows]
  • Browser (if applies) [chrome,]
  • Version of Next.js: [9.0.2 and canary]

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:11
  • Comments:21 (13 by maintainers)

github_iconTop GitHub Comments

4reactions
Timercommented, Sep 26, 2019

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 in node_modules), webpack doesn’t get the chance to bundle or compile next/**. 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 that next 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.

4reactions
jaslaksoncommented, Aug 1, 2019

Note: I had this error this morning when upgrading to 9.0.3, but removed @babel/preset-env from presets in my .babelrc file and I am no longer receiving this error.

{
  "presets": [
-    "@babel/preset-env",
    "next/babel",
  ],
  "compact": true,
...(other config options for styled components, etc)

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.

Read more comments on GitHub >

github_iconTop 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 >

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