Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find unused npm packages in package.json

Is there a way to determine if you have packages in your package.json file that are no longer needed?

For instance, when trying out a package and later commenting or deleting code, but forgetting to uninstall it, I end up with a couple packages that could be deleted.

What would be an efficient way to determine if a package could safely be deleted?

like image 448
Josh C Avatar asked Mar 27 '14 00:03

Josh C


People also ask

How do I find unused packages in json?

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 unused from package. json. To identify the unused package, just run npx depcheck in the project root directory.

How do I clean up unused npm packages?

You can use npm-prune to remove extraneous packages. Extraneous packages are packages that are not listed on the parent package's dependencies list. If the --production flag is specified or the NODE_ENV environment variable is set to production, this command will remove the packages specified in your devDependencies.

How do I see all npm packages?

To view the npm global packages list and their dependencies, you can run the following npm list command followed by the “-g” flag where g stands for global. As you can see in the above result, all the global packages are displayed in a tree-like structure.


2 Answers

You can use an npm module called depcheck (requires at least version 10 of Node).

  1. Install the module:

     npm install depcheck -g   or   yarn global add depcheck 
  2. Run it and find the unused dependencies:

     depcheck 

The good thing about this approach is that you don't have to remember the find or grep command.

To run without installing use npx:

npx depcheck  
like image 90
German Attanasio Avatar answered Sep 20 '22 13:09

German Attanasio


There is also a package called npm-check:

npm-check

Check for outdated, incorrect, and unused dependencies.

enter image description here

It is quite powerful and actively developed. One of it's features it checking for unused dependencies - for this part it uses the depcheck module mentioned in the other answer.

like image 43
alecxe Avatar answered Sep 19 '22 13:09

alecxe