Skip to content

Knip

Knip finds unused files, dependencies, and exports in JavaScript and TypeScript projects. Dead code accumulates silently. You add a utility function, stop using it, and forget to delete it. Dependencies linger in package.json after their last import disappears. Knip catches these.

What Knip Finds

Knip scans your codebase starting from entry points and reports unused files, dependencies, exports, and devDependencies. It also catches duplicate exports (the same thing under multiple names).

The "unused export" detection works recursively. If function A calls function B, and nothing calls A, both get flagged.

Usage

# Install
npm install -D knip typescript @types/node

# Or use the setup wizard
npm init @knip/config

# Run
npx knip

Add a script to package.json:

{
  "scripts": {
    "knip": "knip"
  }
}

Then run with npm run knip.

Configuration

Knip auto-detects frameworks and tools through plugins. Most projects need zero configuration.

For custom entry points or ignored paths, create knip.json:

{
  "entry": ["src/index.ts", "src/cli.ts"],
  "project": ["src/**/*.ts"],
  "ignore": ["src/generated/**"]
}

Output Formats

# Default human-readable output
npx knip

# JSON for programmatic parsing
npx knip --reporter json

# Limit issues shown (useful for large codebases)
npx knip --max-show-issues 10

Dead Code Confusion

Dead code confuses agents. When an agent searches for how to implement something, unused code appears alongside active code. The agent might copy patterns from deprecated utilities or call functions that nothing else uses.

Running Knip before or after agent sessions keeps the codebase clean:

  • Before: the agent sees live code when exploring
  • After: catches any dead code the agent introduced

Unlike lint-on-every-edit tools, Knip runs at session boundaries rather than per-file. A typical Knip run takes a few seconds for medium projects.

CI Integration

Add Knip to your CI pipeline to prevent dead code from accumulating:

# .github/workflows/ci.yml
- name: Check for unused code
  run: npm run knip

Knip exits with code 1 when it finds issues, failing the build.

External Resources