Auto-fix
Run Knip as you normally would, and if the report looks good then run it again
with the --fix
flag to let Knip automatically apply fixes. It fixes the
following issue types:
- Remove
export
keyword for unused exports and exported types - Remove
export default
keywords for unused default exports - Remove exports, re-exports and exported types
- Remove unused enum members
- Remove unused class members (disabled by default)
- Remove unused
dependencies
anddevDependencies
frompackage.json
- Remove unused files
Flags
Add the --fix
flag to remove unused exports and dependencies:
Add --allow-remove-files
to allow Knip to remove unused files:
Use --fix-type
to fix only specific issue types:
files
exports
types
dependencies
Example:
Demo
Post-fix
After Knip has fixed issues, there are four things to consider:
1. Use a formatter
Use a tool like Prettier or Biome if the code needs formatting. Knip removes the minimum amount of code while leaving it in a working state.
2. Unused variables
Use a tool like ESLint or Biome to find and remove unused variables inside files. This may result in more deleted code, and Knip may then find more unused code. Rinse and repeat!
3. Unused dependencies
Verify changes in package.json
and update dependencies using your package
manager.
4. Install unlisted dependencies
If Knip reports unlisted dependencies or binaries, they should be installed.
Example results
Exports
The export
keyword for unused exports is removed:
The default
keyword was also removed here.
Knip removes the whole or part of export declarations:
Re-exports
Knip removes the whole or part of re-exports:
Also across any chain of re-exports:
Export assignments
Knip removes individual exported items in “export assignments”, but does not remove the entire export declaration if it’s empty:
Reason: the right-hand side of the assignment might have side-effects. It’s not safe to always remove the whole declaration. This could be improved in the future (feel free to open an issue/RFC).
Enum members
Unused members of enums are removed:
CommonJS
Knip supports CommonJS and removes unused exports:
Warning: the right-hand side of such an assignment might have side-effects. Knip currently removes the whole declaration (feel free to open an issue/RFC).
Dependencies
Unused dependencies are removed from package.json
:
experimental
Class membersUnused members of classes can be removed:
Currently Knip might be too eager removing class members when they’re not
referenced internally but meant to be called by an external library. For
instance, Knip might think componentDidMount
and render
in React class
component are unused and will remove those.
Note that classMembers
aren’t included by default.
What’s not included
Operations that auto-fix does not (yet) perform and why:
- Add unlisted (dev) dependencies to
package.json
(should it go intodependencies
ordevDependencies
? For monorepos in current workspace or root?) - Add unlisted binaries (which package and package version contains the used binary?)
- Fix duplicate exports (which one should removed?)
ISC License © 2024 Lars Kappert