其他
在 Nx 工作区中只读探索项目、配置、target…
适用于需要只读了解 Nx 工作区有哪些项目、项目/ target 配置、工作区配置、受影响项目和项目依赖关系的场景。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 35/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 40/100 ×0.15
适用场景
在 Nx 工作区中只读探索项目、配置、t特定
解决思路
- 以只读方式执行以下查询,用于理解 Nx 工作区结构、项目配置、可用 targets 和依赖,不修改工作区。
- 若 `nx` 没有全局安装,先查看 lockfile 判断包管理器,并在命令前加 `npx`/`pnpx`/`yarn`。
- 用 `nx show projects` 列出工作区中的项目。
- 用 `-p`/`--projects` 过滤项目;该语法可用于 `nx run-many`、`nx release`、`nx show projects` 等许多 Nx 命令。
- 过滤支持显式项目名、glob 模式、tag 引用(如 `tag:name`)、目录和否定(如 `!project-name`)。
- 按 glob 过滤项目:`nx show projects --projects "apps/*"`、`nx show projects --projects "shared-*"`。
- 按 tag 过滤:`nx show projects --projects "tag:publishable"`、`nx show projects -p 'tag:publishable,!tag:internal'`。
- 按 target 过滤:`nx show projects --withTarget build`。
- 组合过滤:`nx show projects --type lib --withTarget test`、`nx show projects --affected --exclude="*-e2e"`、`nx show projects -p "tag:scope:client,packages/*"`。
- 使用否定模式:`nx show projects -p '!tag:private'`、`nx show projects -p '!*-e2e'`。
- 以 JSON 输出项目列表:`nx show projects --json`。
- 用 `nx show project <name> --json` 获取项目的完整解析配置。
- 不要直接读取 `project.json`,它只包含部分配置;`nx show project --json` 会返回完整解析配置,包括插件推断出的 targets。
- 阅读 `node_modules/nx/schemas/project-schema.json` 了解 Nx 项目配置选项。
- 获取完整项目配置:`nx show project my-app --json`。
- 从项目配置 JSON 提取特定部分:`nx show project my-app --json | jq '.targets'`、`nx show project my-app --json | jq '.targets.build'`、`nx show project my-app --json | jq '.targets | keys'`。
- 检查项目元数据:`nx show project my-app --json | jq '{name, root, sourceRoot, projectType, tags}'`。
- 理解 targets 定义项目上可运行的任务。
- 列出项目的所有 targets:`nx show project my-app --json | jq '.targets | keys'`。
- 获取完整 target 配置:`nx show project my-app --json | jq '.targets.build'`。
- 检查 target executor/command:`nx show project my-app --json | jq '.targets.build.executor'`、`nx show project my-app --json | jq '.targets.build.command'`。
- 查看 target options:`nx show project my-app --json | jq '.targets.build.options'`。
- 检查 target inputs/outputs(用于缓存):`nx show project my-app --json | jq '.targets.build.inputs'`、`nx show project my-app --json | jq '.targets.build.outputs'`。
- 查找具有特定 target 的项目:`nx show projects --withTarget serve`、`nx show projects --withTarget e2e`。
- 直接读取 `nx.json` 获取工作区级配置。
- 阅读 `node_modules/nx/schemas/nx-schema.json` 了解 Nx 配置选项。
- 读取完整 `nx.json`:`cat nx.json`。
- 用 jq 读取 `nx.json` 特定部分:`cat nx.json | jq '.targetDefaults'`、`cat nx.json | jq '.namedInputs'`、`cat nx.json | jq '.plugins'`、`cat nx.json | jq '.generators'`。
- 关注 `nx.json` 关键部分:`targetDefaults`(应用于同名 target 的默认配置)、`namedInputs`(用于缓存的可复用 input 定义)、`plugins`(Nx 插件及其配置)等;阅读 schema 或 `nx.json` 获取更多细节。
- 如果用户询问受影响项目,阅读 `references/AFFECTED.md` 获取详细命令和示例。
- 回答“这个工作区里有什么?”:`nx show projects`、`nx show projects --type app`、`nx show projects --type lib`。
- 回答“如何 build/test/lint 项目 X?”:`nx show project X --json | jq '.targets | keys'`、`nx show project X --json | jq '.targets.build'`。
- 回答“什么依赖库 Y?”:`nx graph --print | jq '.graph.dependencies | to_entries[] | select(.value[].target == "Y") | .key'`。
- 处理 Nx CLI 结果时,用命令行工具程序化计算结果,不要手工计数或解析输出。
- 始终使用 `--json` flags 获取结构化输出,并用 `jq`、`grep` 或本地已安装的其他工具处理。
- 列出项目:`nx show projects --json`。
- 统计项目数量:`nx show projects --json | jq 'length'`。
- 按模式过滤项目:`nx show projects --json | jq '.[] | select(startswith("shared-"))'`。
- 获取受影响项目数组:`nx show projects --affected --json | jq '.'`。
- 获取项目详情:`nx show project my-app --json`。
- 获取 target 名称:`nx show project my-app --json | jq '.targets | keys'`。
- 获取特定 target 配置:`nx show project my-app --json | jq '.targets.build'`。
- 获取 tags:`nx show project my-app --json | jq '.tags'`。
- 获取项目 root:`nx show project my-app --json | jq -r '.root'`。
- 获取项目图:`nx graph --print`。
- 从项目图获取所有项目名:`nx graph --print | jq '.graph.nodes | keys'`。
- 查找项目的依赖:`nx graph --print | jq '.graph.dependencies["my-app"]'`。
- 查找依赖某库的项目:`nx graph --print | jq '.graph.dependencies | to_entries[] | select(.value[].target == "shared-ui") | .key'`。
- 排查 “Cannot find configuration for task X:target”:检查项目已有 targets:`nx show project X --json | jq '.targets | keys'`;检查是否有项目具有该 target:`nx show projects --withTarget target`。
- 排查 “The workspace is out of sync”:运行 `nx sync`;如果 `sync` 不能修复陈旧缓存,运行 `nx reset`。
适用边界 · 注意事项
- 不适用于非 Nx 工作区,或未安装/无法通过 `npx`/`pnpx`/`yarn` 调用 Nx CLI 的环境。
- 不适用于需要修改项目配置、生成代码或执行非只读任务的场景;本技能定位为只读探索。
- 不要直接读取 `project.json` 当作完整项目配置;该文件只包含部分配置,应改用 `nx show project --json`。