knip

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Knip Code Cleanup

Knip代码清理

Run knip to find and remove unused files, dependencies, and exports from this codebase.
运行knip来查找并移除代码库中未使用的文件、依赖项和导出内容。

Setup

设置

  1. Check if knip is available:
    • Run
      npx knip --version
      to test
    • If it fails or is very slow, check if
      knip
      is in package.json devDependencies
    • If not installed locally, install with
      npm install -D knip
      (or pnpm/yarn equivalent based on lockfile present)
  2. If no
    knip.json
    or
    knip.jsonc
    config exists and knip reports many false positives, consider creating a minimal config based on the frameworks detected in package.json
  1. 检查knip是否可用:
    • 运行
      npx knip --version
      进行测试
    • 如果运行失败或速度极慢,检查
      knip
      是否在package.json的devDependencies中
    • 如果未本地安装,使用
      npm install -D knip
      进行安装(或根据现有锁文件使用pnpm/yarn的等效命令)
  2. 如果不存在
    knip.json
    knip.jsonc
    配置文件,且knip报告大量误报,可以根据package.json中检测到的框架创建一个最小配置

Execution

执行

  1. Run
    npx knip
    to get initial report
  2. 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
  1. 运行
    npx knip
    获取初始报告
  2. 查看输出分类:
    • 未使用的文件 - 未在任何地方导入的文件
    • 未使用的依赖项 - 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
--fix --allow-remove-files
for automated fixes, or manually delete/edit as needed.
  • 明显属于内部的未使用导出内容(不属于公共API)
  • 未使用的类型导出
  • 未使用的依赖项(从package.json中移除)
  • 明显孤立的未使用文件(不是入口文件,也不是配置文件)
对于这些内容,无需询问即可直接删除。使用
--fix --allow-remove-files
进行自动修复,或根据需要手动删除/编辑。

Ask 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
    ,
    lib/
    , or files with "public" or "api" in the name
Use the AskUserQuestion tool to clarify before deleting these.
  • 可能是入口文件或动态导入的文件
  • 可能属于公共API的导出内容(index.ts、lib导出)
  • 可能通过CLI或peer dependencies使用的依赖项
  • 位于
    src/index
    lib/
    路径下,或名称中包含“public”或“api”的任何文件
在删除这些内容之前,使用AskUserQuestion工具进行确认。

Workflow

工作流程

  1. Run knip, capture full output
  2. Categorize each issue as auto-delete or needs-clarification
  3. Ask about uncertain items in a single batch question
  4. Perform all deletions (use Edit tool to remove exports, Bash to remove files/deps)
  5. Re-run knip to verify cleanup is complete
  6. Repeat until no issues remain or only intentionally-ignored items exist
  1. 运行knip,捕获完整输出
  2. 将每个问题分类为自动删除或需要确认
  3. 一次性询问所有不确定的项
  4. 执行所有删除操作(使用Edit工具移除导出内容,使用Bash工具移除文件/依赖项)
  5. 重新运行knip以验证清理完成
  6. 重复上述步骤,直到没有问题剩余,或仅存在有意忽略的项

Common Commands

常用命令

bash
undefined
bash
undefined

Basic 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
undefined
npx knip --reporter json
undefined

Notes

注意事项

  • If knip config exists, respect its ignore patterns
  • Watch for monorepo setups - may need
    --workspace
    flag
  • Some frameworks need plugins enabled in config
  • Re-run after each batch of fixes to catch newly-exposed unused code
  • 如果存在knip配置,遵守其忽略规则
  • 注意monorepo设置 - 可能需要
    --workspace
    标志
  • 某些框架需要在配置中启用插件
  • 每完成一批修复后重新运行knip,以发现新暴露的未使用代码