In a Node.js project, package.json is almost a must. Its main function is to manage external dependency packages used in the project, and it is also the entry file for NPM commands.

npmCurrently, the following dependency package management types are supported:

  • dependencies
  • devDependencies
  • peerDependencies
  • optionalDependencies
  • bundledDependencies / bundleDependencies

If you want to use any dependency management, you can put it in the corresponding dependency object in package.json, for example:

"DevDependencies" : {" fw2 ":" ^ 0.3.2 ", "grunt" : "^" 1.0.1, "webpack" : "^ 3.6.0"}, "dependencies" : {" gulp ": "^3.9.1", "hello-else": "^1.0.0"}, "peerDependencies": {}, "optionalDependencies": {}, "bundledDependencies": []Copy the code

Let’s look at them one by one:

dependencies

Application dependencies, or business dependencies, are the most commonly used dependency package management objects! It is used to specify external packages that the application depends on, which are required for normal application execution after publication, but do not include packages used for testing or local packaging. To install it, use the following command:

npm install packageName --saveCopy the code

Dependencies is a simple JSON object that contains the package name and package version, which can be a version number or A URL address. Such as:

{" dependencies ": {" foo" : "1.0.0-2.9999.9999", / / the specified version range "bar" : "> = 1.0.2 < 2.1.2", "baz" : "> 1.0.2 < = 2.3.4", "boo" : "2.0.1," / / specified version "qux" : "< 1.0.0 | | > = 2.3.1 < 2.4.5 | | > = 2.5.2 < 3.0.0 asd" : "," "Http://asdf.com/asdf.tar.gz", / / the specified package address "til" : "1.2", / / the latest available version "elf" : "~ 1.2.3", "the elf" : "^" 1.2.3, / / compatible versions of "two" : "2.x", // 2.1, 2.2... , 2.9 all available "THR" : "*", / / any version "thr2" : "", / / any version" lat ":" latest ", / / the latest "dyl" : "file:.. / dyl, / / "local address" xyz ":" git+ssh://[email protected]: NPM/NPM. Git # v1.0.27 ", / / git address "fir" : "Git+ssh://[email protected]: NPM/npm# semver: ^ 5.0", "wdy" : "git+https://[email protected]/npm/npm.git," combinations "xxy" : "Git://github.com/npm/npm.git#v1.0.27,"}}Copy the code

devDependencies

Development environment dependencies, second only to Dependencies! Gulp, Grunt, webpack, Moca, coffee, etc. < span style = “color: RGB (51, 51, 51, 51); line-height: 20px; font-size: 14px! Important; white-space: normal; word-break: break-all;”

npm install packageName --save-devCopy the code

Here’s an example:

{ "name": "ethopia-waza"."description": "a delightfully fruity coffee varietal"."version": "1.2.3"."devDependencies": {
    "coffee-script": "~ 1.6.3." "
  },
  "scripts": {
    "prepare": "coffee -o lib/ -c src/waza.coffee"
  },
  "main": "lib/waza.js"
}Copy the code

The Prepare script is run before release, so consumers do not have to rely on it when compiling the project. In development mode, NPM install is run and the prepare script is also executed, making it easy to test during development.

So far, do you understand the difference between –save and –save-dev?

peerDependencies

Peer dependencies, or peer dependencies, are used to specify which host version of the current package (that is, the package you wrote) is compatible with. How do you understand that? If you write a plugin for gulp, and there are several major versions of gulp, you only want to be compatible with the latest version. You can specify peerDependencies as follows:

{
  "name": "gulp-my-plugin"."version": "0.0.1"."peerDependencies": {
    "gulp": "3.x"}}Copy the code

When someone else uses our plug-in, peerDependencies tells the user exactly which host version of the plug-in you want to install.

Typically, we use many plug-ins from one host (such as gulp) in a project, and if there are host incompatibations between them, when NPM install is executed, the CLI will throw an error message telling us, for example:

npm ERR! peerinvalid The package gulp does not satisfy its siblings' peerDependencies requirements!
npm ERR! peerinvalid Peer [email protected] wants gulp@~3.1.9
npm ERR! peerinvalid Peer [email protected] wants gulp@~2.3.0Copy the code

Run NPM install gulp-my-plugin –save-dev to install our plugin. Let’s look at the dependency graph:

├ ─ ─ [email protected] └ ─ ─ [email protected]Copy the code

OK, Nice!

Note that NPM 1 and NPM 2 will automatically install the same dependency, NPM 3 will not automatically install, will generate a warning! This can be done by manually adding dependencies to the package.json file.

optionalDependencies

OptionalDependencies. You can use optionalDependencies if you have dependencies that will run even if the installation fails or if you want NPM to continue running. Also, optionalDependencies overwrites dependencies of the same name, so don’t write them in both places.

For example, optional dependencies are like plug-ins for a program, executing one logic if they exist and another logic if they don’t.

try {
  var foo = require('foo')
  var fooVersion = require('foo/package.json').version
} catch (er) {
  foo = null
}
if ( notGoodFooVersion(fooVersion) ) {
  foo = null
}

// .. then later in your program ..

if (foo) {
  foo.doFooThings()
}Copy the code

bundledDependencies / bundleDependencies

BundledDependencies. BundledDependencies are an array object containing the names of dependencies that are packaged into the final distribution. Such as:

{
  "name": "fe-weekly"."description": "The ELSE weekly"."version": "1.0.0"."main": "index.js"."devDependencies": {
    "fw2": "^" 0.3.2."grunt": "^" 1.0.1."webpack": "^ 3.6.0"
  },
  "dependencies": {
    "gulp": "^ 3.9.1." "."hello-else": "^ 1.0.0"
  },
  "bundledDependencies": [
    "fw2"."hello-else"]}Copy the code

Execute the pack command NPM pack and the resulting fe-weeks-1.0.0. TGZ package will contain FW2 and Hello-else. It is important to note, however, that both packages must be declared in devDependencies or Dependencies first, otherwise packaging will fail.

conclusion

This is the dependency management currently supported by NPM. If you have any questions or errors, please leave a comment in the comments section.

Welcome to our column: ELSE

More references:

  • Docs.npmjs.com/files/packa…
  • Blog. Was. Me/peer – depend…