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

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

问题描述

此文档很难回答我的问题.我不明白那些解释.有人可以用简单的话说吗?如果很难选择简单的单词,也许还会举一些例子?

编辑也添加了peerDependencies,它与之关系密切,可能会引起混淆.

解决方案

重要行为差异摘要:

此处未讨论相关选项:

  • bundledDependencies,将在以下问题上进行讨论: optionalDependencies (提到

    在那种情况下,您通常不需要开发依赖项,因此您只需要使用软件包即可:dependencies.

    如果您确实要在这种情况下安装开发包,则可以将dev配置选项设置为true,可能在命令行中将其设置为:

    npm install "$package" --dev
    

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

    peerDependencies

    (在3.0之前测试)

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

    对于常规依赖项,您可以具有多个版本的依赖项:只需将其安装在依赖项的node_modules内即可.

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

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

    但是,

    插件是通常不需要其他软件包的软件包,在这种情况下,该软件包称为 host .相反:

    • 主机需要
    • 插件
    • 插件提供了主机希望找到的标准接口
    • 只有主机会被用户直接调用,因此必须有一个单独的版本.

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

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

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

    我认为这是控制反转设计模式的一个实例.

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

    例如,在类似 https://github.com/gruntjs/grunt-的Grunt插件上contrib-uglify ,您将看到:

    • gruntpeer-dependency
    • 唯一的require('grunt')tests/下:它实际上未被程序使用.

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

    如果每个插件都需要不同的Grunt版本,则此方法将无效.

    手册

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

    关键行是:

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

    然后在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 contains package.json
      • npm install $package on any other directory
    • devDependencies are:

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

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

    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 a peer-dependency
    • the only require('grunt') is under tests/: 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:

    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.

    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天全站免登陆