-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
ESM @nivo/bar 'global is not defined' - Vite #1455
Comments
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. EditUse #1455 (comment) instead |
Thanks @stolinski for the fix here. I haven't needed to use patch-package in many years... Do we know why this package has issues with |
FYI it is worth noting that @stolinski's patch above works, but when I used it the patch kept on being applied every time let global = globalThis
let global = globalThis
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'; This is because The patch below works as expected: 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 @@
import React, { Fragment, Component } from 'react';
+let global = globalThis
import { TransitionMotion, spring } from 'react-motion';
import { defsPropTypes, noop, withTheme, withDimensions, withMotion, getAccessorFor, getLabelGenerator, bindDefs, LegacyContainer, CartesianMarkers, SvgWrapper, getRelativeCursor, isCursorInRect, ResponsiveWrapper } from '@nivo/core'; |
FYI I found a slightly better way (IMO) to achieve the same thing from here. Now there's no need for Add this to your <script>var global = globalThis</script>
<script type="module" src="/src/app/main.tsx"></script>
|
@mrmartineau yeah this is a much better solution than PatchPackage. |
This is fixed in the latest version v0.70.1. |
I'm working with WalletConnect running on react with vite. After fixing |
@armgit5 could you provide us with an example? thanks |
@mzaatar here is an example of import NodeGlobalsPolyfillPlugin from "@esbuild-plugins/node-globals-polyfill";
import nodePolyfills from 'rollup-plugin-node-polyfills';
export default defineConfig({
optimizeDeps: {
esbuildOptions: {
// Fix global is not defined error
define: {
global: "globalThis",
},
plugins: [
// Without this, npm run dev will output Buffer or process is not defined error
NodeGlobalsPolyfillPlugin({
buffer: true,
}),
],
},
},
build: {
rollupOptions: {
plugins: [
/**
* Fix Buffer is not defined error for running production build
* https://www.npmjs.com/package/rollup-plugin-node-polyfills
*/
nodePolyfills()
],
},
/**
* Fix require is not defined error
* https://github.com/wobsoriano/v-dashboard/issues/20
* https://github.com/originjs/vite-plugins/issues/9
*/
commonjsOptions: {
transformMixedEsModules: true,
},
}
}) |
I solved problem. Nice work ! |
Error due to the way ESM works with Nivo: plouc/nivo#1455 Bumping to minimum version where the issue is fixed
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:
Expected behavior
Component to render, like normal
Desktop (please complete the following information):
"@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.
The text was updated successfully, but these errors were encountered: