探索並理解 Nx 工作區。當回答關於工作區、專案或任務的問題時使用。也請在 nx 指令失敗,或需要在執行任務前檢查可用的目標/設定時使用。範例:『這個工作區有哪些專案?』、『專案 X 是怎麼設定的?』、『哪些專案依賴函式庫 Y?』、『我可以執行哪些目標?』、『找不到任務的設定』、『除錯 nx 任務失敗』。
Nx 工作區探索
此技能提供 Nx 工作區的唯讀探索。用於了解工作區結構、專案設定、可用的目標及相依關係。
請注意,如果 nx 未全域安裝,您可能需要在指令前加上 npx/pnpx/yarn。檢查 lockfile 以確定使用的套件管理工具。
列出專案
使用 nx show projects 列出工作區中的專案。
專案過濾語法(-p/--projects)適用於許多 Nx 指令,包括 nx run-many、nx release、nx show projects 等。過濾器支援明確名稱、glob 模式、標籤參考(例如 tag:name)、目錄以及否定(例如 !project-name)。
# 列出所有專案
nx show projects
# 依模式過濾(glob)
nx show projects --projects "apps/*"
nx show projects --projects "shared-*"
# 依標籤過濾
nx show projects --projects "tag:publishable"
nx show projects -p 'tag:publishable,!tag:internal'
# 依目標過濾(具有特定目標的專案)
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 指令會傳回完整的解析設定,包括從外掛推斷的目標。
您可以在 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}'
目標資訊
目標定義了可以在專案上執行的任務。
# 列出專案的所有目標
nx show project my-app --json | jq '.targets | keys'
# 取得完整的目標設定
nx show project my-app --json | jq '.targets.build'
# 檢查目標執行器/指令
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.options'
# 檢查目標輸入/輸出(用於快取)
nx show project my-app --json | jq '.targets.build.inputs'
nx show project my-app --json | jq '.targets.build.outputs'
# 尋找具有特定目標的專案
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 讀取特定區段
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 外掛及其設定- ...還有更多,請閱讀綱要或 nx.json 以了解詳細資訊
受影響的專案
如果使用者詢問受影響的專案,請閱讀受影響的專案參考以取得詳細指令和範例。
常見探索模式
「這個工作區裡有什麼?」
nx show projects
nx show projects --type app
nx show projects --type lib
「如何建置/測試/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 旗標以取得結構化輸出,並使用您本機安裝的 jq、grep 或其他工具進行處理。
列出專案
nx show projects --json
範例輸出:
["my-app", "my-app-e2e", "shared-ui", "shared-utils", "api"]
常見操作:
# 計算專案數量
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
範例輸出:
{
"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": []
}
常見操作:
# 取得目標名稱
nx show project my-app --json | jq '.targets | keys'
# 取得特定目標設定
nx show project my-app --json | jq '.targets.build'
# 取得標籤
nx show project my-app --json | jq '.tags'
# 取得專案根目錄
nx show project my-app --json | jq -r '.root'
專案圖表
nx graph --print
範例輸出:
{
"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": []
}
}
}
常見操作:
# 從圖表中取得所有專案名稱
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'
疑難排解
「找不到任務 X:target 的設定」
# 檢查專案上存在哪些目標
nx show project X --json | jq '.targets | keys'
# 檢查是否有任何專案具有該目標
nx show projects --withTarget target
「工作區不同步」
nx sync
nx reset # 如果 sync 無法修復過期的快取






