What Dead Code is Detected
Knip analyzes your project to find:Unused Files
Files that aren’t imported anywhere in your project
Unused Exports
Exported functions, components, or variables that are never imported
Unused Types
TypeScript types and interfaces that aren’t used
Duplicate Exports
The same symbol exported multiple times
Rules
knip/files
Severity: warning Detects files that aren’t imported by any other file in the project. Example output:- Leftover files from refactoring
- Old implementations that were replaced
- Test files in wrong location
- Files that should be deleted
knip/exports
Severity: warning Detects exported values that are never imported elsewhere. Example output:- Function is only used internally → Make it private
- Old API no longer used → Delete it
- Over-exported helpers → Remove export
- Public API for library consumers
- Explicitly exported for future use
- Required by build tools or frameworks
knip/types
Severity: warning Detects TypeScript types and interfaces that aren’t referenced. Example output:- Legacy types from refactoring
- Over-engineered type definitions
- Types that were used by deleted code
knip/duplicates
Severity: warning Detects the same symbol exported multiple times in different files. Example output:- Confusing for developers
- Inconsistent behavior
- Harder to refactor
- May cause bundler issues
How Knip Works
Knip performs static analysis of your codebase:- Finds entry points (pages, API routes, root files)
- Follows imports from entry points
- Builds dependency graph of your entire project
- Identifies unreachable code not in the graph
- Reports findings as diagnostics
Entry Points
Knip automatically detects common entry points:- Next.js pages and app routes
- API routes
- Server actions
- Configuration files (next.config.js, etc.)
- Test files
- Build scripts in package.json
Configuration
Project-level Knip Config
You can customize Knip behavior with aknip.json file:
Ignoring Dead Code via Config
Ignore specific Knip rules inreact-doctor.json:
Ignoring Specific Files
When to Ignore Dead Code
Public APIs
If you’re building a library, many exports may be intentionally unused:Framework Requirements
Some frameworks require specific exports:Gradual Refactoring
When refactoring, you might want to keep old code temporarily:Generated Code
Generated code often has unused exports:Best Practices
Regular Cleanup
Run dead code detection regularly:Review Before Deleting
Not all “dead code” should be deleted:- Check if it’s part of public API
- Verify it’s not used by external consumers
- Confirm it’s not required by framework
- Ask team if anyone is working on using it
Start Small
Don’t try to fix everything at once:- Start with unused files (safest to delete)
- Move to unused exports
- Finally tackle unused types
- Fix duplicates as you encounter them
Document Intentional Unused Code
Monorepo Support
Knip automatically handles monorepos:- Detect monorepo structure
- Run Knip at workspace level
- Analyze cross-package imports
- Report dead code per package
Performance Tips
Exclude Large Directories
Incremental Analysis
Knip analysis can be slow on large codebases. Consider:- Running only on CI, not locally
- Excluding dead code checks from
watchmode - Using file-based ignores for generated code
False Positives
Knip may report false positives for:Dynamic Imports
Build-time Code Generation
External Consumers
If your package is consumed externally, exports will appear unused. Solution: Maintain a list of public API exports and exclude from dead code detection.Related
- Configuration Guide - How to configure rule ignores
- CLI Commands - Command line options for diagnostics
- Knip Documentation - Full Knip documentation