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 'varName' before initialization

See original GitHub issue

Describe the bug

Upgraded a react-app running React v17 to use react-scripts 4.0.0. First load in development returns an error that did not exist using 3.4.4

Did you try recovering your dependencies?

Yes

6.14.8

Which terms did you search for in User Guide?

Reference error

Environment

Environment Info:

current version of create-react-app: 4.0.0 running from C:\Users\MattCorner\AppData\Roaming\npm-cache_npx\11504\node_modules\create-react-app

System: OS: Windows 10 10.0.19041 CPU: (12) x64 Intel® Core™ i7-9750H CPU @ 2.60GHz Binaries: Node: 14.8.0 - C:\Program Files\nodejs\node.EXE Yarn: Not Found npm: 6.14.8 - C:\Program Files\nodejs\npm.CMD Browsers: Chrome: Not Found Edge: Spartan (44.19041.423.0), Chromium (86.0.622.51) Internet Explorer: 11.0.19041.1 npmPackages: react: Not Found react-dom: Not Found react-scripts: Not Found npmGlobalPackages: create-react-app: Not Found

Steps to reproduce

This applications works fine in react-scripts 3.x.x. Based on the error message I suspect something to do with hot refresh.

The error is complaining about a custom hook called useSnackbar. This useSnackbar.js exports a SnackbarProvider and the useSnackbar hook. The useSnackbar hook is reexported through a hooks/index.js as so:

export {useSnackbar} from "./useSnackbar";

This hook is then use throughout the app.

  1. Start app in development
  2. Microsoft edge loads and reports following error
  3. image

Expected behavior

Applications runs correctly in development using hot refresh.

Actual behavior

App errors on load.

Reproducible demo

Unable to reproduce outside of application.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:17 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
SunburnedGoosecommented, Feb 2, 2021

This happened to me today. The situation was I bundled several files in a directory into an index.ts file. Elsewhere in my app, I was directly referencing a component with its full path instead of the bundled export. This was throwing the error.

./Viewers/index.js

export * from './ComponentViewer';
export * from './FullscreenViewer';
export * from './MobileViewer';
export * from './PanelViewer';
export * from './ViewerRoutes';

(Good) App.tsx

import React from 'react';
import { NavLink } from 'react-router-dom';

import './App.css';
import { ViewerRoutes } from './Viewers';

(Bad) App.tsx

import React from 'react';
import { NavLink } from 'react-router-dom';

import './App.css';
import { ViewerRoutes } from './Viewers/ViewerRoutes';
3reactions
mattcornercommented, Oct 29, 2020

So we’ve got the exact same Context/Provider/hook in two apps. One works with react-scripts 4, one doesn’t.

The only difference between the two that we can tell is one imports the hook through an index.js, and one imports directly from the hook file.

Removing the following line from the index.js stops the issue.

export {useSnackbar} from "./useSnackbar"

I can’t tell why this would cause the issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReferenceError: can't access lexical declaration 'X' before ...
The JavaScript exception "can't access lexical declaration `variable' before initialization" occurs when a lexical variable was accessed ...
Read more >
Cannot access {variable name} before initialization - Stack ...
ReferenceError : Cannot access {variable name} before initialization. I'm aware of "Temporal Deadzones" however this app has had no problem ...
Read more >
[SOLVED] Cannot Access Before Initialization Error in JavaScript
The “cannot access before initialization” reference error occurs in JavaScript when you try to access a variable before it is declared with ...
Read more >
Javascript - referenceerror: cannot access <variable name ...
but not getting why it is giving error. It's giving - ReferenceError: Cannot access 'r1' before initialization. Please help me. What I have ......
Read more >
Fix "Cannot access before initialization" Reference Error in ...
The “cannot access before initialization” reference error occurs in JavaScript when you try to access a variable before it is declared with let...
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