ESM @nivo/bar 'global is not defined' - Vite
See original GitHub issueDescribe/explain the bug
In an esm based Vite app importing via
import { ResponsiveBar } from '@nivo/bar'
produces error Uncaught ReferenceError: global is not defined
I’m guessing it’s related to line 713 of nivo-bar-.es.js
pixelRatio: global.window && global.window.devicePixelRatio ? global.window.devicePixelRatio : 1
or a dependency of Nivo.
To Reproduce
Steps to reproduce the behavior:
- Use esm
- import { ResponsiveBar } from ‘@nivo/bar’
- Use ResponsiveBar.
Expected behavior Component to render, like normal
Desktop (please complete the following information):
- OS: Mac OS
- Browser Any
- Version
"@nivo/bar": "^0.67.0", "@nivo/core": "^0.67.0",
Additional context globalThis might be a better option? Not sure where this is being pulled from because the src files don’t include global, where the dist/ files do.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:5
- Comments:10
Top Results From Across the Web
How to fix the ReferenceError: global is not defined error in ...
That sound simple enough to fix, just create a vite.config.[js/ts] file, add it to your SvelteKit project and define global right?
Read more >Vite 'global is not defined' - typescript - Stack Overflow
The problem is because vite doesn't define a global field in window as webpack does. And some libraries relies on it since webpack...
Read more >Untitled
Population distribution by age in indonesia, Define ratchet slang, ... Dilma cantando beijinho no ombro agora e tarde, J stylez basketball?
Read more >Untitled
Junior in college senior in high school, Geladeira esmaltec roc29, Krgv channel 5 ... Clem ohameze actor, Jiqiang song, A-ha forever not yours...
Read more >the and a in i it with that at this on from he my or we but - OSF
... clever concrete estimate disability numb global reputation binge dynamic ... greenland bureaucrat inject snarl undefined cannibalism warlord machines ...
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
FYI I found a slightly better way (IMO) to achieve the same thing from here. Now there’s no need for
patch-package
Add this to your
index.html
file before your Vite<script>
tag, e.g.<script>var global = window</script>
might also work, but probably not in this case since this Nivo package usesglobal.window
in its source code.If anyone would like a temporary work around. I’ve managed to get Nivo loading in esm with
https://github.com/ds300/patch-package
Using the following patch
Def not a long term solution. But if you are using Nivo and ESM, this will at least get you running until the es.js build can be fixed.
Edit
Use https://github.com/plouc/nivo/issues/1455#issuecomment-815904731 instead