maximegris/angular-electron

global is undefined in production environment with context isolation enabled

thamizh-qwerty opened this issue · 3 comments

Describe the bug
I am using Angular and Electron with context isolation enabled in my application. In development mode, everything works fine, but in production mode, I am getting the global is undefined error. I have tried the following solutions without success:

  • Setting nodeIntegration to false
  • Setting contextIsolation to true
  • Setting enableRemoteModule to false
  • Referencing global as window.global
  • I am using the latest versions of Angular and Electron. It is not feasible for me to switch to a different framework or downgrade Electron at this time.

To Reproduce

  1. Steps to reproduce the behavior:
  2. Enable context isolation in Electron.
  3. Build and run the application in production mode.
  4. Observe the global is undefined error.

Expected behavior
The application should run without errors in production mode with context isolation enabled.

Screenshots
19 - Copy (2)

Additional context

  • OS: Windows 10
  • Angular: 12.1.2
  • Electron: 23.1.1

Tried below solutions, none worked. Do you know the fix @maximegris ?
webpack/webpack#10035
https://stackoverflow.com/questions/65586906/react-and-electron-with-draft-js-global-is-not-defined/67336441#67336441
facebook/fbjs#290
electron/forge#1498
https://stackoverflow.com/questions/74718063/how-to-enable-context-isolation-in-electron-js-next-js-nextron

@thamizh-qwerty just add (window as any).global = window; to your polyfills.ts

@thamizh-qwerty just add (window as any).global = window; to your polyfills.ts

Used the code in polyfills.ts, error still exists.

@thamizh-qwerty
I have no error when I do :

  • pull main branch source code (angular-electron@12.0.0)
  • change contextIsolation to true in app/main.ts
  • launch the app with npm start
  • build and launch the app with npm run electron:build

Changing contextIsolation breaks how electron is injected in the renderer global variable window so isElectron() function in electron.service.ts always return false in the default project template.

So context isolation may not be the issue here.
Maybe the combinaison of other parameters you changed ? or a 3rd party library you are using on your project ? or the way you have exposed electron API from context bridge ?