Install and configure Knip for dead code detection. Use before running dead-code-hunter or dependency-auditor to ensure Knip is available. Handles installation, configuration creation, and validation.
60
76%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
Passed
No findings from the security scan
Fix and improve this skill with Tessl
tessl review fix ./.claude/skills/setup-knip/SKILL.mdInstall and configure Knip - the tool for finding unused files, dependencies, and exports in JavaScript/TypeScript projects.
Check package.json for knip in devDependencies.
Tools Used: Read
# Read package.json and check for knipCheck Logic:
devDependencies.knip exists → Knip is installedIf Knip is not installed, add it as devDependency.
Tools Used: Bash
# Detect package manager
if [ -f "pnpm-lock.yaml" ]; then
pnpm add -D knip
elif [ -f "yarn.lock" ]; then
yarn add -D knip
elif [ -f "bun.lockb" ]; then
bun add -D knip
else
npm install -D knip
fiExpected Output:
+ knip@5.x.xLook for existing Knip configuration files.
Tools Used: Glob
Configuration Files (in priority order):
knip.jsonknip.jsoncknip.tsknip.config.tsknip.config.jspackage.json (knip field)If no configuration found, create knip.json with sensible defaults.
Tools Used: Write
Default Configuration for Standard Project:
{
"$schema": "https://unpkg.com/knip@5/schema.json",
"entry": ["src/index.{ts,tsx,js,jsx}", "src/main.{ts,tsx,js,jsx}"],
"project": ["src/**/*.{ts,tsx,js,jsx}"],
"ignore": [
"**/*.d.ts",
"**/*.test.{ts,tsx}",
"**/*.spec.{ts,tsx}",
"**/test/**",
"**/tests/**",
"**/__tests__/**",
"**/node_modules/**"
],
"ignoreDependencies": [
"@types/*"
]
}Default Configuration for Next.js Project (detected by next in dependencies):
{
"$schema": "https://unpkg.com/knip@5/schema.json",
"entry": [
"src/app/**/*.{ts,tsx}",
"src/pages/**/*.{ts,tsx}",
"app/**/*.{ts,tsx}",
"pages/**/*.{ts,tsx}"
],
"project": ["src/**/*.{ts,tsx}", "app/**/*.{ts,tsx}", "pages/**/*.{ts,tsx}"],
"ignore": [
"**/*.d.ts",
"**/*.test.{ts,tsx}",
"**/*.spec.{ts,tsx}",
"**/node_modules/**"
],
"next": {
"entry": [
"next.config.{js,ts,mjs}",
"middleware.{js,ts}"
]
}
}Default Configuration for Monorepo (detected by workspaces in package.json):
{
"$schema": "https://unpkg.com/knip@5/schema.json",
"workspaces": {
"packages/*": {
"entry": ["src/index.{ts,tsx,js,jsx}"],
"project": ["src/**/*.{ts,tsx,js,jsx}"]
},
"apps/*": {
"entry": ["src/index.{ts,tsx,js,jsx}", "src/main.{ts,tsx,js,jsx}"],
"project": ["src/**/*.{ts,tsx,js,jsx}"]
}
},
"ignore": [
"**/*.d.ts",
"**/*.test.{ts,tsx}",
"**/*.spec.{ts,tsx}",
"**/node_modules/**"
]
}Check if package.json has knip scripts, add if missing.
Tools Used: Read, Bash
Scripts to Add:
{
"scripts": {
"knip": "knip",
"knip:fix": "knip --fix",
"knip:deps": "knip --dependencies",
"knip:exports": "knip --exports",
"knip:files": "knip --files"
}
}Add via npm pkg:
npm pkg set scripts.knip="knip"
npm pkg set scripts.knip:fix="knip --fix"
npm pkg set scripts.knip:deps="knip --dependencies"
npm pkg set scripts.knip:exports="knip --exports"
npm pkg set scripts.knip:files="knip --files"Run Knip to verify installation works.
Tools Used: Bash
npx knip --helpExpected: Help output displayed without errors
Return structured result indicating setup status.
Expected Output:
{
"installed": true,
"version": "5.x.x",
"config_file": "knip.json",
"config_created": true,
"scripts_added": ["knip", "knip:fix", "knip:deps", "knip:exports", "knip:files"],
"project_type": "nextjs|monorepo|standard",
"ready": true
}Initial State:
Actions:
Output:
{
"installed": true,
"version": "5.73.3",
"config_file": "knip.json",
"config_created": true,
"scripts_added": ["knip", "knip:fix", "knip:deps", "knip:exports", "knip:files"],
"project_type": "standard",
"ready": true
}Initial State:
Actions:
Output:
{
"installed": true,
"version": "5.73.3",
"config_file": "knip.json",
"config_created": false,
"scripts_added": ["knip:deps"],
"project_type": "nextjs",
"ready": true
}Initial State:
Actions:
Output:
{
"installed": true,
"version": "5.73.3",
"config_file": "knip.json",
"config_created": true,
"scripts_added": ["knip", "knip:fix", "knip:deps", "knip:exports", "knip:files"],
"project_type": "monorepo",
"ready": true
}For agents using Knip after setup:
| Command | Purpose | Use Case |
|---|---|---|
npx knip | Full analysis | Complete dead code scan |
npx knip --dependencies | Dependencies only | dependency-auditor |
npx knip --exports | Exports only | Unused export detection |
npx knip --files | Files only | Unused file detection |
npx knip --fix --fix-type exports,types | Auto-fix exports/types | Safe automated cleanup |
npx knip --fix --fix-type dependencies | Auto-fix deps | Remove from package.json |
npx knip --reporter json | JSON output | Machine parsing |
npx knip --reporter compact | Compact output | Quick review |
--allow-remove-filesnpx knip --fix --allow-remove-files is FORBIDDEN!
Knip has a critical limitation: it cannot detect dynamic imports.
// Knip CANNOT see these relationships:
const module = await import(`./plugins/${name}.ts`);
const Component = lazy(() => import('./components/Dashboard'));
require(`./locales/${lang}.json`);Files loaded via dynamic imports will appear "unused" to Knip but are actually critical!
ALLOWED:
npx knip --fix --fix-type exports - Safe: removes unused exports from filesnpx knip --fix --fix-type types - Safe: removes unused type exportsnpx knip --fix --fix-type dependencies - Safe: removes from package.json onlyFORBIDDEN:
npx knip --fix --allow-remove-files - DANGEROUS: may delete files with dynamic importsnpx knip --fix (without --fix-type) - May include file removalBefore removing ANY file flagged by Knip:
import(, require(, lazy(, loadable(## Phase 0: Pre-Flight
1. Use setup-knip Skill to ensure Knip is available
2. If result.ready === false, halt with setup instructions
3. If result.ready === true, proceed with detection## Phase 1: Environment Analysis
1. Use setup-knip Skill before dependency analysis
2. Knip will detect unused dependencies more accurately than manual grep--reporter json for machine-readable output--fix flag can auto-remove unused exports and dependencies8c3b576
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.