Skip to content

useGlobalThis

biome.json
{
"linter": {
"rules": {
"style": {
"useGlobalThis": "error"
}
}
}
}

Enforce the use of globalThis over window, self, and global.

globalThis is a standard way to access the global object across platforms such as browsers, Web Workers, Node.js and so on, and using it can make your code portable.

However, there are several exceptions that are allowed:

  1. Certain window/Web Workers-specific APIs, such as window.innerHeight and self.postMessage
  2. Window-specific events, such as window.addEventListener('resize')
window.foo;
code-block.js:1:1 lint/style/useGlobalThis ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Prefer globalThis over window, self and global.

> 1 │ window.foo;
^^^^^^
2 │

globalThis is the standard way to access the global object across environments, which improves code portability.

window.addEventListener('click', () => {});
code-block.js:1:1 lint/style/useGlobalThis ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Prefer globalThis over window, self and global.

> 1 │ window.addEventListener(‘click’, () => {});
^^^^^^
2 │

globalThis is the standard way to access the global object across environments, which improves code portability.

globalThis.foo;
globalThis.addEventListener('click', () => {});
// window/Web Workers-specific APIs are allowed
window.innerWidth;
self.postMessage({ type: 'ready' });
// window-specific events are allowed
window.addEventListener('resize', () => {});