Sub topic 2
Running Temporal Today: Native Support and Polyfills
Before you write any Temporal code, you need to know whether the JavaScript runtime you target provides the Temporal global. This chapter covers how to check for it, how to fall back to a polyfill when it is missing, and the problems that come up when the two are mixed.
This chapter assumes you can run JavaScript in Node.js or a browser, install npm packages, and use ES module import syntax.
Where Temporal is available
Temporal reached Stage 4 of the TC39 process, which means it is part of the ECMAScript standard. Engines are shipping it at different times:
| Runtime | Status |
|---|---|
| Firefox | Shipped natively starting with Firefox 139 |
| Chrome, Edge, and other Chromium browsers | Shipped natively starting with version 144 |
| Safari | Check current release notes; support arrived later than in the other engines |
| Node.js | Follows the V8 version it bundles; recent major releases include it, older LTS lines do not |
| Deno, Bun | Depends on the engine version; check the runtime's documentation |
This table goes out of date quickly. Check the MDN compatibility table for Temporal or caniuse.com before you decide whether you can skip the polyfill. Most applications also have to support users on older browsers and servers running older Node LTS releases, so plan for both cases.
The two main polyfills
A polyfill is a library that implements a missing standard API in plain JavaScript. Two polyfills are widely used:
| Package | Maintained by | Notes |
|---|---|---|
| `@js-temporal/polyfill` | Members of the Temporal proposal champions group | Follows the specification closely. Larger bundle. Exports `Temporal`, `Intl`, and `toTemporalInstant` but does not install a global. |
| `temporal-polyfill` | The FullCalendar project | Much smaller bundle. Exports `Temporal`, and also offers `temporal-polyfill/global`, which installs `globalThis.Temporal`. |
Both packages implement the same API, so the code in this tutorial runs unchanged on either one. Install the one you choose:
npm install @js-temporal/polyfillPattern 1: static fallback
The simplest approach is to import the polyfill and use the native global when it exists:
// Use the native global when available, otherwise the polyfill
import { Temporal as TemporalPolyfill } from '@js-temporal/polyfill';
const Temporal = globalThis.Temporal ?? TemporalPolyfill;
console.log(Temporal.Now.plainDateISO().toString());The ?? operator returns the right-hand side only when globalThis.Temporal is null or undefined, so native implementations win when they exist. The cost is that the polyfill ships in your bundle even when it never runs. On a server that is rarely a problem. In a browser bundle it adds download size for users who don't need it.
To use this everywhere, put it in one module and import Temporal from there:
// temporal.js
import { Temporal as TemporalPolyfill } from '@js-temporal/polyfill';
export const Temporal = globalThis.Temporal ?? TemporalPolyfill;// app.js
import { Temporal } from './temporal.js';
console.log(Temporal.PlainDate.from('2024-03-15').dayOfWeek); // 5Pattern 2: load the polyfill only when needed
A dynamic import() downloads the polyfill only when the runtime lacks Temporal. Bundlers such as Vite, webpack, and esbuild split dynamic imports into a separate chunk:
// temporal-loader.js
export async function loadTemporal() {
if (typeof globalThis.Temporal !== 'undefined') {
return globalThis.Temporal;
}
const { Temporal } = await import('@js-temporal/polyfill');
return Temporal;
}// main.js (an ES module, so top-level await is allowed)
import { loadTemporal } from './temporal-loader.js';
const Temporal = await loadTemporal();
console.log(Temporal.Now.instant().toString());The trade-off is that everything that uses Temporal now depends on an asynchronous step. You usually await it once during startup and pass the result along, or store it in a module that the rest of the app imports after startup.
Pattern 3: install a global
If you would rather write Temporal everywhere with no imports, temporal-polyfill can install the global for you, and it only does so when the global is missing:
import 'temporal-polyfill/global';
console.log(Temporal.Now.plainDateISO().toString());This is convenient in applications. Libraries should avoid it, because a library that modifies globals changes behavior for every other package in the same app.
Common errors
Mixing native and polyfill objects
The native and polyfill classes are different classes. A PlainDate made by the polyfill is not instanceof the native Temporal.PlainDate, and the two sides check internal slots, which are hidden per-object data, so their methods may reject each other's objects. This usually happens when one module imports the polyfill directly while another uses the global. The fix is to route every use through a single module, as shown in Pattern 1.
Passing polyfill objects to native Intl
The native Intl.DateTimeFormat only recognizes native Temporal objects. When you use @js-temporal/polyfill, format with the object's own toLocaleString() or use the Intl that the polyfill exports:
import { Temporal, Intl as TemporalIntl } from '@js-temporal/polyfill';
const date = Temporal.PlainDate.from('2024-03-15');
const fmt = new TemporalIntl.DateTimeFormat('en-US', { dateStyle: 'long' });
console.log(fmt.format(date)); // 'March 15, 2024'Expecting `Date.prototype.toTemporalInstant`
The specification adds toTemporalInstant() to Date.prototype. @js-temporal/polyfill doesn't patch Date. It exports the method as a function instead, which you call with Date as this:
import { toTemporalInstant } from '@js-temporal/polyfill';
const instant = toTemporalInstant.call(new Date('2024-03-15T12:00:00Z'));
console.log(instant.toString()); // '2024-03-15T12:00:00Z'Time zone data differences
Native Temporal reads time zone rules from the engine's ICU data. The polyfills get them from the host's Intl.DateTimeFormat. A runtime built with reduced ICU data, such as some minimal Node builds, may reject time zone identifiers that work elsewhere. Test with the exact runtime you deploy to.
Exercise
Your browser bundle imports @js-temporal/polyfill statically, and a teammate notices that users on current Chrome still download it. Another module in the codebase calls new Intl.DateTimeFormat().format(date) on dates created from globalThis.Temporal ?? TemporalPolyfill. What two changes fix the bundle size and keep formatting reliable on both paths?
Show answer
Load the polyfill with a dynamic import() that runs only when globalThis.Temporal is undefined, so native users never fetch the chunk. For formatting, call date.toLocaleString(locale, options) instead of passing the object to the global Intl.DateTimeFormat, because the native Intl does not recognize polyfill objects, while toLocaleString works on either implementation.
Exercise
Why should every module get Temporal from a single shared module instead of each choosing between the native global and the polyfill on its own?
Show answer
The native and polyfill classes are distinct. If modules make different choices, objects from one implementation reach methods of the other, which can fail brand checks and always break instanceof. A single shared reference guarantees one implementation per process.