ReferenceError: Cannot access __WEBPACK_DEFAULT_EXPORT__ before initialization
Uncaught ReferenceError: Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initializationShort answer
The error means a module read a let, const or export before the line that creates it had run. In a bundled app it nearly always comes from two files that import each other, so one runs first while the other is half done. Find the cycle, then defer the read or move the shared code out.
Fix it fast
1Find out which cause you have
Open the first two files in the stack trace and look at their import lines. If file A imports file B and B imports A, directly or through a third file, you have found the cycle.
grep -n '^import' src/a.js src/b.js2Match what you see, then jump to its fix
- 1
The stack trace shows the same files twice, and the failing line reads an imported value at the top level of a module.Two modules import each otherFix - 2
The error names a local variable, and the declaration sits below the failing line in the same file.A let or const is used above the line that declares itFix
- 1
What does the error look like?
Uncaught ReferenceError: Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initializationReferenceError: Cannot access 'a' before initialization
Cause 1 of 2
Two modules import each other
1Check
The module that is loaded first cannot finish while it waits for the second, and the second reads a value the first has not created yet. Compare the import lines of the files in the stack trace.
grep -n '^import' src/a.js src/b.js2Fix
Do not read the imported value while the module loads. Read it later, inside a function or an event handler, when both modules have finished. Moving the shared code into a third file that neither of the two needs also breaks the cycle.
// b.js
import { a } from "./a.js";
export function showA() {
console.log(a); // read when called, not at load time
}3Verify
Rebuild and reload the app. The error should not appear.
grep -n '^import' src/a.js src/b.jsConfirmed by developer.mozilla.org
Cause 2 of 2
A let or const is used above the line that declares it
1Check
A let or const exists but is unusable from the start of its scope until its declaration runs. Execution order matters, not where the text sits in the file.
grep -n "const config" src/store.js2Fix
Move the declaration above its first use.
const foo = 33;
console.log(foo);3Verify
Reload the page and confirm that the error is gone.
npm run buildConfirmed by developer.mozilla.org
How do I stop it coming back?
- Draw the import direction once: pages import components, components import helpers, helpers import nothing above them.
- Keep stores, constants and helpers in files that do not import UI components.
- Avoid reading imported values at the top level of a module.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix