How do I view the size of npm packages?

NpmPackageSize

Npm Problem Overview


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?

Npm Solutions


Solution 1 - Npm

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 you bundle -

https://bundlephobia.com

Solution 2 - Npm

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

Solution 3 - Npm

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

https://i.stack.imgur.com/5pYoe.png" width="250"/>

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

Solution 4 - Npm

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.

Solution 5 - Npm

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
request@2.83.0: 1.08 MiB

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

Solution 6 - Npm

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

Solution 7 - Npm

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.

Solution 8 - Npm

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

npx howfat jasmine

screensot

Solution 9 - Npm

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.

Solution 10 - Npm

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

Solution 11 - Npm

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.

Solution 12 - Npm

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.

Solution 13 - Npm

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionarielorvitsView Question on Stackoverflow
Solution 1 - NpmShubham KanodiaView Answer on Stackoverflow
Solution 2 - NpmLiron YahdavView Answer on Stackoverflow
Solution 3 - NpmstyfleView Answer on Stackoverflow
Solution 4 - NpmBlackView Answer on Stackoverflow
Solution 5 - Npmarve0View Answer on Stackoverflow
Solution 6 - Npmc01nd01rView Answer on Stackoverflow
Solution 7 - NpmkywView Answer on Stackoverflow
Solution 8 - NpmAlexey ProkhorovView Answer on Stackoverflow
Solution 9 - NpmbkkView Answer on Stackoverflow
Solution 10 - NpmRahul SharmaView Answer on Stackoverflow
Solution 11 - NpmthoragioView Answer on Stackoverflow
Solution 12 - NpmSimon B.View Answer on Stackoverflow
Solution 13 - Npmnishit chittoraView Answer on Stackoverflow