npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

analyze-module-size

v1.4.0

Published

Analyze the size of your module dependencies

Downloads

1,603

Readme

analyze-module-size

NPM version Travis Build Status Coverage Status

Analyze the size of your module dependencies

analyze-module-size shows you, why your package is so large. The reason for the size of your package often lies in the dependencies you are using. For example request@2.81.0` is 7080kb large (including its 53 dependencies) and it comes with a lot of libraries that you may not even need. If you are aware of that, you may choose to use a different library (e.g. popsicle).

The module cost-of-modules does the same, but it only shows one level of the dependency tree. It was an inspiration, but I took no code from it.

Finally, this program still has a lot of opportunities for enhancement. If you have wishes, ideas or questions, please open an issue.

Installation

npm install -g analyze-module-size

Usage

Run analyze-module-size in your project directory. The output will be something like this: (Note that the displayed sizes are accumulated from the each module an its dependencies):

size: 68k... with-dependencies: 1204k
├─┬ [email protected], 488k, 17 deps
│ ├─┬ [email protected], 344k, 10 deps
│ │ ├─┬ [email protected], 136k, 3 deps
│ │ │ └─┬ [email protected], 88k, 2 deps
│ │ │   ├── [email protected], 40k, 0 deps
│ │ │   └── [email protected], 24k, 0 deps
│ │ ├─┬ [email protected], 60k, 2 deps
│ │ │ ├─┬ [email protected], 40k, 1 deps
│ │ │ │ └── [email protected], 20k, 0 deps
│ │ │ └── [email protected], 20k, 0 deps
│ │ ├─┬ [email protected], 40k, 1 deps
│ │ │ └── [email protected], 20k, 0 deps
│ │ ├── [email protected], 32k, 0 deps
│ │ ├── [email protected], 24k, 0 deps
│ │ └── [email protected], 20k, 0 deps
│ ├─┬ [email protected], 44k, 1 deps
│ │ └── [email protected], 24k, 0 deps
│ ├─┬ [email protected], 40k, 1 deps
│ │ └── [email protected], 20k, 0 deps
│ ├── [email protected], 20k, 0 deps
│ └── [email protected], 20k, 0 deps
├─┬ [email protected], 344k, 10 deps
│ ├─┬ [email protected], 136k, 3 deps
│ │ └─┬ [email protected], 88k, 2 deps
│ │   ├── [email protected], 40k, 0 deps
│ │   └── [email protected], 24k, 0 deps
│ ├─┬ [email protected], 60k, 2 deps
│ │ ├─┬ [email protected], 40k, 1 deps
│ │ │ └── [email protected], 20k, 0 deps
│ │ └── [email protected], 20k, 0 deps
│ ├─┬ [email protected], 40k, 1 deps
│ │ └── [email protected], 20k, 0 deps
│ ├── [email protected], 32k, 0 deps
│ ├── [email protected], 24k, 0 deps
│ └── [email protected], 20k, 0 deps
├─┬ [email protected], 144k, 6 deps
│ ├─┬ [email protected], 40k, 1 deps
│ │ └── [email protected], 20k, 0 deps
│ ├─┬ [email protected], 40k, 1 deps
│ │ └── [email protected], 20k, 0 deps
│ ├── [email protected], 20k, 0 deps
│ ├── [email protected], 20k, 0 deps
│ └── [email protected], 20k, 0 deps
├─┬ [email protected], 132k, 1 deps
│ └── [email protected], 20k, 0 deps
├── [email protected], 88k, 0 deps
├── [email protected], 76k, 0 deps
├── [email protected], 52k, 0 deps
├── [email protected], 48k, 0 deps
├─┬ [email protected], 44k, 1 deps
│ └── [email protected], 20k, 0 deps
├── [email protected], 44k, 0 deps
├── [email protected], 20k, 0 deps
└── [email protected], 20k, 0 deps

CLI options

Usage: analyze-module-size [options]

  Analyzes the size of the package in the current directories, including the size of (production) dependencies

  Options:

    -V, --version         output the version number
    -d, --depth <levels>  Show only dependencies up to a given depth of recursion
    -h, --help            output usage information

How it works

`` collects all modules from the node_modules directory, the node_modules directory of each of those modules and the node_modules directory in that modules, and so on.

When all packages have been collected, it reads the package.json of each module and uses the _location-property and the _requiredBy-property to recreate the complete dependency tree.

  • _location contains the location of the module in the directory tree. A module in node_modules/packageA/node_modules/packageB has the location /packageA/packageB
  • _requiredBy contains a list of module that are dependent on the current module. For each such module, it contains the value of the _location-property.

Once the packages is connected, the stats for each package are computed:

  • The number of dependencies is computed transitively across the tree.
  • The total kilobytes (1024 bytes) is computed, include all dependencies. The computation of file sizes assumes that only whole blocks are used, even by small files. The blksize-property of the fs.Stats-object is used as block size. If this value is missing (e.g. on Windows), a size of 4096 is used.

Caveats

In some cases, the dependencies in the node_modules-directory are tempered with. For example, lerna combines dependencies of multiple packages in the node_modules-directory of the root-project and removes obsolete dependencies from the tree. This can lead to cycles in the dependency tree which are displayed in the output like this:

size: 42k... with-dependencies: 42k
└─┬ [email protected], 42k, 3 deps
  └─┬ [email protected], 42k, 3 deps
    └─┬ [email protected], 42k, 3 deps
      └── [email protected] (cycle detected)

Furthermore, this and the use of optional dependencies can lead to a situation where a package is _requiredBy an existing dependency (i.e. a dependent package) but does not exist anymore in the tree. For those delete packages, a dummy package is displayed in a separate tree.

size: 42k... with-dependencies: 42k
└── [email protected], 42k, 0 deps

missing packages, that are referenced as dependent of an existing dependency
└─┬ /dep3, 42k, 2 deps
  └─┬ [email protected], 42k, 1 deps
    └── [email protected], 42k, 0 deps

In this example, a module [email protected] was found. The _requireBy-property shows that dep2 is part of the tree, because it is a dependency of a module that should be in node_modules/dep3, which could not be found.

License

analyze-module-size is published under the MIT-license.

See LICENSE.md for details.

Release-Notes

For release notes, see CHANGELOG.md

Contributing guidelines

See CONTRIBUTING.md.