globalthis

ECMAScript spec-compliant polyfill and shim for globalThis

Library
npm
v1.0.4
157stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
33/100Needs Attention
Development Activity0
Maintenance0
Community52
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
78/100Good
Architecture80
Code Quality85
Innovation55
Learning Curve92

globalthis is an ECMAScript spec-compliant polyfill and shim for the standard globalThis value. It provides a single, reliable way to reference the global object - window in browsers, global in Node.js, self in workers - without resorting to CSP-unsafe tricks like Function(‘return this’).

It implements the es-shim API: calling the package returns the native globalThis when the environment already supports it, and otherwise falls back to a compliant implementation, with an optional shim() method to install globalThis globally when it is missing.

What You Get

  • A getter that returns the native globalThis when available, or a compliant fallback
  • A shim() method to install globalThis on the global object when it is absent
  • es-shim API compliance with separate polyfill, shim, and implementation entry points
  • Broad compatibility down to ES3-supported environments

Common Use Cases

  • Referencing the global object consistently across browsers, Node.js, and workers
  • Supporting older engines that lack native globalThis
  • Avoiding CSP-unsafe patterns like Function(‘return this’) to obtain the global

Under The Hood

Architecture - The package follows the es-shims layout: implementation.js computes the global object, polyfill.js returns native globalThis when compliant or the implementation otherwise, shim.js installs it onto the global when missing, and index.js/auto.js wire these together. Separate browser-specific implementation and shim files (implementation.browser.js, browserShim.js) handle browser bundling constraints.

Tech Stack - Plain JavaScript with no runtime dependencies, structured around the es-shim API interface and the TC39 global proposal. It targets extremely broad compatibility, working in ES3-supported engines.

Code Quality - A small, mature, heavily depended-upon package (tens of millions of weekly downloads) maintained by ljharb under the es-shims org, with CI via GitHub Actions and code coverage tracking. The implementation is deliberately minimal and defensively written for engine quirks.

API Design - Usage is a one-liner: require(‘globalthis’)() to get the global, or require(‘globalthis’).shim() to install it. The multiple entry points (polyfill, shim, auto) follow a well-known es-shims convention, so anyone familiar with that ecosystem knows exactly how to consume it.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search