As a project grows, it’s common for unused files, dependencies, and exports to accumulate. Over time, these unnecessary elements clutter your codebase, increasing the complexity and making maintenance harder. In my experience, keeping a clean codebase is critical to maintaining efficiency and reducing technical debt. One tool that has consistently helped me with this is Knip, a utility that detects and helps you clean up unused files, dependencies, and exports.
Thank me by sharing on Twitter 🙏
In this post, I’ll guide you through how to use Knip to keep your project lean, organized, and efficient. I’ve used it in my own projects, and it’s saved me countless hours of manual codebase auditing.
Why Use Knip?
Unused files, orphaned dependencies, and leftover exports are inevitable as a project evolves. These remnants can add complexity, bloat your project size, and slow down development. Knip simplifies the cleanup process by scanning your project and identifying what’s no longer needed.
I’ve often found that even after refactoring, a few unused components or dependencies are left behind. Knip quickly identifies these so that I can clean them up, keeping the project light and easy to manage.
Running Knip for the First Time
You don’t need to install Knip globally. Instead, you can use it directly with npx
. Running Knip is as simple as:
The Coming Wave: AI, Power, and Our Future
$20.00 (as of January 22, 2025 11:32 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)4Pack [Apple MFi Certified] Charger Lightning to USB Charging Cable Cord Compatible iPhone 14/13/12/11 Pro/11/XS MAX/XR/8/7/6s Plus,iPad Pro/Air/Mini,iPod Touch
$8.49 (as of January 22, 2025 11:32 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)iPhone Charger 3 Pack 10 ft Apple MFi Certified Lightning Nylon Braided Cable Fast Charging Cord Compatible with iPhone 13 12 11 Pro Max XR XS X 8 7 6 Plus SE iPad and More
$6.99 (as of January 22, 2025 11:32 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)npx knip
When you run this command, Knip will automatically scan your codebase and output a report of unused files, exports, and dependencies. The tool categorizes them, making it easy to see what you can remove.
Here’s an example of the types of results you might see:
- Unused files: Files that are not imported anywhere.
- Unused exports: Functions, constants, or classes that are exported but never used.
- Unused dependencies: Packages listed in your
package.json
that are not imported in your code.
Checking for Unused Dependencies
One of the primary areas where Knip shines is identifying unused dependencies. It’s common to install a package to experiment with a feature or for a quick fix, and then forget to remove it. Over time, these unnecessary dependencies can build up and bloat your project.
To check for unused dependencies in your project, run:
npx knip --dependencies
This command focuses specifically on your package.json
, comparing the listed dependencies with what is actually imported in your codebase. If Knip finds any unused dependencies, you can uninstall them easily.
For example, if Knip shows:
Unused dependencies:
- axios
- dayjs
You can clean them up with:
npm uninstall axios dayjs
By removing these unused dependencies, you reduce the size of your project and eliminate potential vulnerabilities or maintenance overhead from packages you no longer need.
Detecting Unused Exports
Another common issue in a growing project is leftover exports—functions, constants, or classes that are exported but no longer used. This often happens after refactors, where parts of the code are no longer required but were forgotten during cleanup.
To identify these unused exports, you can simply run:
npx knip --exports
Knip will analyze your project and list any exports that are not used anywhere in the codebase. Once identified, you can safely remove them, cleaning up both the exported files and any related code.
For instance, if you’ve refactored out an old utility function, but the file is still being exported, Knip will flag it. I always double-check these results to make sure nothing is being dynamically imported or used in unconventional ways, but in most cases, these exports can be safely removed.
Cleaning Up Unused Files
Unused files are another area where Knip provides significant value. Whether it’s an old utility script or a component that was removed from the project’s user interface, leftover files can build up over time and make your project unnecessarily complex.
To check for unused files, run:
npx knip --files
Knip will list files in your project that are not imported anywhere else. These are typically safe to remove. As with unused exports, it’s a good idea to double-check the flagged files in case they’re being used in ways Knip can’t detect (such as through dynamic imports), but I’ve found Knip’s results to be accurate most of the time.
For example, you might see something like this:
Unused files:
- src/utils/oldHelper.ts
- src/components/UnusedComponent.tsx
After reviewing, I’ll go ahead and delete these files to keep the codebase clean. This reduces the overall complexity of the project and makes it easier for others (or my future self) to navigate.
Handling Dynamic Imports and Special Cases
Knip works well out of the box, but there may be times when it flags something as unused that you know is still needed, such as dynamically imported modules. In these cases, you can tell Knip to ignore specific files or patterns by creating a knip.json
configuration file.
For example, if you know a file is being dynamically imported but Knip keeps marking it as unused, you can add it to the ignore list like this:
{
"ignore": ["src/dynamicImportFile.ts"]
}
This ensures that Knip skips over this file during future analyses. It’s a simple way to customize the tool to better suit the needs of your specific project.
Automating Knip in Your Workflow
To get the most out of Knip, I recommend making it a regular part of your workflow. Instead of manually running the command every time, you can integrate Knip checks into your CI pipeline or add it to your package.json
scripts for easy access.
For example, you can add this script to your package.json
:
{
"scripts": {
"check-unused": "knip"
}
}
Now, whenever you want to run Knip, you can simply execute:
npm run check-unused
This ensures you regularly scan your project for unused dependencies, files, and exports. Doing so helps prevent bloat and keeps your project lean over time.
Conclusion
Keeping a codebase clean is one of the most important practices in software development. It makes the project easier to maintain, reduces the likelihood of bugs, and improves overall performance. Knip is a powerful tool that simplifies this process by scanning your project for unused files, exports, and dependencies, allowing you to remove them with confidence.
By incorporating Knip into your regular development routine, you can ensure your codebase stays tidy and efficient, freeing you to focus on what matters most—building great software.