How to find dead code in a JavaScript or TypeScript project
Find unused files, exports and dependencies with knip and TypeScript, confirm with coverage and logs, and remove dead code without breaking dynamic callers.
Dead code (functions nobody calls, files nothing imports, dependencies nothing uses) makes a codebase harder to read. Every time you trace a path, you have to check whether the branch you're looking at can actually run. Removing it is one of the cheapest ways to make a codebase easier to understand, as long as you're sure it's dead.
Kinds of dead code
- Unused files that nothing imports
- Unused exports in files that are otherwise used
- Unused dependencies in
package.json - Unreachable branches: conditions that can never be true, feature flags that are always on
- Unused routes, jobs or handlers that nothing calls in practice
Static tools
knip is the most complete tool for JavaScript and TypeScript projects. It finds unused files, exports, dependencies and more, and understands many frameworks' conventions through plugins.
npx knipTypeScript itself can flag unused locals and parameters with noUnusedLocals and noUnusedParameters. ESLint's no-unused-vars does the same in JavaScript.
Runtime evidence
Static analysis can't see everything. Two runtime checks help:
- Coverage. Run your test suite (or, better, the app under realistic use) with coverage. Code that never runs is a candidate, not proof.
- Production logs. For a route or job you suspect is unused, add a log line or metric and watch it for a few weeks.
The dangerous cases
Some code looks dead but isn't:
- Framework conventions. Files like
page.tsx,route.tsor+server.tsaren't imported; the framework loads them. Good tools know this; manual searches don't. - Dynamic imports and lookups.
import("./handlers/" + name)or a registry keyed by string. - Public APIs. A library's exports may be used by projects you can't see.
- External callers. Webhooks, cron jobs, other services and old mobile app versions calling an endpoint.
- Scripts. One-off scripts referenced only from docs or runbooks.
Remove it safely
- Confirm with at least two signals (static tool plus coverage, or static tool plus logs).
- Remove in small commits, one area at a time.
- Run the full test suite and build.
- For endpoints, deprecate first (log or warn) before deleting.
- Write the reason in the commit message, so
git logexplains it later.
Ask before deleting
Ask RepoGrasp "is legacyPriceAdjust called anywhere, including through dynamic imports or string lookups?" It reads every source file and cites any references it finds. A "no references" answer is useful evidence, but combine it with knip and a code search before deleting.