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?
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.
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.
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.
You can use an npm module called depcheck (requires at least version 10 of Node).
Install the module:
npm install depcheck -g or yarn global add depcheck
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
There is also a package called npm-check
:
npm-check
Check for outdated, incorrect, and unused dependencies.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With