npm package.json 文件中的依赖项、devDependencies 和 peerDependencies 之间有什么区别? [英] What's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?

查看:27
本文介绍了npm package.json 文件中的依赖项、devDependencies 和 peerDependencies 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本文档 对我的问题的回答很差.我不明白这些解释.有人可以用简单的词来说吗?如果很难选择简单的单词,可以举个例子吗?

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 还添加了 peerDependencies,这是密切相关的,可能会引起混淆.

EDIT also added peerDependencies, which is closely related and might cause confusion.

推荐答案

重要行为差异总结:

  • dependencies are installed on both:

  • npm install 来自包含 package.json
  • 的目录
  • npm install $package 在任何其他目录
  • npm install from a directory that contains package.json
  • npm install $package on any other directory

devDependencies 是:

  • 也安装在包含 package.json 的目录上的 npm install 上,除非你传递了 --production 标志(去投票 Gayan Charith 的回答).
  • 未安装在 npm install "$package" 任何其他目录上,除非您给它--dev 选项.
  • 不是以传递方式安装的.
  • also installed on npm install on a directory that contains package.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.

peerDependencies:

  • 3.0 之前:如果缺少则始终安装,如果不同的依赖项使用多个不兼容的依赖项版本,则会引发错误.
  • 预计在 3.0 开始(未测试):发出警告如果 npm install 缺失,你必须自己手动解决依赖.运行时,如果缺少依赖项,则会出现错误(由 @nextgentech 提及)这很好地解释了:https://flaviocopes.com/npm-peer-dependencies/
  • 在第 7 版中 peerDependencies 是自动安装,除非存在无法自动解决的上游依赖项冲突
  • 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) This explains it nicely: https://flaviocopes.com/npm-peer-dependencies/
  • in version 7 peerDependencies are automatically installed unless an upstream dependency conflict is present that cannot be automatically resolved

传递性(由 Ben Hutchison 提及):

  • dependencies 是传递性安装的:如果 A 需要 B,B 需要 C,则安装 C,否则,B 不能工作,A 也不能.

  • 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 不是可传递安装的.例如.我们不需要测试B来测试A,所以可以省去B的测试依赖.

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 npm
  • optionalDependencies (mentioned by Aidan Feldman)

dependencies 需要运行,devDependencies 仅用于开发,例如:单元测试、CoffeeScript 到 JavaScript 的转换、缩小……

dependencies are required to run, devDependencies only to develop, e.g.: unit tests, CoffeeScript to JavaScript transpilation, minification, ...

如果你要开发一个包,你下载它(例如通过git clone),转到包含package.json的根目录,然后运行:

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

既然你有实际的源代码,很明显你要开发它,所以默认情况下,dependencies(因为你当然必须运行才能开发)和devDependency 还安装了依赖项.

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"

在这种情况下,您通常不需要开发依赖项,因此您只需获取使用包所需的内容:dependencies.

In that case, you normally don't want the development dependencies, so you just get what is needed to use the package: dependencies.

如果你真的想在这种情况下安装开发包,你可以将 dev 配置选项设置为 true,可能从命令行如下:

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

该选项默认为 false,因为这是一种不太常见的情况.

The option is false by default since this is a much less common case.

(在 3.0 之前测试过)

(Tested before 3.0)

来源:https://nodejs.org/en/blog/npm/peer-依赖/

使用常规依赖项,您可以拥有多个版本的依赖项:它只是安装在依赖项的 node_modules 内.

With regular dependencies, you can have multiple versions of the dependency: it's simply installed inside the node_modules of the dependency.

例如如果 dependency1dependency2 在不同版本都依赖于 dependency3,项目树将如下所示:

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/

然而,插件是通常不需要其他包的包,在此上下文中称为 host.相反:

Plugins, however, are packages that normally don't require the other package, which is called the host in this context. Instead:

  • 插件需要主机
  • 插件提供了主机期望找到的标准接口
  • 只有宿主会被用户直接调用,所以它必须是单一版本.

例如如果 dependency1dependency2 peer 依赖于 dependency3,项目树将如下所示:

E.g. if dependency1 and dependency2 peer depend on dependency3, the project tree will look like:

root/node_modules/
                 |
                 +- dependency1/
                 |
                 +- dependency2/
                 |
                 +- dependency3 v1.0/

即使您从未在 package.json 文件中提及 dependency3,也会发生这种情况.

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.

对等依赖的典型示例是 Grunt、主机及其插件.

A prototypical example of peer dependencies is Grunt, the host, and its plugins.

例如,在像 https://github.com/gruntjs/grunt- 这样的 Grunt 插件上contrib-uglify,你会看到:

For example, on a Grunt plugin like https://github.com/gruntjs/grunt-contrib-uglify, you will see that:

  • grunt 是一个 peer-dependency
  • 唯一的 require('grunt')tests/ 下:它实际上并未被程序使用.
  • grunt is a peer-dependency
  • the only require('grunt') is under tests/: it's not actually used by the program.

然后,当用户使用插件时,他会通过添加grunt.loadNpmTasks('grunt-contrib-uglify')Gruntfile中隐式地要求插件> 行,但用户将直接调用 grunt.

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.

如果每个插件都需要不同的 Grunt 版本,这将不起作用.

This would not work then if each plugin required a different Grunt version.

我认为文档很好地回答了这个问题,也许您只是对节点/其他包管理器不够熟悉.我可能只了解它,因为我对 Ruby bundler 略知一二.

I think the documentation answers the question quite well, maybe you are just not familiar enough with node / other package managers. I probably only understand it because I know a bit about Ruby bundler.

关键是:

这些东西将在从包的根目录执行 npm link 或 npm install 时安装,并且可以像任何其他 npm 配置参数一样进行管理.有关该主题的更多信息,请参阅 npm-config(7).

These things will be installed when doing npm link or npm install from the root of a package and can be managed like any other npm configuration parameter. See npm-config(7) for more on the topic.

然后在 npm-config(7) 下找到 dev:

And then under npm-config(7) find dev:

Default: false
Type: Boolean

Install dev-dependencies along with packages.

这篇关于npm package.json 文件中的依赖项、devDependencies 和 peerDependencies 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆