Production Mode
Introduction
The default mode for Knip is comprehensive and targets all project code, including configuration files, test files, Storybook stories, and so on. Test files usually import production files. This prevents production files or their exports from being reported as unused, while sometimes both of them can be deleted. Knip features a “production mode” to focus only on the code that you ship.
Configuration
To tell Knip what is production code, add an exclamation mark behind each
pattern!
that represents production code:
Depending on file structure and enabled plugins, it’s possible that you don’t need to modify your configuration at all.
Then run Knip with the --production
flag:
Here’s what’s included in production mode:
- Only
entry
andproject
patterns suffixed with!
- Only production
entry
file patterns exported by plugins (such as Next.js and Remix) - Only the
start
andpostinstall
scripts - Ignore exports with the
@internal
tag
Strict Mode
Additionally, the --strict
flag can be added to:
- Consider
dependencies
(notdevDependencies
) when finding unused or unlisted dependencies - Include
peerDependencies
when finding unused or unlisted dependencies - Verify isolation: workspaces should use strictly their own
dependencies
- Type-only imports should be in
devDependencies
Types
Add --exclude types
if you don’t want to include types in the report:
ISC License © 2024 Lars Kappert