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

global is not defined #2618

Closed
volkanunsal opened this issue Mar 20, 2021 · 12 comments
Closed

global is not defined #2618

volkanunsal opened this issue Mar 20, 2021 · 12 comments

Comments

@volkanunsal
Copy link

⚠️ IMPORTANT ⚠️ Please do not ignore this template. If you do, your issue will be closed immediately.

Describe the bug

I'm getting global is not defined error.

Screen Shot 2021-03-20 at 5 38 00 PM

Reproduction

The header of the file has this comment block.

/*!
 * The buffer module from node.js, for the browser.
 *
 * @author   Feross Aboukhadijeh <http://feross.org>
 * @license  MIT
 */
/* eslint-disable no-proto */

It seems to be coming from a module that includes Node.js modules, and the browser equivalent has been included, and there is a global keyword in there, which vitejs cannot seem to handle.

System Info

[email protected]

@volkanunsal volkanunsal changed the title global is not defined global is not defined Mar 20, 2021
@github-actions
Copy link

Hello @volkanunsal. Please provide a online reproduction by codesandbox or a minimal GitHub repository. Issues labeled by need reproduction will be closed if no activities in 3 days.

@felixakiragreen
Copy link

I'm also encountering this issue. It happens when trying to use the web3 package.

first process is not defined, then global is not defined, then Buffer is not defined

I recognize these as globals/builtins, but:


export default defineConfig({
  plugins: [
    reactRefresh(),
    { ...builtins({ crypto: true }), name: 'rollup-plugin-node-builtins' },
    { ...globals(), name: 'rollup-plugin-node-globals' },
  ],

  define: {
    'process.env': {},
    global: {},
  },
})

does not seem to actually do anything.

@Cyber3x
Copy link

Cyber3x commented Mar 22, 2021

I'm getting the same error global is not defined when using react-i18next in my project. Here's a minimal repo that should reproduce the problem. https://github.com/Cyber3x/vite-i18n. The error is only present after building the project.

@patak-dev
Copy link
Member

patak-dev commented Mar 22, 2021

Vite doesn't include shims for Node variables like Webpack 4 does (in Version 5, shims also needs to be added by the user). Check other issues like #728, you can use rollup-plugin-node-globals like a previous comment (but it hasn't been updated for a while). Maybe this is also an option https://github.com/ionic-team/rollup-plugin-node-polyfills.

Another option is to use modern libraries that do not depend on node globals (or ask for updates in your current dependencies).

Let's close this issue because it is better to try to fix this by discussing it with the maintainers of each library. You can also start a Discussion about a particular library or join Vite Land to chat with others about possible solutions.

@wheatjs
Copy link

wheatjs commented Mar 22, 2021

As a work around for anyone who stumbles upon this issue, if the library you are attempting to use is also provided via a CDN, you can use that CDN instead.

Since the CDN version is built for the browser it shouldn't have any node-only libraries so you will be able to use it in Vite. To do so first you will want to use the Vite CDN Plugin from Evan(currently unpublished) . After installing that plugin you can then use it to import your library via the CDN. The plugin will automatically download it and use it in place of the one installed via node_modules.

Here are two example repositories of this in action
https://github.com/jacobclevenger/vite-mediasoup-client-test
https://github.com/jacobclevenger/octokit-vite-example

@volkanunsal
Copy link
Author

volkanunsal commented Mar 22, 2021

I'm confused. Why is this issue closed? @felixakiragreen already mentioned that using the plugins mentioned above did not to solve the problem. I tried this method as well, and also tried using define to add globals to the environment, and it didn't work. Using modern libraries is not an option.

@wheatjs
Copy link

wheatjs commented Mar 23, 2021

@volkanunsal Providing node shims is not a planned addition to Vite and since Webpack 5 is also removing I don't imagine it will ever be. If the libraries you are using are provided via a CDN you can use the workaround I posted above.

@volkanunsal
Copy link
Author

volkanunsal commented Mar 23, 2021 via email

@patak-dev
Copy link
Member

@volkanunsal if you would like to discuss this further, would you open a Discussion or join Vite Land to chat with others about possible solutions. Thanks!

@volkanunsal
Copy link
Author

volkanunsal commented Mar 23, 2021 via email

@wheatjs
Copy link

wheatjs commented Mar 23, 2021

@volkanunsal well we can't really help you if you don't want to have any discussion on it 😅

@vitejs vitejs locked as too heated and limited conversation to collaborators Mar 23, 2021
@patak-dev
Copy link
Member

For reference, if you only need to shim global, you can add

<script>window.global = window;</script>

to your index.html

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants