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 x before initialization

See original GitHub issue

I tried implementing this and I got an error like in the subject. The reason is that the exports are accessing constants defined below them.

This is what the result looks like:

        "use strict";
        __webpack_require__.r(__webpack_exports__);
        /* WEBPACK VAR INJECTION */
        (function(__react_refresh_utils__, __react_refresh_error_overlay__) {
            /* harmony export (binding) */
            __webpack_require__.d(__webpack_exports__, "BoxWithLine", function() {
                return BoxWithLine;
            });

      
[...]

            __webpack_require__.$Refresh$.runtime = __webpack_require__(/*! react-refresh/runtime */
            "./node_modules/.pnpm/react-refresh@0.8.3/node_modules/react-refresh/runtime.js");
            __webpack_require__.$Refresh$.setup(module.i);

[...]

            const BoxWithLine = /*#__PURE__*/
            Object(styled_components__WEBPACK_IMPORTED_MODULE_13__["default"])(app_components_Layout__WEBPACK_IMPORTED_MODULE_2__["Box"]).withConfig({
                displayName: "Page__BoxWithLine",
                componentId: "sc-174cupy-0"
            })(["\n\tborder-color: transparent;\n\tborder-bottom: 1px solid ", ";\n"], app_styles__WEBPACK_IMPORTED_MODULE_6__["grey10"]);

I put the plugins at the bottom of the plugins array for both webpack and babel.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:19 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
pmmmwhcommented, Dec 31, 2020

Hi, I did a bit of digging and I think this is definitely either caused by:

  1. Unnamed default exports with namespaces
  2. Circular dependencies

Webpack tries to concatenate modules and hoist them in the correct order, but in rare cases this will fail and yield this error.

3reactions
kimbaudicommented, Mar 23, 2021

@wmertens - I was also facing a similar situation where I had a single const that broke on production build due to a circular dependency. Changing the const to a var also worked for me, but I don’t think that is the correct way to handle that situation. Instead, I moved the const into a new module and I can still use const and the ReferenceError went away. I was able to solve the issue by looking at https://github.com/webpack/webpack/issues/9173#issuecomment-494903242

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReferenceError: Cannot access before initialization in JS
The "ReferenceError: Cannot access before initialization" error occurs when a variable declared using let or const is accessed before it was initialized in ......
Read more >
[SOLVED] Cannot Access Before Initialization Error in ...
The “cannot access before initialization” reference error occurs in JavaScript when you try to access a variable before it is declared with ...
Read more >
Cannot access 'variable_name' before initialization
When you assign variables using $: you cannot assign them as part of other variables declared using let , const , or var...
Read more >
can't access lexical declaration`X' before initialization
The JavaScript exception "can't access lexical declaration `variable' before initialization" occurs when a lexical variable was accessed before it was ...
Read more >
uncaught referenceerror: cannot access 'x' before ...
The JavaScript exception "can't access lexical declaration `variable' before initialization" occurs when a lexical variable was accessed before it was ...
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