Back to posts

I finally learned about Knip, and dead code cleanup got much easier

A note on Knip, a tool that finds unused files, dependencies, and exports in JavaScript and TypeScript projects.

May 26, 20264 min read
Knip
TypeScript
JavaScript
Tooling

Share this article

TL;DR

  • Knip finds unused files, dependencies, and exports in JavaScript and TypeScript projects.
  • Its strength is project-wide analysis, which catches things ESLint does not usually cover.
  • It has plugins for tools such as Next.js, Vitest, Storybook, Vite, Nx, and many others.
  • Do not delete everything blindly. Start with the report, review the findings, and clean things up in small pull requests.

I finally learned about it

I only recently learned about Knip.

At first I thought, "Unused code detection? ESLint and TypeScript already cover most of that, right?"
That was not quite right. Knip looks at unused files, unused package dependencies, and unused exports across the whole project.

Things like:

  • utils/old-parser.ts left behind after a refactor
  • dependencies still sitting in package.json after the feature that used them was removed
  • exported functions that no one imports anymore
  • components where only the test or story survived, while the real app no longer uses them

Each one is small.
But over time, they turn into decision cost. Every cleanup starts with "Can I actually delete this?"

Why Knip feels useful

The best part is that Knip works at project scope.

ESLint is great at finding unused variables and imports inside a file.
But checking whether an export is unused anywhere in the project, whether a file is unreachable from entry points, or whether a dependency in package.json is still needed is a different layer.

Knip covers that layer.

The official site describes Knip as a tool that finds and fixes unused dependencies, exports, and files. It also analyzes projects using entry points based on the frameworks and tools in the repository, which makes it much more practical than a simple text search.

The plugin support is also a big deal.
As of this post, the official docs mention around 150 plugins, including Next.js, Vitest, Jest, Storybook, Vite, Webpack, Nx, Remix, Svelte, and GitHub Actions.

Tools like this can easily report false positives for files referenced from config files or CLI conventions. Having those ecosystem rules built in is valuable.

Start with this

The setup is light.

npm init @knip/config
npm run knip

If you only want to try it without adding it to the project:

npx knip

Knip v6 requires Node.js v20.19.0 or newer, or Bun.
If the project is on an older Node.js version, check that first.

If the output is overwhelming, limit the number of shown issues:

npm run knip -- --max-show-issues 5

Trying to fix everything at once is tiring.
Start with the obvious wins.

What to check before deleting

Knip is powerful, but deleting everything from the report without review is risky.

I would start in this order:

  1. Delete clearly old files
  2. Remove unused dependencies
  3. Remove unused exports
  4. Run --production to look at dead code from a production-code perspective
  5. Decide whether it belongs in CI

Exports need extra care.
If an export is internal to an application, it is usually easy to remove. But in a library or public API, "unused in this repository" does not always mean "unused by anyone."

Starting with a closed application or internal package is the safer path.

Why this matters

Reducing unused code is not just about having fewer lines.

  • Search results get cleaner
  • Refactors involve fewer "do I need to touch this too?" moments
  • Dependency upgrades and security triage get smaller
  • Tests and stories left behind without real product usage become easier to spot
  • AI coding agents are less likely to imitate old, unused patterns

That last point matters more than I expected.
The more you ask AI to work in a repository, the more old implementations and unused patterns become noise.

For humans and AI agents alike, unused code is information that can be read but not trusted.
Knip gives you a practical way to surface and remove that noise.

Summary

Knip is excellent.

I learned about it late, but I want more JavaScript and TypeScript projects to use it.
If unused files, stale dependencies, or dead exports are bothering you, running npx knip once is worth it.

Delete carefully.
But make the unused code visible now.

References