Skip to content

unicorn/prefer-global-this Style ​

🚧 An auto-fix is still under development.

What it does ​

Enforces the use of globalThis instead of environment‑specific global object aliases (window, self, or global). Using the standard globalThis makes your code portable across browsers, Web Workers, Node.js, and future JavaScript runtimes.

Why is this bad? ​

• Portability – window is only defined in browser main threads, self is used in Web Workers, and global is Node‑specific. Choosing the wrong alias causes runtime crashes when the code is executed outside of its original environment. • Clarity – globalThis clearly communicates that you are referring to the global object itself rather than a particular platform.

Examples ​

Examples of incorrect code for this rule:

js
// Browser‑only
window.alert("Hi");

// Node‑only
if (typeof global.Buffer !== "undefined") {}

// Web Worker‑only
self.postMessage("done");

Examples of correct code for this rule:

js
globalThis.alert("Hi");

if (typeof globalThis.Buffer !== "undefined") {}

globalThis.postMessage("done");

How to use ​

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny unicorn/prefer-global-this
json
{
  "rules": {
    "unicorn/prefer-global-this": "error"
  }
}

References ​

Released under the MIT License.