Skip to content
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

Closed
stolinski opened this issue Mar 23, 2021 · 10 comments
Closed

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

stolinski opened this issue Mar 23, 2021 · 10 comments

Comments

@stolinski
Copy link

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.

@stolinski
Copy link
Author

stolinski commented Mar 23, 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 #1455 (comment) instead

@mrmartineau
Copy link

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 global and the others do not?

@mrmartineau
Copy link

mrmartineau commented Apr 1, 2021

FYI it is worth noting that @stolinski's patch above works, but when I used it the patch kept on being applied every time yarn or yarn install etc was being used, resulting in something like this:

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 patch-package needs an idempotent patch (see this comment for more info).

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';

@mrmartineau
Copy link

mrmartineau commented 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.

@stolinski
Copy link
Author

@mrmartineau yeah this is a much better solution than PatchPackage.

@wyze
Copy link
Contributor

wyze commented Jun 4, 2021

This is fixed in the latest version v0.70.1.

@wyze wyze closed this as completed Jun 4, 2021
@armgit5
Copy link

armgit5 commented Feb 16, 2022

I'm working with WalletConnect running on react with vite. After fixing global is not defined I got Buffer is not defined issue after that. Found @esbuild-plugins/node-globals-polyfill over stackoverflow that helps me fix both of the issues

@mzaatar
Copy link

mzaatar commented Mar 8, 2022

@armgit5 could you provide us with an example? thanks

@armgit5
Copy link

armgit5 commented Mar 10, 2022

@mzaatar here is an example of global and Buffer is not defined error. For buffer you have to add in 2 places 1. inside of NodeGlobalsPolyfillPlugin for running dev and 2. add nodePolyfills inside of rollupOptions for running production build. Additionally if you get require is not defined then you can look into transformMixedEsModules as well... hope that helps 😃

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,
    },
 }
})

@RemixJazzCode
Copy link

I solved problem. Nice work !

filipecabaco added a commit to Logflare/logflare that referenced this issue Feb 24, 2023
Error due to the way ESM works with Nivo:
plouc/nivo#1455

Bumping to minimum version where the issue is fixed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants