简体   繁体   English

npm list 如何知道哪些是直接依赖?

[英]How does npm list know which ones are direct dependencies?

Get into a new directly, after typing those three commands:输入这三个命令后,直接进入一个新的:

npm install underscore
npm install lodash
npm install express

I get a node_modules directory with many packages:我得到一个包含许多包的node_modules目录:

$ ls node_modules

accepts              cookie-signature  encodeurl     forwarded    lodash             mime-db      parseurl        send            underscore
array-flatten        debug             escape-html   fresh        media-typer        mime-types   path-to-regexp  serve-static    unpipe
content-disposition  depd              etag          http-errors  merge-descriptors  ms           proxy-addr      setprototypeof  utils-merge
content-type         destroy           express       inherits     methods            negotiator   qs              statuses        vary
cookie               ee-first          finalhandler  ipaddr.js    mime               on-finished  range-parser    type-is

While using npm list , I can get a tree strcture:在使用npm list ,我可以得到一个树结构:

$ npm list
/tmp/play/npm
├─┬ express@4.14.0
│ ├─┬ accepts@1.3.3
│ │ ├─┬ mime-types@2.1.13
│ │ │ └── mime-db@1.25.0
│ │ └── negotiator@0.6.1
│ ├── array-flatten@1.1.1
│ ├── content-disposition@0.5.1
│ ├── content-type@1.0.2
│ ├── cookie@0.3.1
│ ├── cookie-signature@1.0.6
│ ├─┬ debug@2.2.0
│ │ └── ms@0.7.1
│ ├── depd@1.1.0
│ ├── encodeurl@1.0.1
│ ├── escape-html@1.0.3
│ ├── etag@1.7.0
│ ├─┬ finalhandler@0.5.0
│ │ ├── statuses@1.3.1
│ │ └── unpipe@1.0.0
│ ├── fresh@0.3.0
│ ├── merge-descriptors@1.0.1
│ ├── methods@1.1.2
│ ├─┬ on-finished@2.3.0
│ │ └── ee-first@1.1.1
│ ├── parseurl@1.3.1
│ ├── path-to-regexp@0.1.7
│ ├─┬ proxy-addr@1.1.2
│ │ ├── forwarded@0.1.0
│ │ └── ipaddr.js@1.1.1
│ ├── qs@6.2.0
│ ├── range-parser@1.2.0
│ ├─┬ send@0.14.1
│ │ ├── destroy@1.0.4
│ │ ├─┬ http-errors@1.5.1
│ │ │ ├── inherits@2.0.3
│ │ │ └── setprototypeof@1.0.2
│ │ └── mime@1.3.4
│ ├── serve-static@1.11.1
│ ├─┬ type-is@1.6.14
│ │ └── media-typer@0.3.0
│ ├── utils-merge@1.0.0
│ └── vary@1.1.0
├── lodash@4.17.2
└── underscore@1.8.3

My question is: from all those dependencies, how does npm list know which ones are my direct dependencies such as undersocre , lodash and express ?我的问题是:从所有这些依赖项中, npm list如何知道哪些是我的直接依赖项,例如undersocrelodashexpress

note: I don't have a package.json file.注意:我没有package.json文件。

It builds the list on the basis of the dependencies of the modules.它根据模块的依赖关系构建列表。 The dependencies of the modules are specified in the package.json of each module in the dependencies field.模块的依赖项在每个模块的package.json中的dependencies字段中指定。 When you install a module npm adds some additional fields to the module's package.json and one of those is the field _requiredBy to store the dependency link in the other direction as well.当你安装一个模块时, npm会向模块的package.json添加一些额外的字段,其中一个是字段_requiredBy来存储另一个方向的依赖链接。 If you run the npm list command it goes through all the modules and reads the _requiredBy field in package.json of each module.如果您运行npm list命令,它会遍历所有模块并读取每个模块的package.json中的_requiredBy字段。

If you install a module directly without saving it to your package.json , npm adds #USER to the _requiredBy field to signify that you manually installed it and it is not just a dependency of the other modules.如果您直接安装模块而不将其保存到package.jsonnpm会将#USER添加到_requiredBy字段以表示您手动安装了它,并且它不仅仅是其他模块的依赖项。 Then npm list shows that module in the root of the tree as well.然后npm list也会在树的根部显示该模块。

你可以使用这个命令:

npm list --depth=0 2>/dev/null

npm list command will print to stdout all the versions of packages that are installed, as well as their dependencies, in a tree-structure. npm list命令将以树状结构将已安装软件包的所有版本及其依赖项打印到标准输出。

So you have only installed three packages所以你只安装了三个包

npm install underscore
npm install lodash
npm install express

All other packages are dependency for express package所有其他包都是express包的依赖项

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

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