DEV Community

Sameer Gurung
Sameer Gurung

Posted on • Originally published at Medium on

How to check unused npm packages?

Meme showing node_modules as heaviest object in the universe. (source: https://bit.ly/2IG40Wb)

I know… I know… After a streak of installing various npm (Node Package Manager) modules, you tend to forget which ones you still use and which ones you already abandoned. Of course, you can check it manually, but why the hassle when you already have a solution?

Let me introduce you to this genie — ‘depcheck’

Depcheck is a tool for analyzing the dependencies in a project to see: how each dependency is used, which dependencies are useless, and which dependencies are missing from package.json. — npmjs

Let’s start by installing the package depcheck from npm registry. You can install it globally by entering the following code in your terminal:

npm install -g depcheck 

Note: depcheck needs node.js >= 6

Now, navigate to the folder where you want your dependencies to be checked. Then enter:

depcheck

It takes a while depending upon the project’s complexity and then provides you with results which is similar to the output shown below:

depcheck performed in freshly installed ionic 4 project

You can also pass additional parameters according to your need:

depcheck [directory] [arguments]

The [directory] argument is the root level directory of the project where package.json file is present. It defaults to current directory if not specified.

The [arguments] parameter can be used to specify different flags to customise our output. For example: --ignore-bin-packages=true ignores the packages containing bin entry.

Full usage instructions can be found in: https://www.npmjs.com/package/depcheck#usage

Don’t want to install ‘depcheck’? No problem:

If you don’t want to install depcheck , you can use npx to run it without installing globally in your machine.

If you don’t have npx , install it globally by:

npm install -g npx

Then, run depcheck by:

npx depcheck

Here,npx will execute the command either from local node_modules/.bin or from a central cache and installs any packages if required.

Enjoy! 👌

References:

  1. https://www.npmjs.com/package/depcheck
  2. https://www.npmjs.com/package/npx

Top comments (0)