问题描述
此文档很难回答我的问题.我不明白那些解释.有人可以用简单的话说吗?如果很难选择简单的单词,也许还会举一些例子?
编辑也添加了peerDependencies
,它与之关系密切,可能会引起混淆.
重要行为差异摘要:
-
dependencies
都安装在两个位置上:- 包含
package.json
的目录中的 -
npm install
-
npm install $package
在任何其他目录上
- 还安装在包含
package.json
的目录的npm install
上,除非您传递--production
标志(请投票 Gayan查理斯的答案). - 未安装在任何其他目录的
npm install "$package"
上,除非您赋予它--dev
选项. - 不是临时安装的.
- 3.0之前的版本:如果丢失,则始终安装,如果不同的依赖项将使用多个不兼容的依赖项版本,则会引发错误.
- 预计从3.0开始(未试用):发出警告如果
npm install
上缺少,则必须自己解决依赖关系.运行时,如果缺少依赖项,则会出现错误(由 @nextgentech 提及)
传递性(由 Ben Hutchison提到):
-
dependencies
是以过渡方式安装的:如果A需要B,而B需要C,则必须安装C,否则B无法工作,A也将无法工作. -
devDependencies
不是临时安装的.例如.我们不需要测试B就可以测试A,因此可以省去B的测试依赖项.
此处未讨论相关选项:
-
bundledDependencies
,将在以下问题上进行讨论: -
optionalDependencies
(提到)
devDependencies
dependencies
是运行所必需的,devDependencies
仅用于进行开发,例如:单元测试,CoffeeScript到JavaScript的代码转换,缩小,...
如果要开发软件包,请下载该软件包(例如,通过git clone
进行下载),转到包含package.json
的根目录,然后运行:
npm install
既然您有实际的来源,很显然您想开发它,因此默认情况下,还将同时安装dependencies
(因为您必须运行以进行开发)和devDependency
依赖项. /p>
但是,如果您只是希望安装软件包以使用它的最终用户,则可以从任何目录进行操作:
npm install "$package"
在那种情况下,您通常不需要开发依赖项,因此您只需要使用软件包即可:dependencies
.
如果您确实要在这种情况下安装开发包,则可以将dev
配置选项设置为true
,可能在命令行中将其设置为:
npm install "$package" --dev
默认情况下该选项为false
,因为这种情况不太常见.
peerDependencies
(在3.0之前测试)
来源: https://nodejs.org/en/blog/npm/peer -dependencies/
对于常规依赖项,您可以具有多个版本的依赖项:只需将其安装在依赖项的node_modules
内即可.
例如如果dependency1
和dependency2
都依赖于dependency3
的不同版本,则项目树将如下所示:
root/node_modules/
|
+- dependency1/node_modules/
| |
| +- dependency3 v1.0/
|
|
+- dependency2/node_modules/
|
+- dependency3 v2.0/
但是,
插件是通常不需要其他软件包的软件包,在这种情况下,该软件包称为 host .相反:
- 主机需要 插件
- 插件提供了主机希望找到的标准接口
- 只有主机会被用户直接调用,因此必须有一个单独的版本.
例如如果dependency1
和dependency2
对等项依赖于dependency3
,则项目树将如下所示:
root/node_modules/
|
+- dependency1/
|
+- dependency2/
|
+- dependency3 v1.0/
即使您从未在package.json
文件中提及dependency3
,也会发生这种情况.
我认为这是控制反转设计模式的一个实例.
对等依赖性的典型示例是Grunt,主机及其插件.
例如,在类似 https://github.com/gruntjs/grunt-的Grunt插件上contrib-uglify ,您将看到:
-
grunt
是peer-dependency
- 唯一的
require('grunt')
在tests/
下:它实际上未被程序使用.
然后,当用户使用插件时,他将通过添加grunt.loadNpmTasks('grunt-contrib-uglify')
行隐式要求Gruntfile
中的插件,但用户直接调用grunt
即可.
如果每个插件都需要不同的Grunt版本,则此方法将无效.
手册
我认为文档可以很好地回答这个问题,也许您对节点/其他包管理器还不够熟悉.我可能只了解它,因为我对Ruby bundler有所了解.
关键行是:
然后在npm-config(7)下找到dev
:
Default: false
Type: Boolean
Install dev-dependencies along with packages.
This documentation answers my question very poorly. I didn't understand those explanations. Can someone say in simpler words? Maybe with examples if it's hard to choose simple words?
EDIT also added peerDependencies
, which is closely related and might cause confusion.
Summary of important behavior differences:
dependencies
are installed on both:npm install
from a directory that containspackage.json
npm install $package
on any other directory
devDependencies
are:- also installed on
npm install
on a directory that containspackage.json
, unless you pass the--production
flag (go upvote Gayan Charith's answer). - not installed on
npm install "$package"
on any other directory, unless you give it the--dev
option. - are not installed transitively.
- also installed on
- before 3.0: are always installed if missing, and raise an error if multiple incompatible versions of the dependency would be used by different dependencies.
- expected to start on 3.0 (untested): give a warning if missing on
npm install
, and you have to solve the dependency yourself manually. When running, if the dependency is missing, you get an error (mentioned by @nextgentech)
Transitivity (mentioned by Ben Hutchison):
dependencies
are installed transitively: if A requires B, and B requires C, then C gets installed, otherwise, B could not work, and neither would A.devDependencies
is not installed transitively. E.g. we don't need to test B to test A, so B's testing dependencies can be left out.
Related options not discussed here:
bundledDependencies
which is discussed on the following question: Advantages of bundledDependencies over normal dependencies in NPMoptionalDependencies
(mentioned by Aidan Feldman)
devDependencies
dependencies
are required to run, devDependencies
only to develop, e.g.: unit tests, CoffeeScript to JavaScript transpilation, minification, ...
If you are going to develop a package, you download it (e.g. via git clone
), go to its root which contains package.json
, and run:
npm install
Since you have the actual source, it is clear that you want to develop it, so by default, both dependencies
(since you must, of course, run to develop) and devDependency
dependencies are also installed.
If however, you are only an end user who just wants to install a package to use it, you will do from any directory:
npm install "$package"
In that case, you normally don't want the development dependencies, so you just get what is needed to use the package: dependencies
.
If you really want to install development packages in that case, you can set the dev
configuration option to true
, possibly from the command line as:
npm install "$package" --dev
The option is false
by default since this is a much less common case.
peerDependencies
(Tested before 3.0)
Source: https://nodejs.org/en/blog/npm/peer-dependencies/
With regular dependencies, you can have multiple versions of the dependency: it's simply installed inside the node_modules
of the dependency.
E.g. if dependency1
and dependency2
both depend on dependency3
at different versions the project tree will look like:
root/node_modules/
|
+- dependency1/node_modules/
| |
| +- dependency3 v1.0/
|
|
+- dependency2/node_modules/
|
+- dependency3 v2.0/
Plugins, however, are packages that normally don't require the other package, which is called the host in this context. Instead:
- plugins are required by the host
- plugins offer a standard interface that the host expects to find
- only the host will be called directly by the user, so there must be a single version of it.
E.g. if dependency1
and dependency2
peer depend on dependency3
, the project tree will look like:
root/node_modules/
|
+- dependency1/
|
+- dependency2/
|
+- dependency3 v1.0/
This happens even though you never mention dependency3
in your package.json
file.
I think this is an instance of the Inversion of Control design pattern.
A prototypical example of peer dependencies is Grunt, the host, and its plugins.
For example, on a Grunt plugin like https://github.com/gruntjs/grunt-contrib-uglify, you will see that:
grunt
is apeer-dependency
- the only
require('grunt')
is undertests/
: it's not actually used by the program.
Then, when the user will use a plugin, he will implicitly require the plugin from the Gruntfile
by adding a grunt.loadNpmTasks('grunt-contrib-uglify')
line, but it's grunt
that the user will call directly.
This would not work then if each plugin required a different Grunt version.
Manual
I think the documentation answers the question quite well, maybe you are not just familiar enough with node / other package managers. I probably only understand it because I know a bit about Ruby bundler.
The key line is:
And then under npm-config(7) find dev
:
Default: false
Type: Boolean
Install dev-dependencies along with packages.
这篇关于npm package.json文件中的依赖关系,devDependencies和peerDependencies之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!