2016-12-17 248 views
2

进入一个新的直接输入这三个命令后:npm列表如何知道哪些是直接依赖关系?

npm install underscore 
npm install lodash 
npm install express 

我得到一个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 

在使用npm list,我能得到一棵树谢灵运:

$ npm list 
/tmp/play/npm 
├─┬ [email protected] 
│ ├─┬ [email protected] 
│ │ ├─┬ [email protected] 
│ │ │ └── [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ ├── [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ ├── [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ ├── [email protected] 
│ │ ├─┬ [email protected] 
│ │ │ ├── [email protected] 
│ │ │ └── [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ └── [email protected] 
├── [email protected] 
└── [email protected] 

我的问题是:来自所有这些依赖关系,npm list知道哪些是我的直接依赖关系,如undersocre,lodashexpress

注意:我没有package.json文件。

回答

1

它构建模块的依赖关系的基础上,该列表的依赖。模块的相关性在dependencies字段中的每个模块的package.json中指定。当您安装模块npm时,会在模块的package.json中添加一些附加字段,其中一个字段为_requiredBy也用于在另一方向存储依赖关系链接。如果运行npm list命令,它会遍历所有模块,并读取每个模块的package.json中的_requiredBy字段。

如果直接安装一个模块,而不将其保存到你的package.jsonnpm增加#USER_requiredBy领域,以表示手动安装它,它不只是一个其它模块的依赖性。然后npm list也在树的根目录中显示该模块。

0

npm list命令将打印到stdout所安装的所有版本的软件包以及它们的依赖项。

所以,你只安装了三个包

npm install underscore 
npm install lodash 
npm install express 

其它所有包都是express