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 undefined in production environment with context isolation enabled #747

Closed
thamizh-qwerty opened this issue Feb 28, 2023 · 3 comments

Comments

@thamizh-qwerty
Copy link

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

@metalllus
Copy link

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

@thamizh-qwerty
Copy link
Author

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

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

@maximegris
Copy link
Owner

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

  • pull main branch source code ([email protected])
  • 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 ?

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

3 participants