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...
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.
17 Answers
Summary of important behavior differences:
dependenciesare installed on both:npm installfrom a directory that containspackage.jsonnpm install $packageon any other directory
devDependenciesare:- also installed on
npm installon a directory that containspackage.json, unless you pass the--productionflag (go upvote Gayan Charith's answer), or if theNODE_ENV=productionenvironment variable is set - not installed on
npm install "$package"on any other directory, unless you give it the--devoption. - are not installed transitively.
- also installed on
-
- 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) This explains it nicely: - in version 7 peerDependencies are automatically installed unless an upstream dependency conflict is present that cannot be automatically resolved
Transitivity (mentioned by Ben Hutchison):
dependenciesare installed transitively: if A requires B, and B requires C, then C gets installed, otherwise, B could not work, and neither would A.devDependenciesis 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:
bundledDependencieswhich is discussed on the following question: Advantages of bundledDependencies over normal dependencies in npmoptionalDependencies(mentioned by Aidan Feldman)
Must Read
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.