How to check unused npm packages?
Tags: javascript, programming, npm, unuse
Published: 2023-02-07 10:25:54

There are several solutions available, depcheck and npm-check are the most common ones.

**depcheck**
Depcheck analyzes the dependencies in a project to see: how each dependency is used, which dependencies are useless, and which dependencies are missing from package.json.

To use depcheck from the command line you have to install it. depcheck requires Node.js >= 10.

**npm install -g depcheck**
After installing it, it can be used with typing depcheck in the root project directory, where the package.json file is. The full syntax of the command looks like this depcheck [directory] [arguments]. Depending on the size of your project the execution can take a while.

Your output should look something like this.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4qx8riat0d29kmxkmnis.png)


I ran depcheck in the repository of this website. The output shows that I have six unused dependencies (3x dependencies, 3x dev dependencies), which I am going to remove with npm uninstall.

If you don’t want to install depcheck globally, run it with npx.

**npx depcheck**
You can also pass additional arguments to depcheck, please have a look at the official documentation.

**npm-check**
npm-check checks for outdated, incorrect, and unused dependencies.

To use npm-check from the command line you have to install it. It requires Node >= 0.11.

**npm install -g npm-check**
After installing it, it can be used with typing npm-check in the root project directory, where the package.json file is. Depending on the size of your project the execution can take a while.

The output of npm-check has more information compared to depcheck.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sjjnncv0srz51a6pstvd.png)

I ran npm-check in the repository of this website, and the output is quite long, since I have not updated to the latest major version of gatsby (It's on the todo list.). npm-check will give you a nice and clear output of the out-of-date dependencies and unused dependencies. It also has a nice, interactive dependency update feature, when adding the -uor --update flag. npm-check will then show an interactive UI for choosing which modules to update and automatically updates versions referenced in the package.json. Have a look at the official documentation for a full list of options when using npm-check.

If you don’t want to install npm-check globally, run it with npx.

npx npm-check
npm-prune
This npm command can be used to remove not required packages from your node_modules directory and devDependencies modules if NODE_ENV environment variable is set to production and if you don’t want remove devDependencies then you need to set — production=false

Now let’s see, how to use npm prune with example:

How to use npm prune with an example?
steps by step procedure to use npm prune:

**Steps to Remove unused packages from Node.js**

1. First, remove the npm packages from packages.json file and save the file.
2. To remove any specific node package run the command npm prune <pkg>
3. run the npm prune command to remove unused or not required node packages from Node.js
4. if you want to remove devDependencies then run prune command with –production flag npm prune — production=true
5. if you don’t want to unbuild devDependencies then you need to set –production flag false npm prune — production=false

If you see an npm module remain in your node_modules directory even after running npm prune even though it’s not in package.json, then you need to check your npm-shrinkwrap.json if it’s present then you need to delete it and then You can follow below method to solve this problem.
READ Get List of all files in a directory in Node.js

If you want to completely remove the node_modules directory and want to do a fresh npm installthen this below one-line can be very useful:

**rm -rf node_modules && npm install**
That was quick!

But, This can take some time depending upon the size of the node_modules directory.

Thank you for keeping reading. If you liked the article