Home >Web Front-end >JS Tutorial >How to clean up Unused Packages in your JavaScript/TypeScript project
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.
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:
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.
Depcheck can be installed globally or as a dev dependency in your project:
npm install -g depcheck
or
npm install --save-dev depcheck
Navigate to your project’s root directory and run:
depcheck
Depcheck will analyze your project and output two key lists:
Here’s what a typical output might look like:
Unused dependencies * moment * lodash Unused devDependencies * jest * eslint Missing dependencies * chalk
From this, you can confidently remove the unused dependencies and add any missing ones.
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.
For automated dependency management, integrate Depcheck into your CI/CD pipeline to ensure your project stays lean throughout development.
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.
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.
The above is the detailed content of How to clean up Unused Packages in your JavaScript/TypeScript project. For more information, please follow other related articles on the PHP Chinese website!