This rule raises an issue when code uses window, self, or global to access the global object, except for environment-specific APIs and events.

Why is this an issue?

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:

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.

What is the potential impact?

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.

How to fix?

Replace window with globalThis when accessing global properties or the global object itself.

Non-compliant code example

const config = window.APP_CONFIG; // Noncompliant
window.myGlobalVar = 'value'; // Noncompliant

Compliant code example

const config = globalThis.APP_CONFIG;
globalThis.myGlobalVar = 'value';

Documentation

Standards