knip
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseKnip Code Cleanup
Knip代码清理
Run knip to find and remove unused files, dependencies, and exports from this codebase.
运行knip来查找并移除代码库中未使用的文件、依赖项和导出内容。
Setup
设置
-
Check if knip is available:
- Run to test
npx knip --version - If it fails or is very slow, check if is in package.json devDependencies
knip - If not installed locally, install with (or pnpm/yarn equivalent based on lockfile present)
npm install -D knip
- Run
-
If noor
knip.jsonconfig exists and knip reports many false positives, consider creating a minimal config based on the frameworks detected in package.jsonknip.jsonc
-
检查knip是否可用:
- 运行进行测试
npx knip --version - 如果运行失败或速度极慢,检查是否在package.json的devDependencies中
knip - 如果未本地安装,使用进行安装(或根据现有锁文件使用pnpm/yarn的等效命令)
npm install -D knip
- 运行
-
如果不存在或
knip.json配置文件,且knip报告大量误报,可以根据package.json中检测到的框架创建一个最小配置knip.jsonc
Execution
执行
- Run to get initial report
npx knip - Review the output categories:
- Unused files - files not imported anywhere
- Unused dependencies - packages in package.json not imported
- Unused devDependencies - dev packages not used
- Unused exports - exported functions/variables not imported elsewhere
- Unused types - exported types not used
- 运行获取初始报告
npx knip - 查看输出分类:
- 未使用的文件 - 未在任何地方导入的文件
- 未使用的依赖项 - package.json中存在但未被导入的包
- 未使用的devDependencies - 未被使用的开发依赖包
- 未使用的导出内容 - 已导出但未在其他地方导入的函数/变量
- 未使用的类型 - 已导出但未被使用的类型
Cleanup Strategy
清理策略
Auto-delete (high confidence):
自动删除(高可信度):
- Unused exports that are clearly internal (not part of public API)
- Unused type exports
- Unused dependencies (remove from package.json)
- Unused files that are clearly orphaned (not entry points, not config files)
For these, proceed with deletion without asking. Use for automated fixes, or manually delete/edit as needed.
--fix --allow-remove-files- 明显属于内部的未使用导出内容(不属于公共API)
- 未使用的类型导出
- 未使用的依赖项(从package.json中移除)
- 明显孤立的未使用文件(不是入口文件,也不是配置文件)
对于这些内容,无需询问即可直接删除。使用进行自动修复,或根据需要手动删除/编辑。
--fix --allow-remove-filesAsk first (needs clarification):
先询问(需要确认):
- Files that might be entry points or dynamically imported
- Exports that might be part of a public API (index.ts, lib exports)
- Dependencies that might be used via CLI or peer dependencies
- Anything in paths like ,
src/index, or files with "public" or "api" in the namelib/
Use the AskUserQuestion tool to clarify before deleting these.
- 可能是入口文件或动态导入的文件
- 可能属于公共API的导出内容(index.ts、lib导出)
- 可能通过CLI或peer dependencies使用的依赖项
- 位于、
src/index路径下,或名称中包含“public”或“api”的任何文件lib/
在删除这些内容之前,使用AskUserQuestion工具进行确认。
Workflow
工作流程
- Run knip, capture full output
- Categorize each issue as auto-delete or needs-clarification
- Ask about uncertain items in a single batch question
- Perform all deletions (use Edit tool to remove exports, Bash to remove files/deps)
- Re-run knip to verify cleanup is complete
- Repeat until no issues remain or only intentionally-ignored items exist
- 运行knip,捕获完整输出
- 将每个问题分类为自动删除或需要确认
- 一次性询问所有不确定的项
- 执行所有删除操作(使用Edit工具移除导出内容,使用Bash工具移除文件/依赖项)
- 重新运行knip以验证清理完成
- 重复上述步骤,直到没有问题剩余,或仅存在有意忽略的项
Common Commands
常用命令
bash
undefinedbash
undefinedBasic run
基础运行
npx knip
npx knip
Production only (ignore test files)
仅生产环境(忽略测试文件)
npx knip --production
npx knip --production
Auto-fix what's safe
自动修复安全的项
npx knip --fix
npx knip --fix
Auto-fix including file deletion
自动修复,包括删除文件
npx knip --fix --allow-remove-files
npx knip --fix --allow-remove-files
JSON output for parsing
生成JSON格式输出以便解析
npx knip --reporter json
undefinednpx knip --reporter json
undefinedNotes
注意事项
- If knip config exists, respect its ignore patterns
- Watch for monorepo setups - may need flag
--workspace - Some frameworks need plugins enabled in config
- Re-run after each batch of fixes to catch newly-exposed unused code
- 如果存在knip配置,遵守其忽略规则
- 注意monorepo设置 - 可能需要标志
--workspace - 某些框架需要在配置中启用插件
- 每完成一批修复后重新运行knip,以发现新暴露的未使用代码