npx skills add https://github.com/brianlovin/claude-config --skill knip运行 knip 来查找并移除此代码库中未使用的文件、依赖项和导出。
检查 knip 是否可用:
npx knip --version 进行测试knip 是否在 package.json 的 devDependencies 中npm install -D knip 安装(或根据存在的 lockfile 使用 pnpm/yarn/bun 的等效命令)Knip 不会移除文件内部未使用的导入/变量——那是 linter 的工作。Knip 查找项目中未使用的文件、依赖项和导出。
始终遵循此配置优先的工作流程。即使是简单的“运行 knip”或“清理代码库”提示,在根据报告的问题采取行动之前,也要正确配置 knip。
knip.json、knip.jsonc 或 package.json 中的 knip 键)广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
npx knip
在关注其他任何内容之前,先关注配置提示。这些提示出现在输出的顶部,并建议进行配置调整以减少误报。
在处理报告的问题之前,先修复配置提示。常见的调整包括:
每次配置更改后重新运行 knip。重复此过程,直到配置提示得到解决且误报最小化。
配置确定后,处理报告的问题。按以下顺序确定优先级:
ignoreExportsUsedInFile(见下文)每批修复后重新运行 knip。移除未使用的文件通常会暴露出新出现的未使用导出和依赖项。
审查或创建 knip 配置时,请遵循以下规则:
ignore 模式 — ignore 会隐藏真正的问题,几乎永远不应使用。始终优先选择具体的解决方案。其他 ignore* 选项(如 ignoreDependencies、ignoreExportsUsedInFile)是可以的,因为它们针对特定的问题类型。ignoreExportsUsedInFile: { interface: true, type: true } — 这处理了类型仅在同一个文件中使用的常见情况。优先使用此选项而非更广泛的忽略选项。.gitignore,因此忽略 node_modules、dist、build、.git 是多余的。vite.config.ts)— 显式启用或禁用相应的插件,而不是忽略该文件。buffer、process)— 添加到 ignoreDependencies。paths 添加到 knip 配置(使用 tsconfig.json 语义)。使用 --production 仅关注生产代码:
npx knip --production
这会排除测试文件、配置文件和其他非生产入口点。请勿使用 project 或 ignore 模式来排除测试文件——请改用 --production。
src/index、lib/ 或文件名中包含 "public" 或 "api" 的任何内容在删除这些内容之前,使用 AskUserQuestion 工具进行澄清。
一旦配置确定并且对结果有信心:
# 自动修复安全的更改(移除未使用的导出和依赖项)
npx knip --fix
# 自动修复包括文件删除
npx knip --fix --allow-remove-files
只有在配置优先的工作流程完成后才使用 --fix。
如果 knip 以代码 2 退出(意外错误,如“加载文件错误”):
knip.json# 基本运行
npx knip
# 仅生产模式(排除测试/配置入口点)
npx knip --production
# 自动修复安全的内容
npx knip --fix
# 自动修复包括文件删除
npx knip --fix --allow-remove-files
# 用于解析的 JSON 输出
npx knip --reporter json
--workspace 标志每周安装次数
241
仓库
GitHub 星标数
275
首次出现
2026年1月20日
安全审计
安装于
claude-code204
codex202
opencode129
gemini-cli121
cursor117
github-copilot112
Run knip to find and remove unused files, dependencies, and exports from this codebase.
Check if knip is available:
npx knip --version to testknip is in package.json devDependenciesnpm install -D knip (or pnpm/yarn/bun equivalent based on lockfile present)Knip does NOT remove unused imports/variables inside files — that's a linter's job. Knip finds unused files, dependencies, and exports across the project.
Always follow this configuration-first workflow. Even for simple "run knip" or "clean up codebase" prompts, configure knip properly before acting on reported issues.
knip.json, knip.jsonc, or knip key in package.json)npx knip
Focus on configuration hints before anything else. These appear at the top of the output and suggest config adjustments to reduce false positives.
Fix configuration hints before addressing reported issues. Common adjustments:
Re-run knip after each config change. Repeat until configuration hints are resolved and false positives are minimized.
Once the configuration is settled, work through reported issues. Prioritize in this order:
ignoreExportsUsedInFile (see below)Re-run knip after each batch of fixes. Removing unused files often exposes newly-unused exports and dependencies.
When reviewing or creating a knip config, follow these rules:
ignore patterns — ignore hides real issues and should almost never be used. Always prefer specific solutions. Other ignore* options (like ignoreDependencies, ignoreExportsUsedInFile) are fine because they target specific issue types.ignoreExportsUsedInFile: { interface: true, type: true } — this handles the common case of types only used in the same file. Prefer this over broader ignore options..gitignore, so ignoring node_modules, dist, , is redundant.Use --production to focus on production code only:
npx knip --production
This excludes test files, config files, and other non-production entry points. Do NOT use project or ignore patterns to exclude test files — use --production instead.
src/index, lib/, or files with "public" or "api" in the nameUse the AskUserQuestion tool to clarify before deleting these.
Once configuration is settled and you're confident in the results:
# Auto-fix safe changes (removes unused exports and dependencies)
npx knip --fix
# Auto-fix including file deletion
npx knip --fix --allow-remove-files
Only use --fix after the configuration-first workflow is complete.
If knip exits with code 2 (unexpected error like "error loading file"):
knip.json in the project root# Basic run
npx knip
# Production only (excludes test/config entry points)
npx knip --production
# Auto-fix what's safe
npx knip --fix
# Auto-fix including file deletion
npx knip --fix --allow-remove-files
# JSON output for parsing
npx knip --reporter json
--workspace flagWeekly Installs
241
Repository
GitHub Stars
275
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code204
codex202
opencode129
gemini-cli121
cursor117
github-copilot112
agent-browser 浏览器自动化工具 - Vercel Labs 命令行网页操作与测试
140,500 周安装
OKX DEX 兑换聚合器 - 多链代币兑换报价、授权与执行完整指南
3,400 周安装
here.now 静态网站一键部署工具 - 从文件/文件夹创建实时URL
3,500 周安装
Azure 定价查询技能 - 实时获取 Azure 服务成本与价格比较
3,400 周安装
Cloudflare Workers 最佳实践指南:API、配置、架构与安全规则
3,500 周安装
n8n工作流五大核心模式详解:Webhook处理、API集成、数据库操作、AI智能体与计划任务
3,500 周安装
Angular 20+ 自定义指令教程:属性指令、DOM操作与组件复用指南
3,500 周安装
build.gitvite.config.ts) — Enable or disable the corresponding plugin explicitly rather than ignoring the file.buffer, process) — Add to ignoreDependencies.paths to knip config (uses tsconfig.json semantics).