221

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.

1
  • 1
    some modules depend on other packages and you may be already downloaded them with other module .
    – mercury
    Commented Oct 26, 2021 at 2:46

15 Answers 15

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

9
  • 9
    Beautiful UI! Thanks this is exactly what I was looking for. Takes forever on super large packages though. Commented Jul 20, 2017 at 22:34
  • 7
    This link: https://cost-of-modules.herokuapp.com now directs to https://bundlephobia.com A very useful tool btw.
    – Adam Weber
    Commented Oct 12, 2017 at 20:19
  • 2
    They have also rolled out a beta version of a package.json scanner!
    – Luke
    Commented Feb 19, 2020 at 11:31
  • 3
    this is very great and accurate! I hope it exists as an extension in VScode, Import Cost is not very accurate sometimes Commented Dec 6, 2020 at 6:05
  • 2
    At first glance, this seems like a useful tool. But it really only shows minified sizes.
    – Seph Reed
    Commented May 28, 2022 at 17:57
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

9
  • 6
    A bit overkill, it need to reinstall all your modules before the computation. But does the job.
    – pdem
    Commented Nov 3, 2017 at 8:14
  • 46
    don't use this package - it creates an artifact folder called nodemodules.bak which creates ugly side effects
    – Nth.gol
    Commented May 22, 2018 at 21:23
  • 2
    install or reinstall will always overkill, Am also looking for the options where nothing can be installed back. Commented Aug 18, 2020 at 2:42
  • 8
    what ugly side effects this package create? Commented Feb 23, 2021 at 19:24
  • 6
    Use flag --no-install npx cost-of-modules --no-install Commented Jul 15, 2022 at 5:52
62

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.

4
  • 4
    This is great, but why do its results differ so markedly from bundlephobia? e.g. compare the results for lodash.lowerfirst Commented Oct 25, 2018 at 13:27
  • 10
    @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.
    – styfle
    Commented Oct 25, 2018 at 13:52
  • This is excellent. Can you please do this to calculate the total of the sizes Commented Jan 7, 2022 at 7:58
  • 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. Commented Mar 2 at 3:06
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.

1
  • 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/…
    – Artfaith
    Commented Dec 24, 2022 at 22:58
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.

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.
    – dagelf
    Commented Nov 18, 2020 at 10:31
  • Is it perhaps missing recursive dependencies?
    – dagelf
    Commented Nov 18, 2020 at 10:37
  • 1
    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?
    – arve0
    Commented Nov 18, 2020 at 11:05
  • Just the cli. Maybe if it showed a package count... or specified the .tar.gz ....
    – dagelf
    Commented Nov 18, 2020 at 15:59
17

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

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

1
  • 1
    useful—would also be nice if it could sort by size
    – IliasT
    Commented Jan 31, 2022 at 19:07
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.

2
  • Also available for WebStorm.
    – JoeTidee
    Commented Aug 12, 2018 at 10:56
  • 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 number
    – shreyansh
    Commented Jan 23, 2020 at 13:41
6

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

npx howfat jasmine

screensot

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.

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

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.

1
  • 2
    Well, use another service like unpkg.com then. Example: curl -i https://unpkg.com/[email protected]/dist/axios.min.js | grep Content-Length
    – thoragio
    Commented Apr 20, 2017 at 8:00
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

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

1

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

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.