nx-workspace

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Nx Workspace Exploration

Nx工作区探索

This skill provides read-only exploration of Nx workspaces. Use it to understand workspace structure, project configuration, available targets, and dependencies.
Keep in mind that you might have to prefix commands with
npx
/
pnpx
/
yarn
if nx isn't installed globally. Check the lockfile to determine the package manager in use.
本技能提供Nx工作区的只读探索功能,可用于了解工作区结构、项目配置、可用目标任务及依赖关系。
请记住,如果未全局安装nx,你可能需要在命令前添加
npx
/
pnpx
/
yarn
前缀。可以通过锁文件确定当前使用的包管理器。

Listing Projects

列出项目

Use
nx show projects
to list projects in the workspace.
The project filtering syntax (
-p
/
--projects
) works across many Nx commands including
nx run-many
,
nx release
,
nx show projects
, and more. Filters support explicit names, glob patterns, tag references (e.g.
tag:name
), directories, and negation (e.g.
!project-name
).
bash
undefined
使用
nx show projects
命令列出工作区中的项目。
项目过滤语法(
-p
/
--projects
)适用于众多Nx命令,包括
nx run-many
nx release
nx show projects
等。过滤器支持明确的项目名称、通配符模式、标签引用(如
tag:name
)、目录路径以及取反操作(如
!project-name
)。
bash
undefined

List all projects

列出所有项目

nx show projects
nx show projects

Filter by pattern (glob)

按通配符模式过滤

nx show projects --projects "apps/" nx show projects --projects "shared-"
nx show projects --projects "apps/" nx show projects --projects "shared-"

Filter by tag

按标签过滤

nx show projects --projects "tag:publishable" nx show projects -p 'tag:publishable,!tag:internal'
nx show projects --projects "tag:publishable" nx show projects -p 'tag:publishable,!tag:internal'

Filter by target (projects that have a specific target)

按目标任务过滤(包含指定目标任务的项目)

nx show projects --withTarget build
nx show projects --withTarget build

Find affected projects (changed since base branch)

查找受影响的项目(与基准分支相比有变更的项目)

nx show projects --affected nx show projects --affected --base=main nx show projects --affected --type app
nx show projects --affected nx show projects --affected --base=main nx show projects --affected --type app

Combine filters

组合过滤器

nx show projects --type lib --withTarget test nx show projects --affected --exclude="-e2e" nx show projects -p "tag:scope:client,packages/"
nx show projects --type lib --withTarget test nx show projects --affected --exclude="-e2e" nx show projects -p "tag:scope:client,packages/"

Negate patterns (use single quotes to prevent bash history expansion)

取反模式(使用单引号避免bash历史展开)

nx show projects -p '!tag:private' nx show projects -p '!*-e2e'
nx show projects -p '!tag:private' nx show projects -p '!*-e2e'

Output as JSON

以JSON格式输出

nx show projects --json
undefined
nx show projects --json
undefined

Project Configuration

项目配置

Use
nx show project <name> --json
to get the full resolved configuration for a project.
Important: Do NOT read
project.json
directly - it only contains partial configuration. The
nx show project --json
command returns the full resolved config including inferred targets from plugins.
You can read the full project schema at
node_modules/nx/schemas/project-schema.json
to understand nx project configuration options.
bash
undefined
使用
nx show project <name> --json
获取项目的完整解析配置。
重要提示:请勿直接读取
project.json
文件——它仅包含部分配置。
nx show project --json
命令返回的是完整的解析配置,包括从插件中推断出的目标任务。
你可以查看
node_modules/nx/schemas/project-schema.json
中的完整项目Schema,以了解Nx项目的配置选项。
bash
undefined

Get full project configuration

获取完整的项目配置

nx show project my-app --json
nx show project my-app --json

Extract specific parts from the 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 '.targets' nx show project my-app --json | jq '.targets.build' nx show project my-app --json | jq '.targets | keys'

Check project metadata

查看项目元数据

nx show project my-app --json | jq '{name, root, sourceRoot, projectType, tags}'
undefined
nx show project my-app --json | jq '{name, root, sourceRoot, projectType, tags}'
undefined

Target Information

目标任务信息

Targets define what tasks can be run on a project.
bash
undefined
目标任务定义了可在项目上运行的操作。
bash
undefined

List all targets for a project

列出项目的所有目标任务

nx show project my-app --json | jq '.targets | keys'
nx show project my-app --json | jq '.targets | keys'

Get full target configuration

获取完整的目标任务配置

nx show project my-app --json | jq '.targets.build'
nx show project my-app --json | jq '.targets.build'

Check target executor/command

查看目标任务的执行器/命令

nx show project my-app --json | jq '.targets.build.executor' nx show project my-app --json | jq '.targets.build.command'
nx show project my-app --json | jq '.targets.build.executor' nx show project my-app --json | jq '.targets.build.command'

View target options

查看目标任务选项

nx show project my-app --json | jq '.targets.build.options'
nx show project my-app --json | jq '.targets.build.options'

Check target inputs/outputs (for caching)

查看目标任务的输入/输出(用于缓存)

nx show project my-app --json | jq '.targets.build.inputs' nx show project my-app --json | jq '.targets.build.outputs'
nx show project my-app --json | jq '.targets.build.inputs' nx show project my-app --json | jq '.targets.build.outputs'

Find projects with a specific target

查找包含特定目标任务的项目

nx show projects --withTarget serve nx show projects --withTarget e2e
undefined
nx show projects --withTarget serve nx show projects --withTarget e2e
undefined

Workspace Configuration

工作区配置

Read
nx.json
directly for workspace-level configuration. You can read the full project schema at
node_modules/nx/schemas/nx-schema.json
to understand nx project configuration options.
bash
undefined
可直接读取
nx.json
文件获取工作区级别的配置。 你可以查看
node_modules/nx/schemas/nx-schema.json
中的完整Schema,以了解Nx项目的配置选项。
bash
undefined

Read the full nx.json

读取完整的nx.json文件

cat nx.json
cat nx.json

Or use jq for specific sections

或使用jq提取特定部分

cat nx.json | jq '.targetDefaults' cat nx.json | jq '.namedInputs' cat nx.json | jq '.plugins' cat nx.json | jq '.generators'

Key nx.json sections:

- `targetDefaults` - Default configuration applied to all targets of a given name
- `namedInputs` - Reusable input definitions for caching
- `plugins` - Nx plugins and their configuration
- ...and much more, read the schema or nx.json for details
cat nx.json | jq '.targetDefaults' cat nx.json | jq '.namedInputs' cat nx.json | jq '.plugins' cat nx.json | jq '.generators'

nx.json的关键部分:

- `targetDefaults` - 应用于所有同名目标任务的默认配置
- `namedInputs` - 可复用的缓存输入定义
- `plugins` - Nx插件及其配置
- ...更多内容请查看Schema或nx.json文件详情

Affected Projects

受影响的项目

Find projects affected by changes in the current branch.
bash
undefined
查找当前分支中受变更影响的项目。
bash
undefined

Affected since base branch (auto-detected)

查找与基准分支相比受影响的项目(自动检测基准分支)

nx show projects --affected
nx show projects --affected

Affected with explicit base

指定基准分支查找受影响的项目

nx show projects --affected --base=main nx show projects --affected --base=origin/main
nx show projects --affected --base=main nx show projects --affected --base=origin/main

Affected between two commits

查找两个提交之间受影响的项目

nx show projects --affected --base=abc123 --head=def456
nx show projects --affected --base=abc123 --head=def456

Affected apps only

仅查找受影响的应用项目

nx show projects --affected --type app
nx show projects --affected --type app

Affected excluding e2e projects

查找受影响的项目(排除e2e项目)

nx show projects --affected --exclude="*-e2e"
nx show projects --affected --exclude="*-e2e"

Affected by uncommitted changes

查找受未提交变更影响的项目

nx show projects --affected --uncommitted
nx show projects --affected --uncommitted

Affected by untracked files

查找受未跟踪文件影响的项目

nx show projects --affected --untracked
undefined
nx show projects --affected --untracked
undefined

Common Exploration Patterns

常见探索场景

"What's in this workspace?"

"此工作区中有什么内容?"

bash
nx show projects
nx show projects --type app
nx show projects --type lib
bash
nx show projects
nx show projects --type app
nx show projects --type lib

"How do I build/test/lint project X?"

"如何构建/测试/检查项目X?"

bash
nx show project X --json | jq '.targets | keys'
nx show project X --json | jq '.targets.build'
bash
nx show project X --json | jq '.targets | keys'
nx show project X --json | jq '.targets.build'

"What depends on library Y?"

"哪些项目依赖库Y?"

bash
undefined
bash
undefined

Use the project graph to find dependents

使用项目图查找依赖该库的项目

nx graph --print | jq '.graph.dependencies | to_entries[] | select(.value[].target == "Y") | .key'
undefined
nx graph --print | jq '.graph.dependencies | to_entries[] | select(.value[].target == "Y") | .key'
undefined

Programmatic Answers

程序化处理结果

When processing nx CLI results, use command-line tools to compute the answer programmatically rather than counting or parsing output manually. Always use
--json
flags to get structured output that can be processed with
jq
,
grep
, or other tools you have installed locally.
处理Nx CLI结果时,请使用命令行工具以程序化方式计算答案,而非手动计数或解析输出。请始终使用
--json
标志获取结构化输出,以便使用
jq
grep
或本地安装的其他工具进行处理。

Listing Projects

列出项目

bash
nx show projects --json
Example output:
json
["my-app", "my-app-e2e", "shared-ui", "shared-utils", "api"]
Common operations:
bash
undefined
bash
nx show projects --json
示例输出:
json
["my-app", "my-app-e2e", "shared-ui", "shared-utils", "api"]
常见操作:
bash
undefined

Count projects

统计项目数量

nx show projects --json | jq 'length'
nx show projects --json | jq 'length'

Filter by pattern

按模式过滤项目

nx show projects --json | jq '.[] | select(startswith("shared-"))'
nx show projects --json | jq '.[] | select(startswith("shared-"))'

Get affected projects as array

以数组形式获取受影响的项目

nx show projects --affected --json | jq '.'
undefined
nx show projects --affected --json | jq '.'
undefined

Project Details

项目详情

bash
nx show project my-app --json
Example output:
json
{
  "root": "apps/my-app",
  "name": "my-app",
  "sourceRoot": "apps/my-app/src",
  "projectType": "application",
  "tags": ["type:app", "scope:client"],
  "targets": {
    "build": {
      "executor": "@nx/vite:build",
      "options": { "outputPath": "dist/apps/my-app" }
    },
    "serve": {
      "executor": "@nx/vite:dev-server",
      "options": { "buildTarget": "my-app:build" }
    },
    "test": {
      "executor": "@nx/vite:test",
      "options": {}
    }
  },
  "implicitDependencies": []
}
Common operations:
bash
undefined
bash
nx show project my-app --json
示例输出:
json
{
  "root": "apps/my-app",
  "name": "my-app",
  "sourceRoot": "apps/my-app/src",
  "projectType": "application",
  "tags": ["type:app", "scope:client"],
  "targets": {
    "build": {
      "executor": "@nx/vite:build",
      "options": { "outputPath": "dist/apps/my-app" }
    },
    "serve": {
      "executor": "@nx/vite:dev-server",
      "options": { "buildTarget": "my-app:build" }
    },
    "test": {
      "executor": "@nx/vite:test",
      "options": {}
    }
  },
  "implicitDependencies": []
}
常见操作:
bash
undefined

Get target names

获取目标任务名称

nx show project my-app --json | jq '.targets | keys'
nx show project my-app --json | jq '.targets | keys'

Get specific target config

获取特定目标任务的配置

nx show project my-app --json | jq '.targets.build'
nx show project my-app --json | jq '.targets.build'

Get tags

获取项目标签

nx show project my-app --json | jq '.tags'
nx show project my-app --json | jq '.tags'

Get project root

获取项目根目录

nx show project my-app --json | jq -r '.root'
undefined
nx show project my-app --json | jq -r '.root'
undefined

Project Graph

项目图

bash
nx graph --print
Example output:
json
{
  "graph": {
    "nodes": {
      "my-app": {
        "name": "my-app",
        "type": "app",
        "data": { "root": "apps/my-app", "tags": ["type:app"] }
      },
      "shared-ui": {
        "name": "shared-ui",
        "type": "lib",
        "data": { "root": "libs/shared-ui", "tags": ["type:ui"] }
      }
    },
    "dependencies": {
      "my-app": [
        { "source": "my-app", "target": "shared-ui", "type": "static" }
      ],
      "shared-ui": []
    }
  }
}
Common operations:
bash
undefined
bash
nx graph --print
示例输出:
json
{
  "graph": {
    "nodes": {
      "my-app": {
        "name": "my-app",
        "type": "app",
        "data": { "root": "apps/my-app", "tags": ["type:app"] }
      },
      "shared-ui": {
        "name": "shared-ui",
        "type": "lib",
        "data": { "root": "libs/shared-ui", "tags": ["type:ui"] }
      }
    },
    "dependencies": {
      "my-app": [
        { "source": "my-app", "target": "shared-ui", "type": "static" }
      ],
      "shared-ui": []
    }
  }
}
常见操作:
bash
undefined

Get all project names from graph

从项目图中获取所有项目名称

nx graph --print | jq '.graph.nodes | keys'
nx graph --print | jq '.graph.nodes | keys'

Find dependencies of a project

查找项目的依赖项

nx graph --print | jq '.graph.dependencies["my-app"]'
nx graph --print | jq '.graph.dependencies["my-app"]'

Find projects that depend on a library

查找依赖某个库的项目

nx graph --print | jq '.graph.dependencies | to_entries[] | select(.value[].target == "shared-ui") | .key'
undefined
nx graph --print | jq '.graph.dependencies | to_entries[] | select(.value[].target == "shared-ui") | .key'
undefined

Troubleshooting

故障排查

"Cannot find configuration for task X:target"

"找不到任务X:target的配置"

bash
undefined
bash
undefined

Check what targets exist on the project

检查项目中存在哪些目标任务

nx show project X --json | jq '.targets | keys'
nx show project X --json | jq '.targets | keys'

Check if any projects have that target

检查是否有项目包含该目标任务

nx show projects --withTarget target
undefined
nx show projects --withTarget target
undefined

"The workspace is out of sync"

"工作区不同步"

bash
nx sync
nx reset  # if sync doesn't fix stale cache
bash
nx sync
nx reset  # 如果sync无法修复缓存过期问题