Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

ReferenceError: Cannot access __WEBPACK_DEFAULT_EXPORT__ before initialization

ErrorUncaught ReferenceError: Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization

Short 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.

  • Applies to Node.js 22, webpack 5, ES modules
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.js
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The 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

What does the error look like?

  • Uncaught ReferenceError: Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization
  • ReferenceError: 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.js

2Fix

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.js

Confirmed by developer.mozilla.org

Did this fix it?

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.js

2Fix

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 build

Confirmed by developer.mozilla.org

Did this fix it?

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.