npm package.json文件中的依赖关系,devDependencies和peerDependencies之间有什么区别?

本文翻译自:What's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?

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


#1楼

参考:https://stackoom.com/question/1HCQM/npm-package-json文件中的依赖关系-devDependencies和peerDependencies之间有什么区别


#2楼

There are some modules and packages only necessary for development, which are not needed in production. 有些模块和软件包仅对于开发是必需的,而在生产中则不需要。 Like it says it in the documentation : 就像它在文档中说的那样:

If someone is planning on downloading and using your module in their program, then they probably don't want or need to download and build the external test or documentation framework that you use. 如果有人计划在其程序中下载和使用您的模块,则他们可能不想或不需要下载并构建您使用的外部测试或文档框架。 In this case, it's best to list these additional items in a devDependencies hash. 在这种情况下,最好在devDependencies哈希中列出这些其他项。


#3楼

例如,mocha通常是devDependency,因为在生产中不需要测试,而express是依赖项。


#4楼

Summary of important behavior differences: 重要行为差异摘要:

  • dependencies are installed on both: dependencies都安装在两个上:

    • npm install from a directory that contains package.json 从包含package.json的目录npm install
    • npm install $package on any other directory npm install $package在任何其他目录上npm install $package
  • devDependencies are: devDependencies是:

    • also installed on npm install on a directory that contains package.json , unless you pass the --production flag (go upvote Gayan Charith's answer ). 也安装在npm install在包含package.json的目录上,除非您传递--production标志(请赞成Gayan Charith的answer )。
    • not installed on npm install "$package" on any other directory, unless you give it the --dev option. 没有安装在npm install "$package"上,除非您给它提供--dev选项,否则请在其他任何目录上npm install "$package"
    • are not installed transitively. 不能临时安装。
  • peerDependencies : 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. 3.0之前的版本:如果缺少则始终安装,如果不同的依赖项使用多个不兼容的依赖项版本,则会引发错误。
    • expected to start on 3.0 (untested): give a warning if missing on npm install , and you have to solve the dependency yourself manually. 预期从3.0开始 (未试用):如果npm install缺少该警告,则发出警告,您必须手动解决依赖关系。 When running, if the dependency is missing, you get an error (mentioned by @nextgentech ) 运行时,如果缺少依赖项,则会出现错误( @nextgentech提到)
  • Transitivity (mentioned by Ben Hutchison ): 可传递性(由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. dependencies是通过传递方式安装的:如果A需要B,而B需要C,则C将被安装,否则B无法工作,A也将无法工作。

    • devDependencies is not installed transitively. devDependencies不会devDependencies安装。 Eg we don't need to test B to test A, so B's testing dependencies can be left out. 例如,我们不需要测试B就可以测试A,因此可以省去B的测试依赖项。

Related options not discussed here: 此处未讨论的相关选项:

devDependencies devDependencies

dependencies are required to run, devDependencies only to develop, eg: unit tests, CoffeeScript to JavaScript transpilation, minification, ... 需要运行dependencies ,仅需要开发devDependencies才能开发,例如:单元测试,CoffeeScript到JavaScript的代码转换,缩小,...

If you are going to develop a package, you download it (eg via git clone ), go to its root which contains package.json , and run: 如果要开发一个软件包,请下载它(例如,通过git clone ),转到包含package.json根目录,然后运行:

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. 由于您拥有实际的源代码,因此很显然要开发它,因此默认情况下,还将同时安装dependencies (因为您必须运行以进行开发)和devDependency依赖项。

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 . 在那种情况下,您通常不需要开发依赖项,因此您只需要使用包就可以: 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: 如果您确实想在这种情况下安装开发包,则可以将dev配置选项设置为true ,可能从命令行将其设置为:

npm install "$package" --dev

The option is false by default since this is a much less common case. 默认情况下,此选项为false ,因为这种情况不太常见。

peerDependencies peerDependencies

(Tested before 3.0) (在3.0之前测试)

Source: https://nodejs.org/en/blog/npm/peer-dependencies/ 资料来源: 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. 使用常规的依赖项,您可以具有多个版本的依赖项:只需将其安装在依赖项的node_modules中即可。

Eg if dependency1 and dependency2 both depend on dependency3 at different versions the project tree will look like: 例如,如果dependency1dependency2都在不同版本上都依赖dependency3 ,则项目树将如下所示:

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. 用户只能直接调用主机,因此必须有一个单独的版本。

Eg if dependency1 and dependency2 peer depend on dependency3 , the project tree will look like: 例如,如果dependency1dependency2对等依赖于dependency3 ,则项目树将如下所示:

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

This happens even though you never mention dependency3 in your package.json file. 即使您从未在package.json文件中提及dependency3 ,也会发生这种情况。

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. 对等依赖关系的一个典型示例是Grunt,主机及其插件。

For example, on a Grunt plugin like https://github.com/gruntjs/grunt-contrib-uglify , you will see that: 例如,在https://github.com/gruntjs/grunt-contrib-uglify之类的Grunt插件上,您将看到:

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

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. 然后,当用户将使用插件,他会隐需要从插件Gruntfile通过添加grunt.loadNpmTasks('grunt-contrib-uglify')线,但它的grunt ,用户将直接调用。

This would not work then if each plugin required a different Grunt version. 如果每个插件都需要不同的Grunt版本,则无法使用。

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. 我可能只了解它,因为我对Ruby bundler有所了解。

The key line is: 关键是:

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. 这些东西将在从软件包根目录执行npm link或npm install时安装,并且可以像任何其他npm配置参数一样进行管理。 See npm-config(7) for more on the topic. 有关该主题的更多信息,请参见npm-config(7)。

And then under npm-config(7) find dev : 然后在npm-config(7)下找到dev

Default: false
Type: Boolean

Install dev-dependencies along with packages.

#5楼

如果您不想安装devDependencies,则可以使用npm install --production


#6楼

To save a package to package.json as dev dependencies: 要将软件包作为dev依赖项保存到package.json

npm install "$package" --save-dev

When you run npm install it will install both devDependencies and dependencies . 当您运行npm install ,它将同时安装devDependenciesdependencies To avoid install devDependencies run: 为了避免安装devDependencies运行:

npm install --production
发布了0 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/105191882
今日推荐