next.js: ReferenceError: Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization
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:
- “zod” could be replaced by any esmodule package. The root cause is that the code generated by webpack uses a dynamic
importto import the code, causing to to be an async module. - Exporting
{ z }is just so that the import doesn’t get tree-shaken-away.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 12
- Comments: 15 (4 by maintainers)
Commits related to this issue
- update webpack (#33831) https://github.com/webpack/webpack/releases/tag/v5.68.0 fixes https://github.com/vercel/next.js/issues/32360 — committed to vercel/next.js by sokra 2 years ago
- update webpack (#33831) https://github.com/webpack/webpack/releases/tag/v5.68.0 fixes https://github.com/vercel/next.js/issues/32360 — committed to natew/next.js by sokra 2 years ago
Seems like this problem is connected to
esmExternalsexperimental 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.
I am facing the same issue in v12.0.9 but in my case surprisingly,
next devworks and also deploy on Vercel platform works but somehow the local build usingnext buildfailsI’m also facing this issue.
I’m seeing this as well. I’m pretty sure its a bug that was released in 12.0.2 that blows up the first server side render:
Try rolling back to 12.0.1 - This fixed it for me.
Just confirming @luixo’s fix worked for me - I was getting it when importing graphql resolvers and schemas on the apollo-server example.
Well, Chrome is the interactive debugger for Node too, so strangely the answer is “yes” 🙂 You’ll want to run next using something like
node --inspect-brk node_modules/.bin/next dev, then follow the instructions here to launch the interactive debugger.You may also have luck adding
console.logstatements before and after every import statement, and then recursively going down the stack until you find the offending module.Note: I am not affiliated with Next or Vercel; this is just the basic debugging steps that I used identifying the problem myself before creating this bug report.