use-persisted-state icon indicating copy to clipboard operation
use-persisted-state copied to clipboard

Uncaught reference error: global is not defined

Open igbominadeveloper opened this issue 3 years ago • 3 comments

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

https://github.com/donavon/use-persisted-state/blob/2a33963050b209f8cc27060a32109978414a4dc7/src/index.js#L7

igbominadeveloper avatar Dec 14 '21 01:12 igbominadeveloper

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: https://github.com/donavon/use-event-listener/pull/55

valtism avatar Dec 28 '21 07:12 valtism

Same problem here. I'm also using Vite.

miloxeon avatar Mar 31 '22 14:03 miloxeon

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>

miloxeon avatar Apr 06 '22 09:04 miloxeon