Skip to content

Uncaught ReferenceError: global is not defined #10035

Closed

Description

@Praveer1981

Bug report

I have an Electron based application. we do webpack in the render process. It throws error when I run the app
Uncaught ReferenceError: global is not defined

If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?
It should not throw this error when I run the application

Other relevant information:
here is the Error:

index.js?e792:25 Uncaught ReferenceError: global is not defined
    at eval (index.js?e792:25)
    at Object../node_modules/@babel/polyfill/lib/index.js (feb33beb921cb1f5a1b3.worker.js:97)
    at __webpack_require__ (feb33beb921cb1f5a1b3.worker.js:20)
    at eval (eval at ./node_modules/babel-loader/lib/index.js?{"babelrc":false,"presets":["@babel/preset-env", "@babel/preset-react"]}!./node_modules/ts-loader/index.js?{"transpileOnly":true,"configFile":"tsconfig.webpack.json"}!./node_modules/ts-loader/index.js?!./packages/myApp/modules/frameworks/file-provider/box-provider/webworker/box.worker.ts (feb33beb921cb1f5a1b3.worker.js:526), <anonymous>:2:73)
    at Module../node_modules/babel-loader/lib/index.js?{"babelrc":false,"presets":["@babel/preset-env", "@babel/preset-react"]}!./node_modules/ts-loader/index.js?{"transpileOnly":true,"configFile":"tsconfig.webpack.json"}!./node_modules/ts-loader/index.js?!./packages/myApp/modules/frameworks/file-provider/box-provider/webworker/box.worker.ts (feb33beb921cb1f5a1b3.worker.js:526)
    at __webpack_require__ (feb33beb921cb1f5a1b3.worker.js:20)
    at feb33beb921cb1f5a1b3.worker.js:84
    at feb33beb921cb1f5a1b3.worker.js:87

Note: Since it is an electron app:
config.target = "electron-renderer";
{global:true};

webpack version: ^4.41.2
Node.js version: 10.16.3
Operating System: windows 10
"@babel/core": "^7.6.4"
Additional tools: chrome 78
Electron version : 7.1.1

Activity

  1. sokra commented on Nov 26, 2019

    @sokra
    Member

    Thanks for your report. It would be great if you reduce your issue to a small reproducible example. Best put this example into a github repository together with instructions how to get to the problem.

  2. Praveer1981 commented on Nov 26, 2019

    @Praveer1981
    Author

    Its little bit hard to reproduce it in the separate application. I want to know under which scenario it is possible.
    once I get the error in the console and write global It shows me the property of the global object which means the global object is available.
    then what could be the reason of getting this error ?

  3. alexander-akait commented on Nov 26, 2019

    @alexander-akait
    Member

    @Praveer1981 Looks like you run babel-loader in worker, why?

  4. Praveer1981 commented on Nov 26, 2019

    @Praveer1981
    Author

    we have babel-loader in the webpack config file

  5. Praveer1981 commented on Nov 26, 2019

    @Praveer1981
    Author

    `module.exports = {
    ....
    module: {
    rules: [
    {
    test: /.ts(x?)$/,
    include: [packagesDir],
    use: [
    {
    loader: require.resolve("babel-loader"),
    options: {
    babelrc: false,
    presets: [
    [
    "@babel/preset-env",
    {
    modules: false,
    "useBuiltIns" : "entry"
    }
    ],
    "@babel/preset-react"
    ],
    plugins: [require.resolve("babel-plugin-syntax-dynamic-import")]
    }
    },
    {
    loader: require.resolve("ts-loader"),
    options: {
    configFile: "tsconfig.webpack.json"
    }
    }
    ]
    }

    ]
    

    }
    .....

    }`

  6. Praveer1981 commented on Nov 26, 2019

    @Praveer1981
    Author

    Additional Information:
    if (global._babelPolyfill && typeof console !== "undefined" && console.warn) {
    console.warn("@babel/polyfill is loaded more than once on this page. This is probably not desirable/intended " + "and may have consequences if different versions of the polyfills are applied sequentially. " + "If you do need to load the polyfill more than once, use @babel/polyfill/noConflict " + "instead to bypass the warning.");
    }

  7. ohanar commented on Feb 18, 2020

    @ohanar
  8. added
    help wanted
    on Feb 19, 2020
  9. amanharwara commented on Mar 13, 2020

    @amanharwara

    I was having the same issue, and I found a hacky fix.
    If you can, change any reference of global which is causing the issue to window.globalThis. At least this worked for me.

  10. EdwinIwan commented on Mar 24, 2020

    @EdwinIwan

    We found easy solution for this bug. We had to only add this code to index.html before your bundle.

    <body id="app">
        <script>
          global = globalThis //<- this should be enough
        </script>
        <script src="./electron-renderer.js"></script>
      </body>
    
  11. added a commit that references this issue on Apr 24, 2020
    67e19e0
  12. webpack-bot commented on Sep 22, 2020

    @webpack-bot
    Contributor

    This issue had no activity for at least three months.

    It's subject to automatic issue closing if there is no activity in the next 15 days.

  13. added
    status: stale
    on Sep 22, 2020
  14. alexander-akait commented on Sep 22, 2020

    @alexander-akait
    Member

    Fixed in worker-loader, also webpack@5 supports workers out of box

  15. 21 remaining items

  16. YasharSL commented on Oct 15, 2022

    @YasharSL

    I'm having the same issue while trying to open a new window in my app. None of the solutions worked.

  17. marcibk commented on Oct 19, 2022

    @marcibk

    Did anyone found a fix on how to use nodeIntegration: false ?

  18. bpejac commented on Nov 13, 2022

    @bpejac

    @marcibk I set target: 'web' in the webpack config for the react app. I do not set nodeIntegration (defaults to false AFAIK).

  19. CarlSaqui29 commented on Dec 3, 2022

    @CarlSaqui29

    on your createWindow(), add the

    webPreferences: {
    nodeIntegration: true,
    contextIsolation: false
    }

    this will work absolutely...

  20. Cedgud commented on Feb 3, 2023

    @Cedgud

    Hello,

    does anone make this work? I've tried the global = globalThis fix. Now I have "require is not defined"

  21. thamizh-qwerty commented on Feb 28, 2023

    @thamizh-qwerty

    We found easy solution for this bug. We had to only add this code to index.html before your bundle.

    <body id="app">
        <script>
          global = globalThis //<- this should be enough
        </script>
        <script src="./electron-renderer.js"></script>
      </body>
    

    It does not works in angular + electronJS + contextIsolation = true

  22. resatDev commented on Sep 21, 2023

    @resatDev

    I have already resolved this problem by changing the node options. According to official doc of webpack, global prop of node is false default. Using suggested plugin or making global is true will probably resolve your problem as well.

  23. travbid commented on May 25, 2024

    @travbid

    For my project I had to set splitChunks: false in the webpack config to fix this error:

    module.exports = {
      configureWebpack:{
        // ...
        target: "electron-renderer",
        optimization: {
          splitChunks: false,
        }
      }
    }
  24. sabelyn commented on Jun 2, 2024

    @sabelyn

    I'll join in here. Tried both combinations of nodeIntegration and contextIsolation and those solutions failed. Tried defining global in my index.html file, which failed. I have tried setups and configurations from at least 5 different "How to setup an electron project with react, webpack, and typescript" articles. I also tried setting node.global to true in all of my various webpack entry configs.

    The only thing that works is setting target: "web" in the react webpack config, and this only works when building in development mode running two separate terminal processes for webpack-dev-server and the actual electron app.

    Something is buggy with the electron-renderer target. Electron apps need global, and electron-renderer isn't providing it. The reference error being thrown isn't even in my own code, so I have no control over whether it's being called or not.

  25. ChuanfengLai commented on Jun 17, 2024

    @ChuanfengLai

    When create brwoser window, use nodeIntegrationInWorker: true in webPreferences
    https://www.electronjs.org/docs/latest/tutorial/multithreading

     new BrowserWindow({
          webPreferences: {
            nodeIntegrationInWorker: true,
            contextIsolation: false,
          },
        })
    
  26. mohan-reviewer1 commented on Dec 11, 2024

    @mohan-reviewer1

    Another workaround is to simply not use target: electron-renderer for now, if you don't need it. web works.

    I have tried that, but I get the same error:

    Uncaught ReferenceError: global is not defined
        at jsonp chunk loading:41:1
        at jsonp chunk loading:509:1
        at startup:6:1
    

    ExampleWasTaken
    is there any workaround for this?

  27. kythanh commented on Jun 8, 2025

    @kythanh

    Still got the same error here.

    Uncaught ReferenceError: global is not defined
        at jsonp chunk loading:84:1
        at jsonp chunk loading:86:1
        at index.tsx:10:67
    
    Image
  28. alexander-akait commented on Jun 10, 2025

    @alexander-akait
    Member

    @kythanh Check your target? What is the value?

  29. hushuxian commented on Aug 5, 2025

    @hushuxian

    I have the same problem with my electron app,only set nodeIntegration:true and contextIsolation:false can fix it .

  30. alexander-akait commented on Aug 5, 2025

    @alexander-akait
    Member

    @hushuxian Can you create reproducible test repo, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedstatus: stale

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions