Third-Party Library Uses `window` not `globalThis` Causing Issues.
I'm using a third-party library that just released a new version and when I try to use it I get an error:
error: Uncaught (in promise) ReferenceError: window is not defined
With further information
This makes sense but is frustrating as I don't control that code and it makes sense they wouldn't reference globalThis
as that isn't the standard. Is there a slick option of how to fix this on the fly or do I need to take their code and replace all window references with globalThis
4 Replies
you could try doing this before importing the third party library:
wait I thought globalThis was standard
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis
MDN Web Docs
globalThis - JavaScript | MDN
The globalThis global property contains the global this value, which is usually akin to the global object.
@kylec32 To have the code
globalThis.widow = globalThis
execute before the import, you actually have to move it into a seperate file that you import before the library import
So for example:
and then in add_window.ts
you do globalThis.window = globalThis;
.
These gymnastics are needed because in JavaScript all imports are always executed before any code in the module that contains the imports.