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
Dependencies vs dev dependencies
Dev dependencies are modules which are only required during development whereas dependencies are required at runtime. If you are deploying your application, dependencies has to be installed, or else your app simply will not work. Libraries that you call from your code that enables the program to run can be considered as dependencies.
Eg- React , React - dom
Dev dependency modules need not be installed in the production server since you are not gonna develop in that machine .compilers that covert your code to javascript , test frameworks and document generators can be considered as dev-dependencies since they are only required during development .
Eg- ESLint , Babel , webpack
@FYI,
mod-a
dev-dependents:
- mod-b
dependents:
- mod-c
mod-d
dev-dependents:
- mod-e
dependents:
- mod-a
----
npm install mod-d
installed modules:
- mod-d
- mod-a
- mod-c
----
checkout the mod-d code repository
npm install
installed modules:
- mod-a
- mod-c
- mod-e
If you are publishing to npm, then it is important that you use the correct flag for the correct modules. If it is something that your npm module needs to function, then use the "--save" flag to save the module as a dependency. If it is something that your module doesn't need to function but it is needed for testing, then use the "--save-dev" flag.
# For dependent modules
npm install dependent-module --save
# For dev-dependent modules
npm install development-module --save-dev
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 codedevDependencies
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)If you do not want to install devDependencies you can use npm install --production
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 directorydevDependencies are:
npm install
on a directory that contains package.json
, unless you pass the --production
flag (go upvote Gayan Charith's answer).npm install "$package"
on any other directory, unless you give it the --dev
option.peerDependencies:
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:
bundledDependencies
which is discussed on the following question: Advantages of bundledDependencies over normal dependencies in NPMdependencies
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.
(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:
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
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.
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.
I found a simple explanation.
Short Answer:
dependencies "...are those that your project really needs to be able to work in production."
devDependencies "...are those that you need during development."
peerDependencies "if you want to create and publish your own library so that it can be used as a dependency"
More details in this post: https://code-trotter.com/web/dependencies-vs-devdependencies-vs-peerdependencies
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