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.

ESM @nivo/bar 'global is not defined' - Vite

See original GitHub issue

Describe/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:

  1. Use esm
  2. import { ResponsiveBar } from ‘@nivo/bar
  3. 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:closed
  • Created 2 years ago
  • Reactions:5
  • Comments:10

github_iconTop GitHub Comments

26reactions
mrmartineaucommented, Apr 8, 2021

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 = globalThis</script>
<script type="module" src="/src/app/main.tsx"></script>

<script>var global = window</script> might also work, but probably not in this case since this Nivo package uses global.window in its source code.

3reactions
stolinskicommented, Apr 10, 2021

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

diff --git a/node_modules/@nivo/bar/dist/nivo-bar.es.js b/node_modules/@nivo/bar/dist/nivo-bar.es.js
index dbaaaf3..4b2a83c 100644
--- a/node_modules/@nivo/bar/dist/nivo-bar.es.js
+++ b/node_modules/@nivo/bar/dist/nivo-bar.es.js
@@ -1,3 +1,4 @@
+let global = globalThis
 import React, { Fragment, Component } from 'react';
 import { TransitionMotion, spring } from 'react-motion';
 import { defsPropTypes, noop, withTheme, withDimensions, withMotion, getAccessorFor, getLabelGenerator, bindDefs, LegacyContainer, CartesianMarkers, SvgWrapper, getRelativeCursor, isCursorInRect, ResponsiveWrapper } from '@nivo/core';

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

Read more comments on GitHub >

github_iconTop 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 >

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