简体   繁体   English

NPM:为什么要安装这个包?

[英]NPM: Why is this package installed?

How do I determine why a particular package is installed?如何确定安装特定软件包的原因? In other words, what package(s) depend on this package?换句话说,什么包依赖于这个包?

The package in question is babelify.有问题的包是 babelify。 npm ls shows it at the top level, but it is not included in package.json anywhere. npm ls在顶层显示它,但它没有包含在 package.json 的任何地方。

Use npm ls to list installed packages and see the dependency graph of a given package eg:使用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

As you mention, npm ls shows packages and their dependencies:正如您提到的, 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

If npm ls shows it at the top level, and it is not a dependency in the top level package.json , it's likely was previously required and is now no longer used.如果npm ls在顶层显示它,并且它不是顶层package.json的依赖项,则它可能以前是必需的,现在不再使用。

Use npm prune to remove the unused package .使用npm prune删除未使用的包

There's a module called npm-why which identifies why a package has been installed.有一个名为npm-why的模块,用于识别安装包的原因。

Of course, if you're using yarn , you have a built-in command yarn why .当然,如果你使用yarn ,你有一个内置命令yarn why

npm explain <package name> is what you're looking for. npm explain <package name>就是你要找的。 It explains why a package is in your node_modules folder by showing a "bottoms up" view.它通过显示“自下而上”视图解释了为什么包在您的 node_modules 文件夹中。 See docs here在此处查看文档

If you can't find a require or import , try looking at child package.json s to see who else needs it.如果找不到requireimport ,请尝试查看子package.json以查看还有谁需要它。

(Note: find requires Linux/macOS, this won't work on Windows) (注意: 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

My one-liner, based on other answers: npm ls | grep -C 10 PACKAGE我的单线,基于其他答案: npm ls | grep -C 10 PACKAGE npm ls | grep -C 10 PACKAGE

Replace PACKAGE with the package you're looking for.将 PACKAGE 替换为您要查找的包。 This is simpler and faster than other suggestions.这比其他建议更简单、更快。 The shell is your friend, friends!贝壳是你的朋友,朋友们!

Breakdown/Explanation分解/说明

  • npm ls - As explained above, this prints the dependency tree representation of your app. npm ls - 如上所述,这会打印您的应用程序的依赖树表示。
  • | - The secret sauce of *nix shells. - *nix shell 的秘方。 This sends the output to the next program, instead of printing it.这会将输出发送到下一个程序,而不是打印它。
  • grep [...] PACKAGE - Search for the string "PACKAGE" (This is actually regex, but that's irrelevant.) grep [...] PACKAGE - 搜索字符串“PACKAGE”(这实际上是正则表达式,但这无关紧要。)
  • -C 10 - This tells grep to print 10 extra lines around the matching lines. -C 10 - 这告诉 grep 在匹配行周围打印 10 条额外的行。 Without this, grep would only print the lines were PACKAGE was found.如果没有这个,grep 只会打印找到 PACKAGE 的行。 Increasing this number gives you more context.增加此数字可为您提供更多背景信息。 If -C doesn't work on your system (less common linux versions), try using -B 10 -A 10 instead.如果-C在您的系统上不起作用(不太常见的 linux 版本),请尝试使用-B 10 -A 10代替。 It's a more verbose way of doing the same thing: "10 lines before, 10 lines after."这是做同样事情的更冗长的方式:“前 10 行,后 10 行”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM