Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPM package size of moment.js is bothersome #3376

Closed
3 tasks
martinheidegger opened this issue Aug 16, 2016 · 26 comments
Closed
3 tasks

NPM package size of moment.js is bothersome #3376

martinheidegger opened this issue Aug 16, 2016 · 26 comments

Comments

@martinheidegger
Copy link

martinheidegger commented Aug 16, 2016

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:

https://gyazo.com/60cb5b6e69c76767c6d001669ff25a2d

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?!

  • Would it be possible to omit the ./src folder from being published to npm?
  • Would it be possible to omit the ./min folder from being published to npm?
  • Would it be possible to move the ./locales folder into a separate npm package to reduce moment to the minimum?
@mattjohnsonpint
Copy link
Contributor

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:

image

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).

@maggiepint
Copy link
Member

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.

@ichernev
Copy link
Contributor

ichernev commented Sep 7, 2016

@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?

@DaSchTour
Copy link

DaSchTour commented Sep 7, 2016

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

  • rx: 6,6 MB
  • rxjs: 3,3 MB
  • @angular: 10,8 MB
  • Intl: 79,1 MB
  • grunt: 6,2 MB

@theofidry
Copy link

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) 😞

@martinheidegger
Copy link
Author

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.

@theofidry
Copy link

@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 :)

@paramsingh88
Copy link

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??

@codeaid
Copy link

codeaid commented Feb 16, 2017

@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!

@kerryboyko
Copy link

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.)

@ichernev
Copy link
Contributor

ichernev commented May 3, 2017

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 #2373

And if you use some package manager or whatever that is making life complicated, bring them in also.

Sorry, my bad. (edited)

@xinghul
Copy link

xinghul commented Aug 1, 2018

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.

screen shot 2018-08-01 at 11 16 23 am

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.

@mp3killa
Copy link

Hi, npm moment is about 3mb, however this impacts the size of lambda edit inline tool. shrinking would definitely help

@BovineEnthusiast
Copy link

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.

@ghost
Copy link

ghost commented Sep 8, 2019

moment.js basically doubles the size of a major part of my Angular application:

image

This really hurts on mobile devices.

@kylekatarnls
Copy link

@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.

@bradisbell
Copy link

@kylekatarnls If you could show an example for those of us using bundlers, that would be helpful. Thanks.

@kylekatarnls
Copy link

kylekatarnls commented Sep 8, 2019

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.

@kylekatarnls
Copy link

Not tried, but this comment has a lot of likes:
#2517 (comment)

@demisx
Copy link

demisx commented Sep 9, 2019

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’d see if date-fns could be used in both ends.

@ghost
Copy link

ghost commented Sep 9, 2019

@demisx I don't need to see more:

image

Thanks for the hint. 👍 Also thx to @xinghul who mentioned it first.

@xgqfrms
Copy link

xgqfrms commented Oct 11, 2019

find a more small size lib, which is an alternative to moment.js

https://github.com/iamkun/dayjs

@kulttuuri
Copy link

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.

@Sampath-Lokuge
Copy link

Sampath-Lokuge commented Feb 24, 2020

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

@Bluefitdev
Copy link

Screen Shot 2020-05-15 at 11 45 16 am

I'm also getting 249kb, not sure if collaborator is going to do anything about this.. moving to date.fns

@Monakiel
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests