What's the difference between dependencies, de

2018-12-31 06:52发布

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.

标签: node.js npm
10条回答
梦该遗忘
2楼-- · 2018-12-31 07:25

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/npm/blob/master/doc/files/package.json.md#devdependencies

查看更多
看风景的人
3楼-- · 2018-12-31 07:32

If you do not want to install devDependencies you can use npm install --production

查看更多
弹指情弦暗扣
4楼-- · 2018-12-31 07:33

A simple explanation that made it more clear to me is:

When you deploy your app, modules in dependencies need to be installed or your app won't work. Modules in devDependencies don't need to be installed on the production server since you're not developing on that machine. link

查看更多
心情的温度
5楼-- · 2018-12-31 07:38

I'd like to add to the answer my view on these dependencies explanations

  • dependencies are used for direct usage in your codebase, things that usually end up in the production code, or chunks of code
  • devDependencies are used for the build process, tools that help you manage how the end code will end up, third party test modules, (ex. webpack stuff)
查看更多
登录 后发表回答