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.

ReferenceError: Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization

See original GitHub issue

What version of Next.js are you using?

12.0.7

What version of Node.js are you using?

14.18.1

What browser are you using?

N/A

What operating system are you using?

macOS, Alpine

How are you deploying your application?

next start

Describe the Bug

A module which imports an es module and throws an exception at the top level has the exception swallowed and a different (vastly less helpful) exception thrown instead. I am pretty confident that this is a webpack bug, but since next vendors webpack, I’m reporting here.

Expected Behavior

When a module throws an exception during initialization, the exception should be passed to the nearest user exception handler, or crash the application. Instead, a different exception is being thrown due to webpack-generated code.

To Reproduce

Create a file lib/test.ts like the following; require it from any page (I used an API page, may be relevant); request that page.

import { z } from "zod";
throw new Error("error");
export default { z };

You will see the following error in the console, without any stack trace:

error - ReferenceError: Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization

Things to note:

  1. “zod” could be replaced by any esmodule package. The root cause is that the code generated by webpack uses a dynamic import to import the code, causing to to be an async module.
  2. Exporting { z } is just so that the import doesn’t get tree-shaken-away.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:12
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

19reactions
luixocommented, Jan 26, 2022

Seems like this problem is connected to esmExternals experimental feature, which is enabled by default.

I have this problem on 12.0.7 without any underlying error, just having ESM module imported in the project. Turning off experimental feature fixed the issue.

// next.config.js
module.exports = {
  ...,
  experimental: {
    esmExternals: false
  }
}
3reactions
humble-barnacle001commented, Jan 28, 2022

I am facing the same issue in v12.0.9 but in my case surprisingly, next dev works and also deploy on Vercel platform works but somehow the local build using next build fails

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Uncaught ReferenceError: Cannot access ...
Uncaught ReferenceError: Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization. This is the code of the class were I am trying to ...
Read more >
ReferenceError: Cannot access '<something>' before ... - GitHub
Enabling both innerGraph and usedExports seems to allow one to work around the problem. If the current behavior is a bug, please provide...
Read more >
Cannot access '__WEBPACK_DEFAULT_EXPORT__' before ...
solution. [Resolve] ReferenceError: Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization on Next.js. // next.config.js module.exports = { ...
Read more >
Cannot access '__WEBPACK_DEFAULT_EXPORT__' before ...
Uncaught ReferenceError: Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization. I am having issues while trying to add token on axios request.
Read more >
cannot access '__webpack_default_export__' ... - You.com
Uncaught ReferenceError: Cannot access 'WEBPACK_DEFAULT_EXPORT' before initialization but showing that everything is working fine in the terminal.
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