use-persisted-state
use-persisted-state copied to clipboard
Uncaught reference error: global is not defined
I just migrated my React app from CRA to Vite and this error
https://github.com/donavon/use-persisted-state/blob/2a33963050b209f8cc27060a32109978414a4dc7/src/index.js#L7
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
Same problem here. I'm also using Vite.
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>