本文翻译自: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: 此处未讨论的相关选项:

  • bundledDependencies which is discussed on the following question: Advantages of bundledDependencies over normal dependencies in NPM bundledDependencies其上以下问题讨论: 超过在正常NPM依赖bundledDependencies的优点
  • optionalDependencies (mentioned by Aidan Feldman ) optionalDependencies ( 由Aidan Feldman提到)

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: BooleanInstall 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

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

  1. package.json文件中,^和~的区别

    package.json文件中,^和~的区别 偶然间遇到这样一个问题, package.json文件中,^和~的区别,查阅后,顺便做个笔记 package.json文件里面,显示的是项目所依赖的插件和 ...

  2. npm package.json 文件基础知识

    前言:package.json文件是Node.js生态系统的核心,是了解和使用Node.js,npm甚至现代JavaScript的基本部分. 它是应用程序.模块.npm引入包等的清单. name 是p ...

  3. npm package.json文件解读

    每个Nodejs项目的根目录下面,一般都会有一个package.json文件.该文件可以由npm init生成,定义了项目所需要的各种模块,以及项目的配置信息(比如名称.版本.许可证等元数据). pa ...

  4. [转]npm生成的package.json文件中依赖项版本号前的波浪号(~)和插入符号(^)是啥意思?

    package.json一般也就长这么模样: {"name": "","description": "","v ...

  5. npm生成的package.json文件中依赖项版本号前的波浪号(~)和插入符号(^)是啥意思?

    package.json一般也就长这么模样: {"name": "","description": "","v ...

  6. makefile文件中的依赖关系理解

    首先,假设当前工程目录为prj/,该目录下有6个文件,分别是:main.c.abc.c.xyz.c.abc.h.xyz.h和Makefile.其中main.c包含头文件abc.h和xyz.h,abc. ...

  7. package.json文件中的main属性使用

    main属性主要使用在引用或者开发某个依赖包的时候需要此属性的支持,不然工程中无法用import导入依赖包: 不使用main属性的话我们可能需要这样写引用:require("some-mod ...

  8. npm 包与模块关系 下载使用包 init命令 package.json文件 node_modules文件夹 全局安装包和本地安装包 开发依赖和生产依赖

    npm 了解npm npm 全称Node Package Manager(node 包管理器),它的诞生是为了解决 Node 中第三方包共享的问题. npm 不需要单独安装.在安装Node的时候,会连 ...

  9. 模块加载及第三方包:Node.js模块化开发、系统模块、第三方模块、package.json文件、Node.js中模块的加载机制、开发环境与生产环境、cookie与session

    1.Node.js模块化开发 1.1 JavaScript开发弊端 JavaScript 在使用时存在两大问题,文件依赖和命名冲突. 1.2 软件中的模块化开发 一个功能就是一个模块,多个模块可以组成 ...

最新文章

  1. imagenet 测试精度记录
  2. Oracle的NVL函数
  3. InstallShield Premier版本和Professional版本的功能差异
  4. ARM硬件支持java技术Jazelle DBX
  5. 科技人员在计算机前的肖像,首个全国科技工作者日来了 西南大学学子为科技工作者画像...
  6. mongodb存储数据_在MongoDB中存储分层数据
  7. 如何访问.then()链中的先前的诺言结果?
  8. java 通过反射获取数组
  9. Atitit 翻页功能的解决方案与版本历史 v4 r49
  10. 垃圾处理设备远程调试远程运维方案
  11. Word模板引擎使用指南
  12. android 有线网络,安卓手机免费“有线”上网
  13. 微信小程序安全需求基线
  14. win7旗舰版6l打印机咋安驱动_打印机安装不了,教您解决打印机驱动安装不了
  15. Android将APP做成一个launcher
  16. Xilinx火龙果学习笔记(1)---初识火龙果
  17. 移动终端课程设计——校园淘二手交易APP
  18. 地面气象观测数据-A文件转excel方法
  19. python抓取google搜索结果
  20. [百家号]铁流:华为Hi1620发布 自研内核还是ARM改?

热门文章

  1. Okhttp----缓存的加入方式----附完整demo
  2. 【剑指offer-Java版】34丑数
  3. 【剑指offer-Java版】22栈的压入弹出序列
  4. 【数据库】Ubuntu12.04安装配置Redis3.0
  5. JavaScript语言基础6
  6. UIBezierPath使用
  7. 医院挂号设计源代码_锡盟蒙医医院设立智慧导诊系统服务患者
  8. php人员权限管理(RBAC)
  9. Java蓝桥杯——贪心算法
  10. 如何写好一篇高质量的IEEE/ACM Transaction级别的计算机科学论文?