以下是 在Vue項目中查詢所有版本號為 1.1.9
的依賴包名 的具體方法,支持 npm/yarn/pnpm 等主流工具:
一、使用 npm
1. 直接過濾依賴樹
npm ls --depth=0 | grep "1.1.9"
- 說明:
npm ls --depth=0
:僅顯示直接依賴(不包含子依賴)。grep "1.1.9"
:過濾出版本號包含1.1.9
的包。
2. 遞歸查找所有依賴(包括子依賴)
npm ls | grep "1.1.9"
- 注意:此命令會列出所有層級的依賴,輸出可能較多,需結合
grep
精確匹配。
3. 精確匹配版本號 1.1.9
npm ls | grep -E "1.1.9$"
- 使用正則表達式
1.1.9$
確保版本號嚴格匹配(避免1.1.90
或1.1.9-beta
等干擾)。
二、使用 yarn
1. 列出所有依賴并過濾
yarn list --depth=0 | grep "1.1.9"
- 說明:
yarn list --depth=0
:僅顯示直接依賴。grep "1.1.9"
:過濾版本號。
2. 遞歸查找所有依賴
yarn list | grep "1.1.9"
3. 精確匹配版本號
yarn list | grep -E "1.1.9$"
三、使用 pnpm
1. 列出依賴并過濾
pnpm ls --depth=0 | grep "1.1.9"
2. 遞歸查找
pnpm ls | grep "1.1.9"
3. 精確匹配
pnpm ls | grep -E "1.1.9$"
四、高級方法:JSON格式解析
1. 生成依賴樹的JSON文件
# npm
npm ls --json > dependencies.json# yarn
yarn list --json > dependencies.json# pnpm
pnpm ls --json > dependencies.json
2. 使用 jq
工具篩選
# 安裝jq(若未安裝)
sudo apt-get install jq # Ubuntu/Debian
brew install jq # macOS# 篩選版本號為1.1.9的包
cat dependencies.json | jq 'select(.version == "1.1.9") | .name'
五、注意事項
-
依賴層級:
--depth=0
:僅顯示直接依賴。- 無參數:遞歸顯示所有依賴(包括子依賴)。
-
排除開發依賴:
npm ls --production | grep "1.1.9"
-
處理模糊匹配:
- 若版本號可能帶有后綴(如
1.1.9-beta
),需用正則精確匹配:grep -E "1.1.9(\s|$)" # 匹配 `1.1.9` 后跟空格或行尾
- 若版本號可能帶有后綴(如
六、示例輸出
$ npm ls | grep -E "1.1.9$"
└─┬ package-a@1.1.9
└─┬ package-b@1.1.9
└─┬ package-c@1.1.9
七、可視化工具輔助
若需更直觀的依賴關系圖,可結合以下工具:
-
madge
:madge --jsonp ./node_modules | madge --image dependencies.png
-
depcheck
:depcheck --duplicates --unused
通過上述方法,可以快速定位項目中所有版本為 1.1.9
的依賴包,并排查潛在的版本沖突或升級需求。