| name | knip-cleanup |
| description | This skill should be used when the user asks to "find unused code", "clean up dead code", "find unused dependencies", "find unused exports", "find unused files", "remove dead code", "run knip", "check for unused imports", "clean up dependencies", "prune unused packages", or mentions "knip", "dead code detection", "unused exports", "unused dependencies", "unused files", "dependency cleanup", "export cleanup". Provides expert guidance on using knip for JavaScript/TypeScript project cleanup including configuration, analysis, auto-fixing, and safe removal workflows. |
| license | MIT |
| metadata | {"author":"Chris Kelley (hello@iwritecode.io)","version":"1.0.0"} |
Knip — Dead Code & Dependency Cleanup
Knip finds unused files, dependencies, exports, types, and duplicate packages in JavaScript/TypeScript projects. Use this skill to analyze, report, and safely clean up projects.
What Knip Detects
| Category | Description | Risk Level |
|---|
| Unused files | Source files not imported anywhere | Medium |
| Unused dependencies | Packages in dependencies/devDependencies not imported | High — safe to remove |
| Unlisted dependencies | Packages imported but not in package.json | Critical — must add |
| Unused exports | Named exports not consumed by other modules | Low — may be public API |
| Unused types | TypeScript types/interfaces not referenced | Low |
| Duplicate packages | Same package at multiple versions in lockfile | Medium |
| Unused binaries | Binaries declared but not used in scripts | Low |
| Unresolved imports | Imports that point to missing modules | Critical |
Installation
pnpm add -D knip
pnpm dlx knip
Configuration
Knip works zero-config for many projects but supports knip.json, knip.jsonc, .knip.json, knip.ts, or a knip key in package.json.
Basic Config (knip.json)
{
"$schema": "https://unpkg.com/knip@latest/schema.json",
"entry": ["src/index.ts", "src/main.ts"],
"project": ["src/**/*.ts"],
"ignore": ["**/*.test.ts", "**/*.spec.ts"],
"ignoreDependencies": ["@types/node"]
}
Monorepo Config
{
"$schema": "https://unpkg.com/knip@latest/schema.json",
"workspaces": {
"packages/*": {
"entry": ["src/index.ts"],
"project": ["src/**/*.ts"]
},
"apps/web": {
"entry": ["src/main.tsx", "next.config.{js,ts}"],
"project": ["src/**/*.{ts,tsx}"]
}
}
}
Common Framework Configurations
Next.js:
{
"entry": [
"src/app/**/page.tsx",
"src/app/**/layout.tsx",
"src/app/**/route.ts",
"src/app/**/loading.tsx",
"src/app/**/error.tsx",
"src/app/**/not-found.tsx",
"src/middleware.ts",
"next.config.{js,ts,mjs}"
],
"project": ["src/**/*.{ts,tsx}"]
}
Vite/React:
{
"entry": ["src/main.tsx", "vite.config.ts"],
"project": ["src/**/*.{ts,tsx}"]
}
Express/Node API:
{
"entry": ["src/index.ts", "src/server.ts"],
"project": ["src/**/*.ts"],
"ignoreDependencies": ["@types/express"]
}
Built-in Plugin Support
Knip has built-in plugins for common tools and frameworks. These automatically detect entry points so you don't need to configure them manually:
- Frameworks: Next.js, Remix, Astro, Nuxt, Svelte, Angular, Gatsby
- Build tools: Vite, Webpack, Rollup, esbuild, tsup, Turbopack
- Testing: Jest, Vitest, Mocha, Playwright, Cypress, Storybook
- Linting: ESLint, Prettier, Stylelint, lint-staged, commitlint
- Bundlers/Config: TypeScript, Babel, PostCSS, Tailwind CSS
- CI/CD: GitHub Actions, Husky, Lefthook
- Other: Prisma, Drizzle, GraphQL Codegen, Sentry, MSW
When a plugin detects its config file, it automatically adds the right entry points. Override or extend with explicit config only when needed.
Running Knip
Basic Commands
pnpm knip
pnpm knip --reporter compact
pnpm knip --reporter json
pnpm knip --reporter markdown
pnpm knip --include files
pnpm knip --include dependencies
pnpm knip --include exports
pnpm knip --include types
pnpm knip --include duplicates
pnpm knip --include unlisted
pnpm knip --exclude exports
pnpm knip --production
pnpm knip --strict
pnpm knip --debug
pnpm knip --performance
Auto-Fix Mode
pnpm knip --fix
pnpm knip --fix --dry-run
pnpm knip --fix --include exports
pnpm knip --fix --include dependencies
pnpm knip --fix --allow-remove-files
Important --fix behavior:
- Removes unused
export keywords from declarations
- Removes unused dependency entries from
package.json
- Does NOT delete files unless
--allow-remove-files is passed
- Does NOT run your package manager's install after removing deps — you must run
pnpm install after
- Always review changes with
git diff after running --fix
Safe Removal Workflow
Follow this order when cleaning up knip findings to avoid cascading false positives:
Step 1: Fix Unlisted Dependencies First
pnpm knip --include unlisted
pnpm add <missing-package>
Step 2: Remove Unused Dependencies
pnpm knip --include dependencies --fix
pnpm install
Step 3: Remove Unused Exports
pnpm knip --include exports --fix --dry-run
pnpm knip --include exports --fix
Step 4: Remove Unused Files
pnpm knip --include files
Step 5: Verify Nothing Broke
pnpm tsc --noEmit
pnpm test
pnpm build
pnpm knip
Handling False Positives
Knip may flag things as unused that are actually needed:
Dynamic imports / runtime usage
{
"ignoreDependencies": ["sharp", "pg-native"]
}
Exports consumed by external packages (library authors)
{
"entry": ["src/index.ts!"],
"ignore": ["src/internal/**"]
}
The ! suffix means "include all exports from this entry as used."
Framework magic (decorators, auto-imports, etc.)
Use the appropriate plugin or add explicit entry points.
Per-file export ignore
export function myPublicAPI() {}
Or use the @knip tag in comments:
export const API_VERSION = "2.0";
Ignore patterns
{
"ignore": [
"**/*.d.ts",
"**/generated/**",
"scripts/**"
],
"ignoreDependencies": [
"@types/*",
"dotenv"
]
}
Interpreting Results
When presenting knip results:
- Group by severity — unlisted deps and unresolved imports are critical; unused types are informational
- Check before removing exports — they may be part of a public API consumed by other packages
- Cross-reference unused deps — check if they're used in scripts, CI configs, or Dockerfiles that knip doesn't scan
- Unused files — verify they aren't dynamically imported, used as templates, or referenced in config files
- Duplicate packages — these bloat bundles; resolve by aligning versions in
package.json or using pnpm dedupe
Integration with Quality Gate
Knip is included in the quality gate as a WARN-level check. For standalone deep analysis, use the /knip command instead, which provides more granular control and guided cleanup workflows.