What's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?

前端 未结 14 2041
灰色年华
灰色年华 2020-11-22 00:43

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 si

14条回答
  •  感动是毒
    2020-11-22 01:31

    dependencies
    Dependencies that your project needs to run, like a library that provides functions that you call from your code.
    They are installed transitively (if A depends on B depends on C, npm install on A will install B and C).
    Example: lodash: your project calls some lodash functions.

    devDependencies
    Dependencies you only need during development or releasing, like compilers that take your code and compile it into javascript, test frameworks or documentation generators.
    They are not installed transitively (if A depends on B dev-depends on C, npm install on A will install B only).
    Example: grunt: your project uses grunt to build itself.

    peerDependencies
    Dependencies that your project hooks into, or modifies, in the parent project, usually a plugin for some other library or tool. It is just intended to be a check, making sure that the parent project (project that will depend on your project) has a dependency on the project you hook into. So if you make a plugin C that adds functionality to library B, then someone making a project A will need to have a dependency on B if they have a dependency on C.
    They are not installed (unless npm < 3), they are only checked for.
    Example: grunt: your project adds functionality to grunt and can only be used on projects that use grunt.

    This documentation explains peer dependencies really well: https://nodejs.org/en/blog/npm/peer-dependencies/

    Also, the npm documentation has been improved over time, and now has better explanations of the different types of dependencies: https://github.com/npm/cli/blob/latest/doc/files/package.json.md#devdependencies

提交回复
热议问题