Having Moment.js Replacements Is Not Enough
Tags: webdev, javascript
Published: 2020-11-09 06:50:09

# Having Moment.js Replacements Is Not Enough
A few months ago, moment.js announced the project is now in [maintenance mode](https://momentjs.com/docs/#/-project-status/). They discourage you from using it in new projects, offer alternatives (including their own [Luxon](https://moment.github.io/luxon/)) and tell of a bright future with [Temporal](https://github.com/tc39/proposal-temporal) (a stage 2 proposal at the time of writing).
This is good; Moment.js has served us well, but it currently a huge library that doesn’t support tree-shaking, and isn't as performant as other libraries.
It is, however, still a dependency of many, many libraries, and it’ll take a long time for this to change (if at all - not all libraries have active development going on).
As long as you’re installing one of these packages, you’re going to have a 
[non-tree-shakable, 232kb (68kb if your dependency developer knew to ignore locales) hunk of code](https://inventi.studio/en/blog/why-you-shouldnt-use-moment-js), in addition to whatever time-and-date library you’d like to be using.
In my case, I usually end up using moment.js in my larger projects, as I see no reason to include two libraries to work with dates, especially when one is this huge.

## What can we do?
We CAN wait a few years, but most companies won’t appreciate this kind of timeline. Another solution is to **provide moment API wrappers for the new generation of time and date libraries.** I’ll explain:

Let’s say I’m publishing my own cool new time and date library. Let’s call it Chair.js, as coming with good names for libraries is hard (I spent more time finding a name for [Inventar](https://www.npmjs.com/package/inventar) than coding its first release), and I looked at a chair just now.

Chair is light, fast and simple to work with. It sits well in its role (eh?). Its API is modern and immutable, which means that it’s different than moment.js.
I also know that developers wouldn’t use Chair if they have moment.js installed as a dependency of a dependency already, which they probably do. So I’ll make sure that these dependencies of dependencies can easily use Chair instead, by providing an additional package - chair-moment-wrapper.

**chair-moment-wrapper:**
* Has a similar interface to that of moment.js, or at least its most used features. This interface calls Chair behind the scenes.
* Has Chair as a [peer dependency](https://nodejs.org/es/blog/npm/peer-dependencies/), so no additional copy of it is installed.
* Is optimally tree-shakable.
* Isn’t necessarily optimized for performance. It can get away with being at moment.js’ ballpark.

As a module, it should add itself as moment, or the user should install it with an alias (`npm install —save moment@npm:chair-moment-wrapper` or `yarn add moment@npm:chair-moment-wrapper`).
It should also replace the moment global object, if it’s added.

With this kind of wrappers for major time and date libraries, most developers could pick one knowing they wholly and completely replace moment.js - even for their dependencies. They could reap most of Chair’s size and performance benefits and, most importantly, could more quickly get to the day where they won’t have to use it.

So, developers of [Luxon](https://moment.github.io/luxon/), [date-fns](https://date-fns.org/), [js-joda](https://js-joda.github.io/js-joda/), [Day.js](https://day.js.org/) - What do you think?