To Remove Moment.js, We First Made Everything Depend on It

TypeScript

Over the past ten weeks I led a date standardization effort in a large TypeScript monorepo, a web client with around 60 packages and two apps. The goal sounded simple: remove Moment.js. The reality was three date libraries plus raw Intl calls, a thousand call sites, eight locales, and a product whose users would notice if a single timestamp changed 😅

If you read my previous article about the time zone that was undefined, this was the initiative that incident happened in the middle of.

In this article I will tell you how we did it, what worked, what bit us, and what I would do again.

The starting point

Before touching anything, we audited the codebase, and here is what we found:

  • Moment.js in ~350 production files, with ~1,000 call sites. It has been in maintenance mode since 2020, it’s mutable by default, it’s not tree-shakeable, and it shipped its own chunk on every page load.
  • date-fns in about 20 files, with a token syntax that doesn’t match Moment’s (yyyy-MM-dd vs YYYY-MM-DD).
  • dayjs in one file that nobody knew about.
  • Raw Intl.DateTimeFormat and toLocale*String in ~90 files, with the same options objects copied by hand into six different packages.
  • Two hand-rolled format-string parsers that compared tokens with ===.
  • An internal date-time package with more than 50 dependents, which wrapped six functions and did no formatting at all.

Nothing was broken, but everything was slowly getting worse, and every localization request cost more than it should have, because the answer lived in four places.

The decision that made it possible: facade first, engine later

The obvious plan is to pick the target library, design the ideal API, get sign-off, and then migrate. We didn’t do that, because the design conversation (which formats are “our style”, Moment wording or CLDR wording, presets or raw tokens) would have blocked every code change on people whose time we didn’t control.

Instead, the first step turned the internal date-time package into a thin facade over Moment itself. format(date, 'LL') called moment(date).format('LL'), and add(date, 1, 'month') called Moment’s add. Same tokens, same output, by construction, and that meant:

  • Migrating 500 files carried zero design risk, the output was identical because the same library was still doing the work underneath.
  • The codemod was almost entirely mechanical, because Moment’s token vocabulary was already the API.
  • The design questions moved to the end, where they could take as long as they needed.

Everyone asked me the same question: “isn’t that double work?” It is, but the work you redo stays inside one package, not across 500 files. Every signature the consumers call was designed to survive the engine swap, and it did.

Guardrails before the first migration PR

Nothing moved until four things existed. They took about a week, and they paid for themselves in the first month.

  • A lint fence with a grandfather list: a check:date-usage script that fails CI on any direct use of Moment, dayjs, date-fns, Intl.DateTimeFormat or toLocale*String outside the date-time package (with one exception, more on that below). Existing offenders were listed in a baseline file, so the check passed on day one, and the rule was simple, the list can only shrink. Every migration PR removed its files from the baseline, and no PR could add to it. When the last entry was gone, the baseline file went too, and the fence became absolute.
  • A health report with a kickoff baseline: one command counted the files, references and calls per library, and printed the difference against the day-one snapshot. Progress became a number in every PR description instead of a feeling.
  • A weekly burndown in Slack: a scheduled workflow ran the report every Monday and posted the table to the initiative’s channel. Stakeholders watched the Moment count fall from around 350 to 0 without asking, and anyone whose package was next saw it coming.
  • A validated codemod: we ran the Moment transform in dry-run mode against the whole tree and diffed its output before touching any package. It handled the mechanical 90%, and the rest (the two hand-rolled format parsers, .clone() calls, type annotations without an import) was listed up front as manual work.

Plus one announcement post, so around 40 teams knew their packages would change under them and had a channel to shout in. Nobody did.

When the program closed, a final PR deleted the report, the workflow and the codemod, but the lint stayed. The guardrail is permanent, the tools were just for the journey.

Execution: one package per PR

  • One package per PR, around 75 PRs in total, each merged on green CI, so a bad migration could be reverted without touching anything else. The biggest PR touched more than 60 files, but most of them touched two or three.
  • No delegation: because the output was guaranteed to be identical, we didn’t need each team to migrate their own code. We announced it once and ran it centrally, and more than 60 reviewers from around 40 code-owner teams reviewed the PRs. Believe me, that is a very different cost from 40 teams scheduling the work.

The engine swap

Once every consumer was on the facade, we replaced Moment underneath. And no, we didn’t just swap in date-fns formatting. We ported Moment’s formatting, parsing, relative time and locale data for all eight languages into the package, and used date-fns only for calendar arithmetic, for two reasons:

  • Moment’s output was copy that had been reviewed in production across eight locales, CLDR wording differs in small ways (Spanish month abbreviations lose their dot, Polish month names switch grammatical case in some formats), and nobody had asked for those changes.
  • Porting gave us something exact to test against: golden tests compared the new engine to Moment across time zones, byte-identical or it didn’t merge. With CLDR output, there would have been nothing to compare with.

The swap deleted Moment from the repository and the bundler config, that’s roughly −110 KB raw on every page load 🎉

Later we removed date-fns too. By then it only provided three functions and a UTC date class, about 2 KB in the bundle, so replacing them was a day’s work, and the package now has no third-party dependency at all. It’s a small win in bytes, and a big one in never having to run this migration again.

The design conversation, finally

With the plumbing stable, we asked content design and localization the questions we had pushed to the end:

  1. Which of the format presets are actually our style?
  2. Moment wording or CLDR wording?
  3. Do quarter labels need a preset?
  4. Is the week-start table right for each locale?
  5. Raw tokens in product code, or presets only?

The answers came within a week, because they were five concrete questions with evidence tables, not a design review of a library. English got a style guide: no leading zero on the day, 12-hour time, lowercase am/pm with no space before it, and no minutes on the hour (Jun 9, 2026, 2:05pm, 10am), the same everywhere a time is shown. Other locales kept their wording, and the rule became presets for anything a user reads, and tokens only for machine strings.

The English change was the only visible change in the whole program, so it shipped behind a feature flag, we verified it on staging, turned it on in production, and removed the flag a week later.

Numbers

WhatResult
Duration10 weeks, from kickoff to the last merge
Tickets / PRs~85 / ~75
Lines~29k added, ~9k removed (mostly the ported engine and its golden tests)
Files moved~500, ~400 of them production code
Reviewers60+ people from ~40 code-owner teams, ~250 reviews
Incidents caused by the migration0
Bundle~110 KB raw smaller

What I would do again

Wrap the thing you are removing before you replace it. The facade-first move turned a multi-quarter, cross-team design project into a five-week mechanical migration plus a contained engine swap.

Lint on day one, with a baseline. The fence has to exist before the migration starts, otherwise the codebase drifts back while you work.

Verify output, not intent. “The tests pass” is a much weaker claim than “the new engine produces the same bytes as the old one for these ten thousand inputs across six time zones”.

Keep the visible change separate and behind a flag. Ten weeks of zero-diff refactoring and one week of a deliberate, approved, flagged copy change is a much easier story to tell than mixing the two.

Ask designers concrete questions. Five decisions with evidence tables got answered in days, “please review our date API” would still be sitting in a backlog.

What bit us

Semantic merge conflicts. We removed an API option while another team’s PR added a new caller of it, Git merged both cleanly, and the type check broke on main, because neither PR’s CI could see the other. So re-run CI right before merging anything that removes an API.

Metrics that lie. The bundle-size column in our health report summed the gzipped source of every module reachable from an import, including the nearly 300 date-fns modules that tree-shaking throws away. It reported 180 KB, while the real number was 2 KB 🤯 Measure what ships, not what is imported.

The lint gap you accept comes back. We deliberately didn’t flag bare toLocaleString(), because it’s usually number formatting. And guess what? Within a month of the fence going up, four date-formatting call sites had slipped through that gap, including someone re-creating a per-locale switch we had just deleted 🙈 We closed it by checking the options object for date keys.

What’s next

Nothing, for now, and that was the whole point. The natural next step for the package is the Temporal API once it’s Baseline across browsers, and thanks to the facade, that swap would touch one package, not 500 files.

If you are planning a migration like this one, or you have a question, let’s discuss it in the comments below 👇

Resources

Tot ziens 👋

Read next

Comments loading…