Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught reference error: global is not defined #62

Open
igbominadeveloper opened this issue Dec 14, 2021 · 3 comments
Open

Uncaught reference error: global is not defined #62

igbominadeveloper opened this issue Dec 14, 2021 · 3 comments

Comments

@igbominadeveloper
Copy link

igbominadeveloper commented Dec 14, 2021

I just migrated my React app from CRA to Vite and this error
Screenshot 2021-12-14 at 02 47 20

if (typeof global !== 'undefined' && global.localStorage) {

@valtism
Copy link

valtism commented Dec 28, 2021

I'm getting this error too, which is strange because the typeof check should be working to guard against this. 🤔

Edit: my case was actually not from this library, but a library that is used by this. I have opened a PR there: donavon/use-event-listener#55

@miloxeon
Copy link

Same problem here. I'm also using Vite.

@miloxeon
Copy link

miloxeon commented Apr 6, 2022

I know it's a hack, but the following solves this error.

I defined global myself just for it to not be undefined:

<script>
	global = {}
</script>

In index.html:

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>Foo</title>
	</head>

	<body>
		<div id="root"></div>
		<!-- workaround for https://github.com/donavon/use-persisted-state/issues/62 -->
		<script>
			global = {}
		</script>
		<script type="module" src="/src/index.tsx"></script>
	</body>
</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants