home / skills / pproenca / dot-skills / knip-deadcode

knip-deadcode skill

/skills/.experimental/knip-deadcode

This skill helps you optimize JavaScript and TypeScript codebase by applying Knip dead code best practices for configuration, analysis, and cleanup.

npx playbooks add skill pproenca/dot-skills --skill knip-deadcode

Review the files below or copy the command above to add this skill to your agents.

Files (55)
SKILL.md
7.8 KB
---
name: knip-deadcode
description: Knip dead code detection best practices for JavaScript and TypeScript projects. Use when configuring Knip, analyzing unused code, setting up CI integration, or cleaning up codebases. Triggers on knip.json, dead code, unused exports, unused dependencies, bundle optimization.
---

# Community Knip Dead Code Detection Best Practices

Comprehensive guide for detecting and removing dead code in JavaScript and TypeScript projects using Knip. Contains 43 rules across 8 categories, prioritized by impact to guide configuration, CI integration, and cleanup workflows.

## When to Apply

Reference these guidelines when:
- Configuring Knip for a new project or monorepo
- Investigating false positives or false negatives
- Setting up CI pipelines to prevent dead code regressions
- Using auto-fix to clean up unused code
- Optimizing Knip performance for large codebases

## Rule Categories by Priority

| Priority | Category | Impact | Prefix |
|----------|----------|--------|--------|
| 1 | Configuration Foundations | CRITICAL | `config-` |
| 2 | Entry Point Strategy | CRITICAL | `entry-` |
| 3 | Workspace & Monorepo | HIGH | `workspace-` |
| 4 | Dependency Analysis | HIGH | `deps-` |
| 5 | Export Detection | MEDIUM-HIGH | `exports-` |
| 6 | CI Integration | MEDIUM | `ci-` |
| 7 | Auto-Fix Workflow | MEDIUM | `fix-` |
| 8 | Performance Optimization | LOW-MEDIUM | `perf-` |

## Quick Reference

### 1. Configuration Foundations (CRITICAL)

- [`config-avoid-broad-ignore`](references/config-avoid-broad-ignore.md) - Avoid broad ignore patterns
- [`config-configure-path-aliases`](references/config-configure-path-aliases.md) - Configure path aliases in Knip
- [`config-enable-plugins-explicitly`](references/config-enable-plugins-explicitly.md) - Enable framework plugins explicitly
- [`config-run-without-config`](references/config-run-without-config.md) - Run without config first for baseline
- [`config-separate-entry-project`](references/config-separate-entry-project.md) - Separate entry files from project files
- [`config-use-json-schema`](references/config-use-json-schema.md) - Use JSON schema for configuration validation
- [`config-use-negation-patterns`](references/config-use-negation-patterns.md) - Use negation patterns for exclusions
- [`config-use-production-mode`](references/config-use-production-mode.md) - Use production mode for shipping code analysis

### 2. Entry Point Strategy (CRITICAL)

- [`entry-add-dynamic-imports`](references/entry-add-dynamic-imports.md) - Add dynamic import targets as entry points
- [`entry-exclude-test-files`](references/entry-exclude-test-files.md) - Exclude test files from production entries
- [`entry-include-all-entry-points`](references/entry-include-all-entry-points.md) - Include all application entry points
- [`entry-include-bin-scripts`](references/entry-include-bin-scripts.md) - Include binary scripts as entry points
- [`entry-use-compilers`](references/entry-use-compilers.md) - Use compilers for non-standard file types
- [`entry-use-plugin-entries`](references/entry-use-plugin-entries.md) - Use plugin entry points for frameworks
- [`entry-verify-with-debug`](references/entry-verify-with-debug.md) - Verify entry points with debug mode

### 3. Workspace & Monorepo (HIGH)

- [`workspace-configure-root-workspace`](references/workspace-configure-root-workspace.md) - Configure root workspace explicitly
- [`workspace-ignore-specific`](references/workspace-ignore-specific.md) - Ignore specific workspaces when needed
- [`workspace-isolate-for-strict`](references/workspace-isolate-for-strict.md) - Isolate workspaces for strict dependency checking
- [`workspace-list-cross-deps`](references/workspace-list-cross-deps.md) - List cross-workspace dependencies explicitly
- [`workspace-per-workspace-plugins`](references/workspace-per-workspace-plugins.md) - Configure plugins per workspace
- [`workspace-use-workspace-globs`](references/workspace-use-workspace-globs.md) - Use workspace globs for consistent configuration

### 4. Dependency Analysis (HIGH)

- [`deps-add-unlisted-deps`](references/deps-add-unlisted-deps.md) - Add unlisted dependencies to package.json
- [`deps-avoid-transitive-reliance`](references/deps-avoid-transitive-reliance.md) - Avoid relying on transitive dependencies
- [`deps-configure-plugin-deps`](references/deps-configure-plugin-deps.md) - Configure plugins for tool-specific dependencies
- [`deps-fix-files-first`](references/deps-fix-files-first.md) - Fix unused files before dependencies
- [`deps-ignore-conditional-deps`](references/deps-ignore-conditional-deps.md) - Ignore conditionally loaded dependencies
- [`deps-remove-obsolete-types`](references/deps-remove-obsolete-types.md) - Remove obsolete type definition packages

### 5. Export Detection (MEDIUM-HIGH)

- [`exports-check-class-members`](references/exports-check-class-members.md) - Check class members for unused code
- [`exports-enable-entry-exports`](references/exports-enable-entry-exports.md) - Enable entry export checking for private packages
- [`exports-handle-reexports`](references/exports-handle-reexports.md) - Handle re-exports in barrel files
- [`exports-ignore-same-file`](references/exports-ignore-same-file.md) - Ignore exports used in same file
- [`exports-tag-public-api`](references/exports-tag-public-api.md) - Tag public API exports with JSDoc
- [`exports-trace-usage`](references/exports-trace-usage.md) - Trace export usage before removal
- [`exports-use-include-libs`](references/exports-use-include-libs.md) - Use include libs for type-based consumption

### 6. CI Integration (MEDIUM)

- [`ci-add-to-pipeline`](references/ci-add-to-pipeline.md) - Add Knip to CI pipeline
- [`ci-separate-production-check`](references/ci-separate-production-check.md) - Separate production and default mode checks
- [`ci-use-cache`](references/ci-use-cache.md) - Enable cache for faster CI runs
- [`ci-use-max-issues`](references/ci-use-max-issues.md) - Use max issues for gradual adoption
- [`ci-use-reporters`](references/ci-use-reporters.md) - Use appropriate reporters for CI output
- [`ci-watch-mode-local`](references/ci-watch-mode-local.md) - Use watch mode for local development

### 7. Auto-Fix Workflow (MEDIUM)

- [`fix-allow-remove-files`](references/fix-allow-remove-files.md) - Explicitly allow file removal
- [`fix-format-after-fix`](references/fix-format-after-fix.md) - Format code after auto-fix
- [`fix-review-before-commit`](references/fix-review-before-commit.md) - Review auto-fix changes before commit
- [`fix-update-deps-after`](references/fix-update-deps-after.md) - Update package manager after dependency fix
- [`fix-use-fix-type`](references/fix-use-fix-type.md) - Use fix type for targeted cleanup

### 8. Performance Optimization (LOW-MEDIUM)

- [`perf-filter-issue-types`](references/perf-filter-issue-types.md) - Filter issue types for focused analysis
- [`perf-limit-output`](references/perf-limit-output.md) - Limit output for large codebases
- [`perf-profile-performance`](references/perf-profile-performance.md) - Profile performance for slow analysis
- [`perf-use-bun-runtime`](references/perf-use-bun-runtime.md) - Use Bun runtime for faster analysis
- [`perf-use-cache-flag`](references/perf-use-cache-flag.md) - Enable cache for repeated analysis
- [`perf-use-workspace-filter`](references/perf-use-workspace-filter.md) - Filter workspaces for faster monorepo analysis

## How to Use

Read individual reference files for detailed explanations and code examples:

- [Section definitions](references/_sections.md) - Category structure and impact levels
- [Rule template](assets/templates/_template.md) - Template for adding new rules

## Reference Files

| File | Description |
|------|-------------|
| [references/_sections.md](references/_sections.md) | Category definitions and ordering |
| [assets/templates/_template.md](assets/templates/_template.md) | Template for new rules |
| [metadata.json](metadata.json) | Version and reference information |

Overview

This skill documents Knip dead code detection best practices for JavaScript and TypeScript projects. I provide prioritized guidance across configuration, entry strategy, workspace handling, dependency analysis, export detection, CI integration, auto-fix workflows, and performance. The goal is to make Knip reliable and actionable for both single-repo and monorepo setups.

How this skill works

The skill inspects common Knip configuration pitfalls and testing patterns, recommending concrete rules and patterns to reduce false positives and false negatives. It groups 43 rules into eight priority categories and describes practical steps for entry points, dependency handling, export tracing, CI gating, and safe auto-fix. Each rule points to a focused reference with examples and expected outcomes.

When to use it

  • When configuring Knip for a new project or adopting it in a monorepo
  • When investigating suspected false positives or false negatives from Knip reports
  • When adding Knip to CI to prevent dead code regressions
  • When running automated fixes to remove unused code and update deps
  • When optimizing Knip performance for large codebases or frequent runs

Best practices

  • Start by running Knip without a config to establish a baseline before adding ignores
  • Explicitly configure path aliases, framework plugins, and compiler entries to avoid missed references
  • Include all real entry points (dynamic imports, bins, plugin entries) and exclude test-only files
  • Treat workspace boundaries explicitly in monorepos: isolate strict checks and list cross-workspace deps
  • Use CI with separate production checks, caching, and gradual max-issues thresholds for adoption
  • Apply auto-fix carefully: allow file removals explicitly, format after changes, and review before commit

Example use cases

  • Configure knip.json for a TypeScript web app to pick up dynamic imports and eliminate unused exports
  • Set up Knip in a monorepo to verify cross-workspace dependencies and avoid relying on transitive packages
  • Add Knip as a CI step with cache and reporter settings to block new dead code from being merged
  • Run a targeted cleanup workflow that auto-removes unused files, updates package.json, and runs formatting
  • Profile and filter analysis on a large repo to reduce runtime and focus on high-impact issue types

FAQ

How do I avoid false positives for re-exported symbols?

Enable rules that handle re-exports and include barrel files in export tracing; tag public API exports to exclude intentional public surfaces.

Can Knip run safely in CI without breaking builds for in-progress cleanup?

Yes—use separate production-mode checks, enable cache, and start with a max-issues threshold to adopt Knip gradually before enforcing zero tolerance.