npm-query

相依性選取器查詢

選擇 CLI 版本

概要

npm query <selector>

說明

npm query 指令允許使用 css 選取器來擷取相依性物件陣列。

將 npm 查詢傳遞至其他指令

# find all dependencies with postinstall scripts & uninstall them
npm query ":attr(scripts, [postinstall])" | jq 'map(.name)|join("\n")' -r | xargs -I {} npm uninstall {}
# find all git dependencies & explain who requires them
npm query ":type(git)" | jq 'map(.name)' | xargs -I {} npm why {}

延伸使用案例和查詢

// all deps
*
// all direct deps
:root > *
// direct production deps
:root > .prod
// direct development deps
:root > .dev
// any peer dep of a direct deps
:root > * > .peer
// any workspace dep
.workspace
// all workspaces that depend on another workspace
.workspace > .workspace
// all workspaces that have peer deps
.workspace:has(.peer)
// any dep named "lodash"
// equivalent to [name="lodash"]
#lodash
// any deps named "lodash" & within semver range ^"1.2.3"
#lodash@^1.2.3
// equivalent to...
[name="lodash"]:semver(^1.2.3)
// get the hoisted node for a given semver range
#lodash@^1.2.3:not(:deduped)
// querying deps with a specific version
#lodash@2.1.5
// equivalent to...
[name="lodash"][version="2.1.5"]
// has any deps
:has(*)
// deps with no other deps (ie. "leaf" nodes)
:empty
// manually querying git dependencies
[repository^=github:],
[repository^=git:],
[repository^=https://github.com],
[repository^=http://github.com],
[repository^=https://github.com],
[repository^=+git:...]
// querying for all git dependencies
:type(git)
// get production dependencies that aren't also dev deps
.prod:not(.dev)
// get dependencies with specific licenses
[license=MIT], [license=ISC]
// find all packages that have @ruyadorno as a contributor
:attr(contributors, [email=ruyadorno@github.com])

範例回應輸出

  • 傳回相依性物件陣列,其中可能包含同一個套件的複本,這些套件可能已連結或未連結、已重複或未重複
[
{
"name": "",
"version": "",
"description": "",
"homepage": "",
"bugs": {},
"author": {},
"license": {},
"funding": {},
"files": [],
"main": "",
"browser": "",
"bin": {},
"man": [],
"directories": {},
"repository": {},
"scripts": {},
"config": {},
"dependencies": {},
"devDependencies": {},
"optionalDependencies": {},
"bundledDependencies": {},
"peerDependencies": {},
"peerDependenciesMeta": {},
"engines": {},
"os": [],
"cpu": [],
"workspaces": {},
"keywords": [],
...
},
...

預期特定數量的結果

npm query 的一個常見用途是確保樹狀結構中只有一個特定相依性的版本。這對於依賴 typescript 的生態系統來說尤其常見,因為在兩個不同但名稱相同的套件中分割狀態會導致錯誤。您可以在設定中使用 --expect-results--expect-result-count,以確保如果樹狀結構不符合您的期望,npm 會以退出碼退出。

$ npm query '#react' --expect-result-count=1

也許您想快速檢查是否有任何可以更新的生產依賴項

$ npm query ':root>:outdated(in-range).prod' --no-expect-results

僅限套件鎖定模式

如果啟用 package-lock-only,則只會載入封鎖封裝 (或 shrinkwrap) 中的資訊。這表示您的依賴項的 package.json 檔案中的資訊不會包含在結果集中 (例如說明、首頁、引擎)。

設定

全域

  • 預設值:false
  • 類型:布林值

在「全域」模式中運作,以便將封裝安裝到 prefix 資料夾中,而不是目前的作業目錄。請參閱 資料夾,以進一步了解行為差異。

  • 封裝安裝到 {prefix}/lib/node_modules 資料夾中,而不是目前的作業目錄。
  • bin 檔案連結到 {prefix}/bin
  • man 頁面連結到 {prefix}/share/man

工作區

  • 預設值
  • 類型:字串 (可以設定多次)

啟用在目前專案設定的工作空間的內容中執行指令,同時透過僅執行此設定選項定義的工作空間來過濾。

workspace 設定的有效值為

  • 工作空間名稱
  • 工作空間目錄的路徑
  • 父工作空間目錄的路徑 (將導致選取該資料夾中的所有工作空間)

當設定為 npm init 指令時,這可能會設定為尚未存在的資料夾,以建立資料夾並將其設定為專案中的全新工作空間。

此值不會匯出到子程序的環境中。

工作區

  • 預設值:null
  • 類型:null 或布林值

設定為 true 以在所有設定的工作空間的內容中執行指令。

明確將此設定為 false 將導致 install 等指令完全忽略工作空間。未明確設定時

  • node_modules 樹狀結構上運作的指令 (install、update 等) 將連結工作空間到 node_modules 資料夾。- 執行其他作業的指令 (test、exec、publish 等) 將在根專案上運作,除非workspace 設定中指定一個或多個工作空間。

此值不會匯出到子程序的環境中。

include-workspace-root

  • 預設值:false
  • 類型:布林值

當為指令啟用工作空間時,包含工作空間根目錄。

如果為 false,透過 workspace 設定指定個別工作區,或透過 workspaces 旗標指定所有工作區時,npm 將僅在指定工作區上執行,而不在根目錄專案上執行。

此值不會匯出到子程序的環境中。

package-lock-only

  • 預設值:false
  • 類型:布林值

如果設定為 true,目前的作業將只使用 package-lock.json,忽略 node_modules

對於 update,這表示將只更新 package-lock.json,而不是檢查 node_modules 並下載相依項。

對於 list,這表示輸出將根據 package-lock.json 所描述的樹狀結構,而不是 node_modules 的內容。

expect-results

  • 預設值:null
  • 類型:null 或布林值

告知 npm 是否預期命令會產生結果。可以是 true(預期有些結果)或 false(預期沒有結果)。

此設定無法與下列設定搭配使用:expect-result-count

expect-result-count

  • 預設值:null
  • 類型:null 或數字

告知預期命令會產生特定數量的結果。

此設定無法與下列設定搭配使用:expect-results

另請參閱