knip-dead-code
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseKnip Dead Code Detection
Knip 死代码检测
Knip is a comprehensive tool for finding unused code, dependencies, and exports in JavaScript and TypeScript projects. It helps maintain clean codebases and catch dead code before it accumulates.
Knip 是一款全面的工具,可用于在 JavaScript 和 TypeScript 项目中查找未使用的代码、依赖项及导出内容。它有助于维护整洁的代码库,并在死代码堆积前及时发现。
When to Use This Skill
适用场景
| Use this skill when... | Use another approach when... |
|---|---|
| Finding unused dependencies | Removing unused CSS (use PurgeCSS) |
| Detecting unused exports in libraries | Finding runtime dead code paths |
| Cleaning up codebases after refactors | Optimizing bundle size (use bundler tree-shaking) |
| Enforcing dependency hygiene in CI | Detecting duplicate dependencies (use npm-dedupe) |
| 适合使用该工具的场景... | 适合其他方案的场景... |
|---|---|
| 查找未使用的依赖项 | 移除未使用的CSS(使用PurgeCSS) |
| 检测库中的未使用导出内容 | 查找运行时死代码路径 |
| 重构后清理代码库 | 优化包体积(使用打包器的摇树优化) |
| 在CI中管控依赖项规范 | 检测重复依赖项(使用npm-dedupe) |
Core Expertise
核心功能
What is Knip?
- Unused detection: Files, dependencies, exports, types, enum members
- Plugin system: Supports 80+ frameworks and tools
- Fast: Analyzes large codebases in seconds
- Actionable: Clear reports with file locations
- CI-ready: Exit codes for failing builds
什么是Knip?
- 未使用内容检测:文件、依赖项、导出内容、类型、枚举成员
- 插件系统:支持80+框架与工具
- 快速分析:数秒内完成大型代码库的分析
- 结果清晰:附带文件位置的明确报告
- CI兼容:提供用于构建失败的退出码
Installation
安装
bash
undefinedbash
undefinedProject-local (recommended)
项目本地安装(推荐)
bun add --dev knip
bun add --dev knip
Verify installation
验证安装
bunx knip --version
undefinedbunx knip --version
undefinedBasic Usage
基础用法
bash
undefinedbash
undefinedRun Knip (scans entire project)
运行Knip(扫描整个项目)
bunx knip
bunx knip
Show only unused dependencies
仅显示未使用的依赖项
bunx knip --dependencies
bunx knip --dependencies
Show only unused exports
仅显示未使用的导出内容
bunx knip --exports
bunx knip --exports
Show only unused files
仅显示未使用的文件
bunx knip --files
bunx knip --files
Production mode (only check production dependencies)
生产模式(仅检查生产依赖项)
bunx knip --production
bunx knip --production
Exclude specific issue types
排除特定类型的问题
bunx knip --exclude-exports-used-in-file
bunx knip --exclude-exports-used-in-file
Output JSON (for CI)
输出JSON格式结果(用于CI)
bunx knip --reporter json
bunx knip --reporter json
Debug mode (show configuration)
调试模式(显示配置信息)
bunx knip --debug
undefinedbunx knip --debug
undefinedConfiguration
配置
Auto-detection (Zero Config)
自动检测(零配置)
Knip automatically detects:
- Entry points (package.json ,
main,exports)bin - Frameworks (Next.js, Vite, Remix, etc.)
- Test runners (Vitest, Jest, Playwright)
- Build tools (ESLint, TypeScript, PostCSS)
No configuration needed for standard projects.
Knip可自动检测:
- 入口文件(package.json中的、
main、exports)bin - 框架(Next.js、Vite、Remix等)
- 测试运行器(Vitest、Jest、Playwright)
- 构建工具(ESLint、TypeScript、PostCSS)
标准项目无需额外配置。
knip.json (Explicit Configuration)
knip.json(显式配置)
json
{
"$schema": "https://unpkg.com/knip@latest/schema.json",
"entry": ["src/index.ts", "src/cli.ts"],
"project": ["src/**/*.ts"],
"ignore": ["**/*.test.ts", "scripts/**"],
"ignoreDependencies": ["@types/*"],
"ignoreBinaries": ["npm-check-updates"]
}json
{
"$schema": "https://unpkg.com/knip@latest/schema.json",
"entry": ["src/index.ts", "src/cli.ts"],
"project": ["src/**/*.ts"],
"ignore": ["**/*.test.ts", "scripts/**"],
"ignoreDependencies": ["@types/*"],
"ignoreBinaries": ["npm-check-updates"]
}knip.ts (TypeScript Configuration)
knip.ts(TypeScript配置)
typescript
// knip.ts
import type { KnipConfig } from 'knip';
const config: KnipConfig = {
entry: ['src/index.ts', 'src/cli.ts'],
project: ['src/**/*.ts', 'scripts/**/*.ts'],
ignore: ['**/*.test.ts', '**/*.spec.ts', 'tmp/**'],
ignoreDependencies: [
'@types/*', // Type definitions
'typescript', // Always needed
],
ignoreExportsUsedInFile: true,
ignoreWorkspaces: ['packages/legacy/**'],
};
export default config;typescript
// knip.ts
import type { KnipConfig } from 'knip';
const config: KnipConfig = {
entry: ['src/index.ts', 'src/cli.ts'],
project: ['src/**/*.ts', 'scripts/**/*.ts'],
ignore: ['**/*.test.ts', '**/*.spec.ts', 'tmp/**'],
ignoreDependencies: [
'@types/*', // 类型定义
'typescript', // 始终需要
],
ignoreExportsUsedInFile: true,
ignoreWorkspaces: ['packages/legacy/**'],
};
export default config;Common Patterns
常见使用模式
Check Only Unused Dependencies
仅检查未使用的依赖项
bash
undefinedbash
undefinedFastest check - only dependencies
最快的检查方式 - 仅检查依赖项
bunx knip --dependencies
bunx knip --dependencies
Exit with error if any unused dependencies
若存在未使用依赖项则以错误码退出
bunx knip --dependencies --max-issues 0
**Use in CI to enforce strict dependency hygiene.**bunx knip --dependencies --max-issues 0
**用于CI环境以严格管控依赖项规范。**Check Only Exports (Library Development)
仅检查导出内容(库开发场景)
bash
undefinedbash
undefinedCheck for unused exports
检查未使用的导出内容
bunx knip --exports
bunx knip --exports
Allow exports used in same file
允许在同一文件中使用的导出内容
bunx knip --exports --exclude-exports-used-in-file
**Use for libraries to ensure clean public API.**bunx knip --exports --exclude-exports-used-in-file
**用于类库开发,确保公共API的整洁性。**Production vs Development Dependencies
生产依赖与开发依赖
bash
undefinedbash
undefinedCheck production code only
仅检查生产代码
bunx knip --production
bunx knip --production
Check everything (including dev dependencies)
检查全部内容(包括开发依赖)
bunx knip
undefinedbunx knip
undefinedInterpreting Results
结果解读
Example Output
输出示例
No unused files
No unused dependencies
2 unused exports
src/utils.ts:
- calculateTax (line 42)
- formatDate (line 58)
src/types.ts:
- UserRole (line 12)No unused files
No unused dependencies
2 unused exports
src/utils.ts:
- calculateTax (line 42)
- formatDate (line 58)
src/types.ts:
- UserRole (line 12)Issue Types
问题类型
| Type | Description | Action |
|---|---|---|
| Unused file | File not imported anywhere | Delete or add to entry points |
| Unused dependency | Package in package.json not used | Remove from dependencies |
| Unused export | Exported but never imported | Remove export or make private |
| Unused type | Type/interface exported but unused | Remove or make internal |
| Unused enum member | Enum member never referenced | Remove member |
| Duplicate export | Same export from multiple files | Consolidate exports |
| 类型 | 描述 | 处理建议 |
|---|---|---|
| 未使用文件 | 文件未被任何地方引入 | 删除该文件或添加至入口点 |
| 未使用依赖项 | package.json中的包未被使用 | 从依赖项中移除 |
| 未使用导出内容 | 已导出但从未被引入 | 删除导出或将其设为私有 |
| 未使用类型 | 已导出的类型/接口未被使用 | 删除或设为内部使用 |
| 未使用枚举成员 | 枚举成员从未被引用 | 删除该成员 |
| 重复导出 | 同一内容从多个文件导出 | 整合导出逻辑 |
Agentic Optimizations
智能化优化场景
| Context | Command |
|---|---|
| Quick scan | |
| Dependencies only | |
| Exports only | |
| CI strict mode | |
| Production check | |
| JSON output | |
| Debug config | |
| Changed files | |
| 场景 | 命令 |
|---|---|
| 快速扫描 | |
| 仅检查依赖项 | |
| 仅检查导出内容 | |
| CI严格模式 | |
| 生产环境检查 | |
| JSON格式输出 | |
| 调试配置 | |
| 仅检查变更文件 | |
Quick Reference
快速参考
| Flag | Description |
|---|---|
| Check only unused dependencies |
| Check only unused exports |
| Check only unused files |
| Production dependencies only |
| Fail if more than N issues |
| JSON output for CI |
| Compact output |
| Show configuration details |
| Check only changed files |
| Changed files since main |
| Check specific workspace |
| Ignore same-file exports |
For detailed examples, advanced patterns, and best practices, see REFERENCE.md.
| 参数 | 描述 |
|---|---|
| 仅检查未使用的依赖项 |
| 仅检查未使用的导出内容 |
| 仅检查未使用的文件 |
| 仅检查生产依赖项 |
| 若问题数超过N则失败 |
| 输出JSON格式结果用于CI |
| 输出精简格式结果 |
| 显示配置详情 |
| 仅检查变更的文件 |
| 检查自main分支以来变更的文件 |
| 检查指定的工作区 |
| 忽略同一文件内使用的导出内容 |
如需查看详细示例、高级用法及最佳实践,请参阅 REFERENCE.md。
References
参考链接
- Official docs: https://knip.dev
- Configuration: https://knip.dev/reference/configuration
- Plugins: https://knip.dev/reference/plugins
- CLI reference: https://knip.dev/reference/cli
- FAQ: https://knip.dev/reference/faq