間接的に依存しているライブラリに脆弱性がある場合等、自身のpackgae.jsonのどのpackageを更新すれば解消できるか等を調べたい場合、lockファイルを頑張って確認するのは大変です。
package manager(yarn, npm)の機能を使うと割と簡単に調べられたのでメモ🗒
yarn
yarn
だとwhy
という機能が提供されていています。
使い方は簡単でyarn why package-name
で実行すると以下のように依存関係を出力してくれます。
$ yarn why minimist yarn why v1.22.17 [1/4] 🤔 Why do we have the module "minimist"...? [2/4] 🚚 Initialising dependency graph... [3/4] 🔍 Finding dependency... [4/4] 🚡 Calculating file sizes... => Found "minimist@1.2.6" info Reasons this module exists - "@lhci#cli#chrome-launcher#mkdirp" depends on it - Hoisted from "@lhci#cli#chrome-launcher#mkdirp#minimist" - Hoisted from "@lhci#cli#update-notifier#latest-version#package-json#registry-auth-token#rc#minimist" info Disk size without dependencies: "104KB" info Disk size with unique dependencies: "104KB" info Disk size with transitive dependencies: "104KB" info Number of shared dependencies: 0 ✨ Done in 0.20s.
info Reasons this module exists
で最上位に記載されているpackageが自身が直接依存している(package.jsonに記載している)packageということですね。便利!
npm
npm
ではnpm ls
という機能が提供されています。
こちらも使い方は簡単でnpm ls package-name
で依存関係のツリーを表示してくれます。
$ npm ls kind-of my_package@1.0.0 /foo/bar └─┬ webpack-cli@4.4.0 └─┬ webpack-merge@5.7.3 └─┬ clone-deep@4.0.1 ├── kind-of@6.0.3 └─┬ shallow-clone@3.0.1 └── kind-of@6.0.3 deduped
my_packageを除いた最上位に記載されているpackageが自身が直接依存している(package.jsonに記載している)packageということですね。便利!
おまけ: RubyGems
bundle exec gem dependency -R
で調べられるっぽい。リストが間接的に依存しているもので、Used by
にinstall済みで直接 nokogiri
に依存しているライブラリっぽい(?)、引数にはgem名を表す正規表現を指定する。完全一致にしたい場合には^foo$
で完全一致に該当する正規表現を設定してあげれば良さそう。
bundle exec gem dependency -R "^nokogiri$" Gem nokogiri-1.13.3-arm64-darwin bundler (~> 2.2, development) hoe-markdown (~> 1.4, development) minitest (~> 5.15, development) minitest-reporters (~> 1.4, development) racc (~> 1.4) rake (~> 13.0, development) rake-compiler (= 1.1.7, development) rake-compiler-dock (~> 1.2, development) rdoc (~> 6.3, development) rexical (~> 1.0.7, development) rubocop (~> 1.23, development) rubocop-minitest (~> 0.17, development) rubocop-performance (~> 1.12, development) rubocop-rake (~> 0.6, development) rubocop-shopify (~> 2.3, development) ruby_memcheck (~> 1.0, development) simplecov (~> 0.21, development) Used by rails-dom-testing-2.0.3 (nokogiri (>= 1.6)) loofah-2.15.0 (nokogiri (>= 1.5.9)) actiontext-7.0.2.3 (nokogiri (>= 1.8.5)) xpath-3.2.0 (nokogiri (~> 1.8)) capybara-3.36.0 (nokogiri (~> 1.8)) reverse_markdown-2.1.1 (nokogiri (>= 0))