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

Problem with global in prod build vite + React #139

Closed
michalbujalski opened this issue Oct 3, 2022 · 5 comments
Closed

Problem with global in prod build vite + React #139

michalbujalski opened this issue Oct 3, 2022 · 5 comments

Comments

@michalbujalski
Copy link

michalbujalski commented Oct 3, 2022

I have vite+React setup. Everything works fine in development enviroment but when I run yarn build I get the following error:
image

I've added a workaround to vite.config.ts:

export default defineConfig({
  plugins: [react()],
  define: {
    global: {}
  },
...
@michalbujalski michalbujalski changed the title Global not defined with vite + React Problem with global in prod build vite + React Oct 3, 2022
@johnrdoty92
Copy link

@michalbujalski Have you tried adding the rollup-plugin-node-polyfills plugin to the rollupOptions?

import nodePolyfills from "rollup-plugin-node-polyfills";

export default defineConfig({
  // add the following to the rest of your config:
  build: {
    rollupOptions: {
      plugins: [ nodePolyfills() as any],
    }
  }
})

@michalbujalski
Copy link
Author

Yes, I got the same result

@johnrdoty92
Copy link

@michalbujalski Are you getting that error after running yarn preview? Can you show what your config looks like for the optimizeDeps option? You could also try changing your definition of global to globalThis:

define: {
  global: "globalThis",
},

Also, if you see any errors in the dev tools console about require, you might need to add the following to your build option:

commonjsOptions: {
  transformMixedEsModules: true,
},

@bdxndev
Copy link

bdxndev commented Feb 22, 2023

any update on a fix

@damiano-melcarne-lab49
Copy link

I ran into a similar set of issues with a Vite+React application. A local build worked as expected but the same build deployed to Netlify would throw the following errors in the Chrome DevTools console:

Uncaught ReferenceError: global is not defined
Uncaught ReferenceError: require is not defined
Uncaught ReferenceError: Buffer is not defined

Setting the transformMixedEsModules field to true and adding global node polyfills fixed these issues:

/* vite.config.ts */
import { nodePolyfills } from "vite-plugin-node-polyfills";
/* ... other  config ... */

export default defineConfig({
/* ... other  config ... */
build: {
    commonjsOptions: {
      transformMixedEsModules: true,
    },
  },
plugins: [
    nodePolyfills({
      globals: {
        Buffer: true,
        global: true,
        process: true,
      },
    }),
  ]
})

transformMixedEsModules: true enables mixed module transformations. This is useful for scenarios where modules include a mix of ES import statements and CommonJS require statements. Setting transformMixedEsModules to true transforms require statements to import statements (and vice versa for false). This is useful for bundling hashconnect since the ES esm/hashconnect.js file uses a CommonJS 'require' statement to import buffer: global.Buffer = global.Buffer || require('buffer').Buffer;.

The node polyfills should fix the errors related to the 'not defined' global variables.

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

5 participants