ReferenceError: Cannot access x before initialization
See original GitHub issueI 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:
- Created 3 years ago
- Reactions:3
- Comments:19 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi, I did a bit of digging and I think this is definitely either caused by:
Webpack tries to concatenate modules and hoist them in the correct order, but in rare cases this will fail and yield this error.
@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 theconst
to avar
also worked for me, but I don’t think that is the correct way to handle that situation. Instead, I moved theconst
into a new module and I can still useconst
and the ReferenceError went away. I was able to solve the issue by looking at https://github.com/webpack/webpack/issues/9173#issuecomment-494903242