Checking for Unused Libraries with depcheck
Introduction
Keeping your project clean and efficient is crucial, especially when dealing with dependencies. Over time, unused libraries can accumulate, bloating your node_modules folder and potentially causing conflicts. In this post, I'll show you how I used depcheck to identify and remove unnecessary dependencies from a Node.js project.
Preface
- Tool Used: depcheck
- Purpose: Find unused npm packages and clean up your
node_modules - Prerequisite: Your project must contain a
package.jsonfile
Solution
-
Install depcheck Globally
To begin, installdepcheckglobally on your system by running:npm install -g depcheck -
Run depcheck
Navigate to the folder where you want to check for unused dependencies. Make sure this folder contains yourpackage.jsonfile. Run the following command:depcheck -
Review the Output
If you’ve installed a package, sayreact-bootstrap, but haven’t imported anything from it,depcheckwill list it underUnused dependencies. Example Output:Unused dependencies * react-bootstrap Missing dependencies * ... -
Remove Unused Packages
Once identified, you can remove any unwanted packages by running:npm uninstall <package_name>For example:
npm uninstall react-bootstrap
Why
- Improved Performance: Smaller
node_modulesdirectory resulting in faster installations and deployments. - Reduced Security Risks: Fewer dependencies reduce the attack surface for potential vulnerabilities.
- Cleaner Codebase: Less clutter means a more maintainable project.
Limitations
depcheckmay occasionally report false positives, particularly for packages used dynamically (e.g., viarequire()or with unconventional import structures).- Dependencies listed as
peerDependenciesordevDependenciesmay require careful consideration before removal.
Conclusion
Using depcheck is a straightforward way to ensure your Node.js projects remain free from unnecessary dependencies. Regularly running this tool can help you maintain a cleaner and more efficient codebase.
Further Reading
For more details, refer to the official depcheck documentation.
My Technical Skills

AWS

JavaScript

TypeScript

React

Next.js

Cypress

Figma

