Website Development

Checking for Unused Libraries with depcheck

IN THIS POST
  1. Why clean up unused dependencies?
  2. What depcheck does and what it needs
  3. How to remove unused packages with depcheck
  4. What removing unused dependencies gets you
  5. Limitations
  6. Keeping a project free of unused dependencies
  7. Further Reading

Why clean up unused dependencies?

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.

What depcheck does and what it needs

  • Tool Used: depcheck
  • Purpose: Find unused npm packages and clean up your node_modules
  • Prerequisite: Your project must contain a package.json file

How to remove unused packages with depcheck

Install depcheck, run it in the folder that holds your package.json, review what it lists and remove what you do not need.

  1. Install depcheck Globally
    To begin, install depcheck globally on your system by running:

    bash
    npm install -g depcheck
  2. Run depcheck
    Navigate to the folder where you want to check for unused dependencies. Make sure this folder contains your package.json file. Run the following command:

    bash
    depcheck
  3. Review the Output
    If you’ve installed a package, say react-bootstrap, but haven’t imported anything from it, depcheck will list it under Unused dependencies. Example Output:

    bash
    Unused dependencies * react-bootstrap Missing dependencies * ...
  4. Remove Unused Packages
    Once identified, you can remove any unwanted packages by running:

    bash
    npm uninstall <package_name>

    For example:

    bash
    npm uninstall react-bootstrap

What removing unused dependencies gets you

Fewer dependencies mean faster installs and deployments, a smaller attack surface and a project that is easier to maintain.

  • Improved Performance: Smaller node_modules directory 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

  • depcheck may occasionally report false positives, particularly for packages used dynamically (e.g., via require() or with unconventional import structures).
  • Dependencies listed as peerDependencies or devDependencies may require careful consideration before removal.

Keeping a project free of unused dependencies

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.

See the six things I am hired to do and how an engagement runs.

Available from 12 December 2026.

Outside IR35, remote, six months or longer. Tell me the workstream and the date it has to land. I reply within 24 hours, and we start with a call.