"Global is not defined" Sporadic Error in Angular 16 Application npm package

95 views Asked by At

I seem to get a sporadic global is not defined error in my angular application when the app initialises.

The vast majority of the time the error doesn't throw, however sometimes it will and of course it completely breaks the app.

The error is coming from an old npm package that we've got to keep using.

I found a suggestion that said to add a file where you set global and add this to polyfills array in angular.json. I did this, here's the contents of that file...

(window as any).global = window;

Then my polyfills array...

 "polyfills": [
              "zone.js",
              "global.polyfills.ts"
            ]

This fixed it to begin with, because before I added this the error was throwing every single time.

But now it just happens every now and then.

I assume it's something to do with the order in which files are loaded sometimes? and when it breaks it's because the angular app tried to initialise the npm package in question before that polyfills file has been loaded?

How can I fix this permanently?

0

There are 0 answers