Site icon MoreOnFew

How to find unused dependencies in npm?

Javascript on MoreOnFew.com

Javascript on MoreOnFew.com

NPM has become the most popular package manager over time. It is really helpful and useful to manage the package dependencies of your project. However, over time, as your project keeps growing and new packages get released, there are times when we move on to a better alternative of a package or even stop using a package since we might not require it anymore. In such cases, it is important that we find unused dependencies in our npm’s package.json so that our project does not get bloated up. It also is important to find outdated packages in npm and to update or remove them.

Finding unused packages manually can be a very cumbersome process and might not be efficient too. In such cases, it is recommended to use any tool that would help you find unused dependencies in package.json.

Here is how you can find unused dependencies in package.json via npm

As I mentioned earlier, finding unused dependencies in package.json manually can be a really tedious task. The easier alternative is to use a tool that would make your task fast and easy. One such tool is depcheck . In their own words :

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

https://www.npmjs.com/package/depcheck

The usage of it is fairly simple. Run the following command and it would find all the unused dependencies in your package.json :

npx depcheck

The command above would execute the package without installing it on your machine. If you are looking to install the package and run it then you can try the following command:

$ npm install -g depcheck

$ depcheck

Below is an example of running depcheck on a sample project where I’ve installed bootstrap but haven’t used it.

D:\project>npx depcheck
npx: installed 146 in 20.628s
Unused dependencies
* bootstrap

In the above example, you can notice that after running the npx depcheck command, it runs the tool and lists the unused dependencies which in my case was only bootstrap. If there were more, it would list all the unused packages in package.json.

Please note that you need to run the commands in the directory where the package.json file can be found since by default it looks for the package.json file in the root directory. Also, please note that you need to have nodejs version 10 or greater installed.

There are many more options that depcheck provides and you can check them out on their official page at https://www.npmjs.com/package/depcheck .

To conclude I would say that it is very easy to find unused dependencies in package.json using npm packages like depcheck etc. It is a good practice to keep the package dependencies on the check and to regularly remove all the unused package dependencies. It is equally important to find outdated packages in npm and to either update them or remove those outdated packages. Hope you found this article to be useful.