How do I view the size of npm packages?
Asked Answered
B

15

220

When I search for packages on NPM, I would like to see package sizes (in KB or MB, etc). NPM doesn’t seem to show this information.

How can I determine how much bloat an NPM package will add to my project?


Update 2020 (copied from @styfle answer)

The "Unpacked Size" (basically Publish Size) is available on the npmjs.com website along with "Total Files". However, this is not recursive meaning that npm install will likely be much bigger because a single package likely depends on many packages (thus Package Phobia is still relevant).

There is also a pending RFC for a feature which prints this information from the CLI.

Burkhart answered 16/11, 2016 at 20:47 Comment(1)
some modules depend on other packages and you may be already downloaded them with other module .Brooklyn
M
329

What you probably want to measure is the impact a package has if you were to add it to your app bundle. Most of the other answers will estimate the size of the source files only, which maybe inaccurate due to inline comments, long var names etc.

There is a small utility I made that'll tell you the min + gzipped size of the package after it gets into your bundle.

https://bundlephobia.com

Michellmichella answered 2/4, 2017 at 8:18 Comment(9)
Beautiful UI! Thanks this is exactly what I was looking for. Takes forever on super large packages though.Salian
This link: https://cost-of-modules.herokuapp.com now directs to https://bundlephobia.com A very useful tool btw.Thar
How should the result of bundlephobia be interrupted? For example, for bundlephobia.com/[email protected], it is showing a size of 31kb (minified), but according to cost-of-modules (128.09M), and download-size (36MiB).Seminary
When I run my bundle through I get an unexpected error too many requests, how do I fix that that? @ShubhamKanodia Thanks!Crinum
They have also rolled out a beta version of a package.json scanner!Claussen
this is very great and accurate! I hope it exists as an extension in VScode, Import Cost is not very accurate sometimesPurify
This is extremely elegant. Exactly what I was looking for--thank you!Ayo
@ZeyadShaban they had made it an extension at your time of writing... looks like it's still flown under the radar. It requires package.json at root of workspace, but it looks like it works. marketplace.visualstudio.com/…Tranquilizer
At first glance, this seems like a useful tool. But it really only shows minified sizes.Mcdaniel
P
99

Take a look at this cost-of-modules project. It's an npm package that will list the size of a package and number of children.

Installation: npm install -g cost-of-modules

Usage: Run cost-of-modules in the directory you are working in.

enter image description here

Prady answered 21/1, 2017 at 1:32 Comment(9)
A bit overkill, it need to reinstall all your modules before the computation. But does the job.Lexington
don't use this package - it creates an artifact folder called nodemodules.bak which creates ugly side effectsPerron
install or reinstall will always overkill, Am also looking for the options where nothing can be installed back.Ron
i would prefer to go with @bkk npmjs.com/package/npm-module-statsRon
what ugly side effects this package create?Tyrannicide
Use flag --no-install npx cost-of-modules --no-installRabbet
Great, I ran the command and now my project doesn't workMinistry
@KrystianMateusiak Is also messing around with the loc files.Oxeyed
I ran this module, it creates 8756 files in my node_modules! This package does it without any warning notification. Destructive and rubbish.Tried
T
61

I created Package Phobia early this year with the hope to get the package size information into npmjs.com and also track package bloat over time.

https://packagephobia.com

img

This is designed to measure disk space after you run npm install for server-side dependencies like express or dev dependencies like jest.

You can read more about this tool and other similar tools in the readme here: https://github.com/styfle/packagephobia


Update 2020

The "Unpacked Size" (basically Publish Size) is available on the npmjs.com website along with "Total Files". However, this is not recursive meaning that npm install will likely be much bigger because a single package likely depends on many packages (thus Package Phobia is still relevant).

There is also a pending RFC for a feature which prints this information from the CLI.

Transubstantiation answered 6/9, 2018 at 15:37 Comment(4)
This is great, but why do its results differ so markedly from bundlephobia? e.g. compare the results for lodash.lowerfirstAvivah
@DanyalAytekin Because they're measuring different things. Short answer: if you are looking at a front-end package use BundlePhobia. If you are looking at a back-end package use PackagePhobia. There are more details in the readme if you are interested.Transubstantiation
This is excellent. Can you please do this to calculate the total of the sizesManana
Great tool, but it should probably offer better feedback about the package it's measuring - something to verify you're comparing the right one, like a description.Sibell
T
36

In case you are using webpack as your module bundler have a look at:

I definitely recommend the first option. It shows size in interactive treemap. This helps you to find the size of package in your bundled file.

Webpack Bundle Analyzer

The other answers in this post show you size of the project, but you might not be using all parts of the project, for example with tree shaking. Other approaches then might not show you accurate size.

Twoply answered 12/9, 2017 at 8:14 Comment(1)
Marvelous! In general, the first one may be added as a plugin to a Webpack configuration or passed in Laravel Mix. At the plugin Webpack build time (after the bundling/compilation), it starts a web server on 127.0.0.1:8888 with the interactive map. Related: github.com/webpack-contrib/…Torquemada
A
31

I've created a tool, npm download size, which inspects tarball size for a given npm package, including all tarballs in the dependency tree. This gives you an idea of the cost (install time, disk space, runtime resources, security audit, ...) of adding the dependency up front.

download size of webpack

In image above, Tarball size is tar.gz of package, and Total size is size of all tarballs. The tool is pretty basic, but it does what it says.

A cli tool is also available. You can install it like this:

npm i -g download-size

And use it like this:

$ download-size request
[email protected]: 1.08 MiB

The source code is available on Github: api, cli tool and web client.

Attune answered 14/10, 2017 at 22:10 Comment(4)
Doesn't seem to calculate sizes for any dependencies... ie. when I ran it it just listed the package I asked. When I actually installed, more than 1000 packages were installed... taking up more than 10 times the amount of space reported.Terrorist
Is it perhaps missing recursive dependencies?Terrorist
It does resolve packages recursive, but report tar.gz size, not unpacked size. The web-ui also shows number of recursive dependencies. What package did you test on?Attune
Just the cli. Maybe if it showed a package count... or specified the .tar.gz ....Terrorist
C
16

Try to use package-size.

npx package-size vue,vue-router,vuex react,react-dom,react-router,redux

https://github.com/egoist/package-size package-size npm

Commune answered 25/1, 2018 at 14:44 Comment(0)
A
7

Before publishing the npm package, You can check the size of the package using the following command.

npm publish --dry-run

I have attached the result of my npm package.

enter image description here

Adenine answered 1/7, 2021 at 7:13 Comment(1)
useful—would also be nice if it could sort by sizeSmall
M
6

If you use Visual Studio Code, you could use an extension called Import Cost.

This extension will display inline in the editor the size of the imported package. The extension utilizes webpack with babili-webpack-plugin in order to detect the imported size.

Manicotti answered 28/5, 2018 at 9:28 Comment(2)
Also available for WebStorm.Electromagnet
This extension doesn't work for all the packages. It shows Calculating... and then suddenly vanishes even after changing the timeout value to a very large numberNoh
N
6

howfat is one more tool which can show total package size:

npx howfat jasmine

screensot

Neritic answered 29/10, 2019 at 16:30 Comment(0)
C
3

You could check out npm-module-stats. It is an npm module that gets the size of an npm module and its dependencies without installing or downloading the module.

Usage:

var stats = require("npm-module-stats");

stats.getStats("glob").then((stack) => {

  let dependencies = Object.keys(stack);
  let totalSize = dependencies.reduce((result, key, index) => {
    return result + stack[key].size;
  }, 0);

  console.log('Total Size in Bytes ', totalSize);
  console.log('Total Dependencies ', dependencies.length-1);

}).catch((err) => {
  console.error(err);
});

It might seem a little verbose but it solves the problem you described appropriately.

Creamcups answered 7/3, 2017 at 9:14 Comment(1)
What about 2nd level deps?Kandykane
H
3

I prefer https://github.com/aholachek/bundle-wizard all the way since it was released.

  • It works on deployed sites: npx bundle-wizard reddit.com

  • It works on your local project:

    For multi-page apps/sites adjust the last line with the path you want to check.

    npm run build
    npx serve -s build
    npx bundle-wizard localhost:5000/
    

The interactive view is really helpful in discovering what's where.

Held answered 18/10, 2021 at 14:12 Comment(0)
Q
2

A "quick & dirty" way is to use curl and wzrd.in to quickly download the minified package and then grep the file size:

curl -i https://wzrd.in/standalone/axios@latest | grep Content-Length

The download is minified but not gzipped, but you get a good idea of the relative size of packages when you compare two or more of them.

Quartas answered 13/4, 2017 at 16:39 Comment(1)
Well, use another service like unpkg.com then. Example: curl -i https://unpkg.com/[email protected]/dist/axios.min.js | grep Content-LengthQuartas
W
1

In order to check the impact of different packages on your bundle. You can check out source-map-explorer.

Install:

npm install -g source-map-explorer

Usage:

source-map-explorer bundle.min.js
source-map-explorer bundle.min.js bundle.min.js.map
source-map-explorer bundle.min.js*
source-map-explorer *.js

This will open up a visualization of how space is used in your minified bundle.

enter image description here

Waffle answered 15/3, 2021 at 7:33 Comment(0)
J
1

I created bundlejs for this exact reason. Bundlejs was designed to be a fast and always accessible online bundler that can treeshake, analyze the bundle, minify, gzip & brotli the bundle, give the compressed size, and also return the fully bundled code, you can check it out at https://bundlejs.com.

The source code is available on GitHub at https://github.com/okikio/bundlejs

image of bundlejs bundling & minifying react

image of the final bundle for react

However, if you're goal is to just check what the npm install size for packages, I highly recommend https://packagephobia.com. I didn't create packagephobia but it's a high quality tool that covers the other niche when it comes to checking js package sizes

Jongjongleur answered 8/2, 2023 at 3:38 Comment(0)
D
0

qnm is a great tool for querying the node_modules packages and their size.

https://github.com/ranyitz/qnm

Steps:

npm i --global qnm
qnm doctor

enter image description here

Dangle answered 5/4 at 12:38 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.