← 返回 CLI 目录
Knip
$ knip 社区开源 低风险 webpro · 代码托管与研发协同 · ISC · macos / linux / windows
面向 JavaScript 与 TypeScript 项目的依赖与死代码分析器,内置 100+ 框架插件(Next.js、Vite、Vitest、Jest、Playwright)。支持结构化 --reporter json 输出、--production 生产模式分析以及 --fix 清理。
FCRS-1.0 综合评分
90 /100
极佳 (Excellent)
中置信度 (源码/文档)
零摩擦安装命令
npm install -D knip CLI vs MCP 选型建议 仅 CLI 可用
数秒内离线遍历本地 TypeScript/JS 模块图与 package.json,无需 MCP。
Agent 友好度体检单
✓ 原生 JSON 输出
✓ 字段裁剪
✓ 无头非交互
— 支持 Dry-Run
✓ 只读模式开关
— 支持 NO_COLOR
✓ 自省/Schema 发现
— 官方 SKILL.md
当前为静态评估(本机尚未安装或尚未运行探针)。可在本地运行 npm run data:probe 并提交 data/probes/ 升级为实测证据。
FCRS-1.0 六维评分拆解
Agent 友好度 23 / 25
支持 --reporter json、--include/--exclude 分类过滤、--no-exit-code 与 --workspace 定向分析。
--reporter jsonyields structured dead-code inventories--include/--excludefilter issue types cleanly
安全与权限边界 24 / 25
默认只读;即便开启 --fix,也必须额外传入 --allow-remove-files 才会删除文件。
- Requires explicit
--allow-remove-filesopt-in for file deletion
安装与运行摩擦 13 / 15
跨平台 npm 包,需在包含 Node.js 的工作区中运行。
- Runs cleanly via
npx knipacross macOS, Linux, and Windows
能力覆盖与边际价值 14 / 15
自动识别 100+ JS/TS 工具链(Next.js、Vite、Turborepo、Vitest),大幅降低死代码误报。
- Built-in workspace and framework plugin ecosystem
维护与来源背书 8 / 10
由 webpro-nl/knip 以 ISC 协议高频积极维护。
- Frequent releases at
webpro-nl/knip
文档与可验证性 8 / 10
knip.dev/reference/cli 提供清晰的 CLI 参考与插件文档。
- Official CLI reference at knip.dev/reference/cli
安全边界、鉴权与放权指南
鉴权方式
无需鉴权
凭证存储位置
离线本地模块图分析器,不存储也不需要任何凭证。
潜在副作用
只读查询 本地写入
费用与配额提示
ISC 开源 npm 包,完全免费。
可复制到 CLAUDE.md / AGENTS.md 的安全护栏
# Knip (knip) Agent Rules - Always run `npx knip --reporter json` read-only and review findings before using `--fix`. - Never pass `--allow-remove-files` without user confirmation, as dynamic imports or framework entrypoints can cause false positives.
Agent 高频安全命令配方
只读查询(安全推荐)
以 JSON 审计未使用的文件、依赖与导出
npx knip --reporter json --no-exit-code
输出结构化 JSON,详细列出未使用的文件、dependencies、devDependencies 与导出符号,不修改文件。
仅检查生产环境入口
npx knip --production --reporter json
将死代码分析范围严格限定在生产入口及生产依赖。
受控写入与预演
自动移除未使用的导出与依赖
npx knip --fix --allow-remove-files
在显式授权后移除标记的未使用导出、package.json 依赖条目及冗余文件。
核心优势
- +
--reporter json配合--include/--exclude让 Agent 精准定位冗余文件、导出与依赖。 - + 删除文件需显式传入
--allow-remove-files,防止--fix误删。
注意事项与踩坑点
- ! 动态
require()或非常规入口可能出现误报,需在knip.json中补充入口配置。 - ! 准确解析依赖图前需先安装工作区的
node_modules。
已验证兼容的 Agent 宿主
Claude Code Codex CLI Gemini CLI