I checked the file-size of my ./node_modules
folder and it seems that moment.js is the heaviest non-C library. 2.1MB is quite a lot. A quick look in the folder size shows:
Most of the weight comes from publishing compressed versions of moment.js with npm
as well as double-publish all the locales. But the locales alone are quite heavy by themselves.
As such I thought it might be good for moment.js and the community to reduce this amount?!
./src
folder from being published to npm
?./min
folder from being published to npm
?./locales
folder into a separate npm
package to reduce moment to the minimum?People use moment in different ways, and the general consensus is that disk space in a node app is not a premium in the same way that it is in the browser.
That said, I'm not sure exactly what's going on in the min
folder:
I'm not sure why we need moment-with-locales.min.js
in npm at all. It should be sufficient to have moment.min.js
and locales.min.js
, right?
Also, there shouldn't be a need for non-minimized files in a min
folder (locale.js
and moment-with-locales.js
).
The min folder is really just misnamed. Built might be a better name. But that's just where the moment-with-locales and locales files live until we run the update-index grunt task for release.
We could certainly take the moment-with-locales files out of npm specifically, but I'm not sure what value it provides.
@martinheidegger can you please explain under what conditions is the size of the whole package a problem. The package grows as people want more options to consume the library. If we are to remove the things you list we need to publish separate packages that are minimal in some respect, and then publish also a large package that has everything.
Can you give an example with another library that cuts everything to bare minimum in some/all of its npm builds?
npm install of lodash is 1.4 MB while minified it's only _69 KB_
The fact, that lodash also publishes every function as an own package only leads to a long list of packages that are installed again, because they are referenced in another package.
When looking on my typical node_modules folder with a size of around 300 MB, I'm not sure if it's worth the effort to make moment.js a little bit smaller.
And there are a lot more, event a lot bigger, here are just some of them
While I don't really care of the size of the npm package, I do find astonishing that I have to include 58KB for manipulated dates in a sane way (assuming I don't need locales, which are way heavier) 馃槥
I am tired of arguing this sort of thing. I thought lower footprint would make the installation of my package faster (of which moment is the biggest one). I guess I am running against windmills. So: kthxbye.
@martinheidegger give yarn a try, at least they are more sane than npm and cache the package, so you shouldn't spend as much time on downloads :)
I'm facing the same issue. Doing import moment from 'moment';
increases the bundle size from 30kb to 250 kb!!! Is there an easy solution guys??
@paramsingh88 Sounds like the only option is to not use it. Found this thread because of the same issue. My project size increases by 58kB by simply doing moment.unix(created).format('MMMM D, YYYY')
. Absolutely crazy!
I don't understand this attitude:
disk space in a node app is not a premium in the same way that it is in the browser.
The problem is that moment is not a node app - it's a library that can be used either in a backend application or in the front-end library. In this case, yes, I do need to use Moment.js to validate dates on the front-end of the application before sending them to the backend (which has it's own, seperate, validation.)
The size of moment without locales is 50k (minified), the size with locales is around 300k (minified). If you want to do something about it check the discussion in here https://github.com/moment/moment/issues/2373
And if you use some package manager or whatever that is making life complicated, bring them in also.
Sorry, my bad. (edited)
the general consensus is that disk space in a node app is not a premium in the same way that it is in the browser
I don't think you can build a good lib with this kind of mindset.
The moment people realize bundle size is a real issue, and you chose to do nothing about it, moment
is dead.
For anyone who's looking for lightweight alternatives, date-fns
would be an excellent choice.
Hi, npm moment is about 3mb, however this impacts the size of lambda edit inline tool. shrinking would definitely help
Using this in an Angular app gzipped ends up being 70% the size of my smaller bundles. Would be awesome if we could exclude features we don't need as at the moment I have to import * as moment from 'moment'
the whole package.
moment.js basically doubles the size of a major part of my Angular application:
This really hurts on mobile devices.
@silentsnooc On runtime, you're supposed to load only locales you need. So the main concern for you is to lazy-load locales files only when you use them.
@kylekatarnls If you could show an example for those of us using bundlers, that would be helpful. Thanks.
The example for JSPM given in the doc here:
https://momentjs.com/docs/#/i18n/loading-into-browser/
also work for webpack, and I guess it can do the job for other bundlers.
Not tried, but this comment has a lot of likes:
https://github.com/moment/moment/issues/2517#issuecomment-185836313
I believe moments.js was not meant to be used in the browser front end and there were no plans from maintainers to do so. When we got hit with the similar problem we ended up using date-fns
on the front end and keeping moments.js on the backend. For a new project, I鈥檇 see if date-fns
could be used in both ends.
@demisx I don't need to see more:
Thanks for the hint. :+1: Also thx to @xinghul who mentioned it first.
moment.js
Just replaced momentjs with dayjs. Seems to be working well for my purposes and it's only around 15Kb with locale and fromNow plugin set. Fantastic.
Ionic 4/ Angular 8 app bundle size differences:
with momentJS
: it adds = 204 KB
with dayJS
: it adds = 6 KB
I just need basic data/time formatting and manipulations only. So I have decided to go with dayJS
: https://github.com/iamkun/dayjs
I'm also getting 249kb, not sure if collaborator is going to do anything about this.. moving to date.fns
Not tried, but this comment has a lot of likes:
#2517 (comment)
This worked well for everyone searching for a quick optimization.
Allowed my moment to go from 654.7kB to 178.4kB. It may still be big, but it's an improvement.
Wish we could import function as date-fns.
Most helpful comment
I don't think you can build a good lib with this kind of mindset.
The moment people realize bundle size is a real issue, and you chose to do nothing about it,
moment
is dead.For anyone who's looking for lightweight alternatives,
date-fns
would be an excellent choice.