I am upgrading an existing Angular application to Angular 15. I am receiving the following error in the browser:
index.js:43 Uncaught ReferenceError: global is not defined
at 34768 (index.js:43:30)
at __webpack_require__ (bootstrap:19:1)
at 92634 (AuthenticationDetails.js:65:4)
at __webpack_require__ (bootstrap:19:1)
at 53843 (UserAgent.js:19:25)
at __webpack_require__ (bootstrap:19:1)
at 48562 (vendor.js:26:84)
at __webpack_require__ (bootstrap:19:1)
at 88951 (auth.module.ts:8:25)
at __webpack_require__ (bootstrap:19:1)
This used to be solved by adding the below to polyfills.ts
(window as any).global = window;
I believe this file has been removed from Angular 15. How do I solve this error now?
I tried creating a polyfills.ts without any success
According to this PR, you should put this in a file and point to it in your angular.json under a polyfills array. Example:
// other stuff in the angular.json
polyfills: [ "./window-global-fix.ts" ],
I was configuring Angular 15 with AWS Amplify following available online tutorials when I came across this issue. The provided answer above ultimately revealed what I needed to do so I upvoted the above answer. I'm putting this here for quick-reference for anyone who comes across similar Angular15+Amplify issue.
1.) Create some kind of file to act in place of "pollyfill.ts"
Angular-Project / src / main / webapp / src / aws-pollyfill-like-file.ts
(window as any).global = window;
(window as any).process = {
env: { DEBUG: undefined },
};
Angular-Project / ... / webapp / tsconfig.app.json
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
...
},
"files": [
"src/aws-pollyfill-like-file.ts",
"src/main.ts"
],
...
}
Angular-Project / ... / webapp / angular.json
{
"$schema": ...,
...
"projects": {
"angularclient": {
...
"architect": {
"build": {
...
"options": {
...
"polyfills": [
"zone.js",
"src/aws-pollyfill-like-file.ts"
],
...
},
...
},
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With