This rule raises an issue when code uses window, self, or global to access the global object, except for
environment-specific APIs and events.
globalThis is the standardized way to access the global object across all JavaScript environments. Before globalThis,
developers had to use different global references depending on the environment:
window in browsers global in Node.js self in Web Workers This created compatibility issues when code needed to run in multiple environments. Using environment-specific globals makes code less portable and harder to maintain.
globalThis was introduced in ES2020 as a unified solution that works consistently across all JavaScript environments. It provides the
same global object reference regardless of whether your code runs in a browser, Node.js, or Web Worker.
Using globalThis makes your code more future-proof and eliminates the need for environment detection when accessing global
properties.
Using environment-specific global references reduces code portability and can cause runtime errors when code is moved between different JavaScript environments. This makes applications harder to maintain and test across different platforms.
Replace window with globalThis when accessing global properties or the global object itself.
const config = window.APP_CONFIG; // Noncompliant window.myGlobalVar = 'value'; // Noncompliant
const config = globalThis.APP_CONFIG; globalThis.myGlobalVar = 'value';