Unused exports
Finding unused exports in your JavaScript and TypeScript projects is one of Knip’s core features. And it comes with even more features to identify and remove clutter to keep your projects in great shape.
Why are unused exports a problem?
Having unused exports in your codebase is problematic for several reasons:
- They increase bundle sizes if not properly eliminated by tree-shaking.
- They clutter the codebase and make it harder to navigate and understand.
- They mislead developers into thinking certain code is used when it’s not.
- They make refactoring and maintaining the codebase more difficult.
- They slow down tooling that analyze the codebase, such as bundlers, linters and type checkers.
- They may represent dead code that is no longer needed but hasn’t been cleaned up.
How do I find unused exports?
Knip is a powerful tool that can help you find and remove unused exports in your JavaScript and TypeScript projects. It analyzes the codebase, identifies exports that are not imported anywhere, and reports them.
Get started and install Knip to run it on your project. Knip will scan your files and provide a detailed report of unused exports, and much more.
How does Knip identify unused exports?
Knip performs both static and dynamic analysis to determine which exports are actually being used in your codebase. It looks at import statements, export usage, and a lot more code patterns to identify unused exports.
Knip supports JavaScript and TypeScript projects, and handles both CommonJS and ES Modules syntax.
Can Knip remove unused exports?
Yes, Knip not only finds unused exports but can also remove them for you. Run
Knip with the --fix
flag to enable the auto-fix feature, and it will
modify your source code and remove the unused exports.
It’s always recommended to review the changes made by Knip before committing them to ensure no unintended modifications were made.
Can Knip handle large codebases?
Absolutely. Knip supports monorepos with workspaces and utilizes workspace sharing to efficiently analyze large monorepos. This makes it easier and more fun to manage and optimize large multi-package projects.
Does Knip work with my favorite editor or IDE?
Knip is a command-line tool that runs independently of your editor or IDE. However, if you run Knip inside an integrated IDE terminal, the report contains file names and positions in a format IDEs like VS Code and WebStorm understand to easily navigate around.
How is Knip different from ESLint for finding unused exports?
While linters like ESLint can find unused variables and imports within individual files, Knip analyzes the entire project to determine which exports are actually unused. By building a comprehensive module graph, Knip identifies exports that are not imported or used anywhere in the codebase. This allows Knip to catch unused exports and dead code that ESLint and other linters would miss.
Also see Why isn’t Knip an ESLint plugin?
ISC License © 2024 Lars Kappert