DEV Community

Cover image for How to clean up Unused Packages in your JavaScript/TypeScript project
BrendahKiragu
BrendahKiragu

Posted on

How to clean up Unused Packages in your JavaScript/TypeScript project

What if there is some magic in the developer's world?

Sometimes we get immersed in a project, installing package after package, only to realize along the way: "That’s not the package I needed." So you go ahead and install another package — problem solved, right? But over time, your project accumulates unused packages. I used to sort them unused packages by visiting the package.json.

Until I came across JavaScript's magic sweepbroom, on my adventure of seeing what is interesting on the internet. depcheck It cleaned up my projects in seconds, and I thought I’d share this little trick with someone.

What Is depcheck?

Depcheck is a powerful command-line tool designed to analyze JavaScript and TypeScript projects, by identifying unused packages/dependencies and missing dependencies. It saves you from manually combing through package.json or the node_modules jungle.

With Depcheck, you can:

  • Automatically list unused dependencies in your project.
  • Identify missing dependencies that are being used but not declared in package.json.
  • Customize the analysis to suit your project’s structure and tools.

Why Do You Need Depcheck?

Modern development involves relying on numerous libraries and frameworks. It’s easy for unnecessary dependencies to accumulate over time, making your project:

Bloated — Unused packages increase the size of your node_modules, adding clutter.

Slower - The more packages you have, the slower installation and build processes become.

Harder to Maintain — Keeping track of essential vs. redundant packages becomes overwhelming.

Depcheck tackles these problems head-on by pinpointing exactly which packages you can safely remove.

How to Use Depcheck

Let’s walk through how to integrate Depcheck into your workflow.

1. Install Depcheck

Depcheck can be installed globally or as a dev dependency in your project:

npm install -g depcheck

or

npm install --save-dev depcheck

2. Run Depcheck

Navigate to your project’s root directory and run:

depcheck

Depcheck will analyze your project and output two key lists:

  1. Unused Dependencies — Installed but not used in your code.
  2. Missing Dependencies — Used in your code but not listed in package.json.

3. Example Output

Here’s what a typical output might look like:

Unused dependencies
* moment
* lodash

Unused devDependencies
* jest
* eslint

Missing dependencies
* chalk
Enter fullscreen mode Exit fullscreen mode

From this, you can confidently remove the unused dependencies and add any missing ones.

Advanced Options

Depcheck supports several options to enhance its analysis:

Ignore Specific Dependencies

If there are certain packages you always want to keep, you can ignore them by adding an option:

depcheck --ignores=chalk,jest

Custom Parsers and Detectors

If your project uses unconventional module patterns, you can specify custom parsers or detectors to help Depcheck accurately analyze your code.

Integrate with CI/CD

For automated dependency management, integrate Depcheck into your CI/CD pipeline to ensure your project stays lean throughout development.

  • Cleaning Up with Confidence

Once Depcheck identifies unused dependencies, you can remove them with:

npm uninstall package-name

For missing dependencies, you can add them to your project:

npm install missing-package

This ensures your project’s package.json remains optimized, with no dead weight slowing you down.

Conclusion

By using Depcheck, I managed to declutter my projects and streamline my workflow effortlessly. It’s the tool you didn’t know you needed but can’t imagine working without once you’ve tried it.

If you’re tired of manually sifting through your dependencies or dealing with bloated node_modules, give Depcheck a spin. You’ll be surprised how satisfying a clean, well-structured project can feel.

Have you tried Depcheck, or do you have other tricks for keeping your projects lean? Share your thoughts below—I’d love to hear them! Happy Coding.

Top comments (0)