簡體   English   中英

NPM:為什么要安裝這個包?

[英]NPM: Why is this package installed?

如何確定安裝特定軟件包的原因? 換句話說,什么包依賴於這個包?

有問題的包是 babelify。 npm ls在頂層顯示它,但它沒有包含在 package.json 的任何地方。

使用npm ls列出已安裝的包並查看給定包的依賴關系圖,例如:

> npm ls core-js

my_module /path/to/my_module>
└─┬ pug@2.0.4
  └─┬ pug-code-gen@2.0.2
    └─┬ constantinople@3.1.2
      └─┬ babel-types@6.26.0
        └─┬ babel-runtime@6.26.0
          └── core-js@2.6.10

正如您提到的, npm ls顯示包及其依賴項:

> npm ls leveldown
appless@5.0.0 C:\Users\mikem\Code\appless
`-- @architect/architect@5.7.0
  `-- dynalite@2.2.0
    `-- UNMET OPTIONAL DEPENDENCY leveldown@4.0.2

如果npm ls在頂層顯示它,並且它不是頂層package.json的依賴項,則它可能以前是必需的,現在不再使用。

使用npm prune刪除未使用的包

有一個名為npm-why的模塊,用於識別安裝包的原因。

當然,如果你使用yarn ,你有一個內置命令yarn why

npm explain <package name>就是你要找的。 它通過顯示“自下而上”視圖解釋了為什么包在您的 node_modules 文件夾中。 在此處查看文檔

如果找不到requireimport ,請嘗試查看子package.json以查看還有誰需要它。

(注意: find需要 Linux/macOS,這不適用於 Windows)

find . -name package.json -exec grep -l babelify /dev/null {} \\;

./node_modules/browserify-zlib/package.json
./node_modules/cssnext/package.json
./node_modules/cypress/dist/Cypress.app/Contents/Resources/app/packages/reporter/package.json
./node_modules/cypress/dist/Cypress.app/Contents/Resources/app/packages/server/node_modules/@cypress/browserify-preprocessor/package.json
./node_modules/cypress/dist/Cypress.app/Contents/Resources/app/packages/server/node_modules/async/package.json
./node_modules/cypress/dist/Cypress.app/Contents/Resources/app/packages/server/node_modules/babel-core/package.json
./node_modules/cypress/dist/Cypress.app/Contents/Resources/app/packages/server/node_modules/babelify/package.json
./node_modules/cypress/dist/Cypress.app/Contents/Resources/app/packages/server/node_modules/getos/node_modules/async/package.json
./node_modules/cypress/dist/Cypress.app/Contents/Resources/app/packages/server/node_modules/object-assign/package.json
./node_modules/cypress/dist/Cypress.app/Contents/Resources/app/packages/server/node_modules/watchify/node_modules/browserify-zlib/package.json
./node_modules/cypress/dist/Cypress.app/Contents/Resources/app/packages/server/package.json
./node_modules/eslint/package.json
./node_modules/extract-text-webpack-plugin/node_modules/async/package.json
./node_modules/getos/node_modules/async/package.json
./node_modules/postcss-modules-extract-imports/package.json
./node_modules/postcss-modules-scope/package.json
./node_modules/webpack/node_modules/async/package.json

我的單線,基於其他答案: npm ls | grep -C 10 PACKAGE npm ls | grep -C 10 PACKAGE

將 PACKAGE 替換為您要查找的包。 這比其他建議更簡單、更快。 貝殼是你的朋友,朋友們!

分解/說明

  • npm ls - 如上所述,這會打印您的應用程序的依賴樹表示。
  • | - *nix shell 的秘方。 這會將輸出發送到下一個程序,而不是打印它。
  • grep [...] PACKAGE - 搜索字符串“PACKAGE”(這實際上是正則表達式,但這無關緊要。)
  • -C 10 - 這告訴 grep 在匹配行周圍打印 10 條額外的行。 如果沒有這個,grep 只會打印找到 PACKAGE 的行。 增加此數字可為您提供更多背景信息。 如果-C在您的系統上不起作用(不太常見的 linux 版本),請嘗試使用-B 10 -A 10代替。 這是做同樣事情的更冗長的方式:“前 10 行,后 10 行”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM