This article mainly introduces you to several types of npm dependency package management that you should know. npm is the package manager in node.js and is a command line tool. The article introduces it in detail through sample code. Friends who need it You can use it as a reference, and follow the editor to learn together.
npm currently supports the following types of dependency package management:
dependencies
- ##devDependencies
- peerDependencies
- optionalDependencies
- bundledDependencies / bundleDependencies
If you Which kind of dependency management you want to use, then you can put it in the corresponding dependency object in package.json, such as:
"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 after login
Let’s look at it one by one:
dependencies
Application dependencies, or business dependencies, are our most commonly used dependency package management objects! It is used to specify the external packages that the application depends on. These dependencies are required for normal execution after the application is released, but do not include packages used during testing or local packaging. You can use the following command to install:
npm install packageName --save
Copy after login
dependencies is a simple JSON object, including the package name and package version, where the package version can be a version number or a URL address. For example:
{
"dependencies" :{
"foo" : "1.0.0 - 2.9999.9999", // 指定版本范围
"bar" : ">=1.0.2 <2.1.2",
"baz" : ">1.0.2 <=2.3.4",
"boo" : "2.0.1", // 指定版本
"qux" : "<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0",
"asd" : "http://asdf.com/asdf.tar.gz", // 指定包地址
"til" : "~1.2", // 最近可用版本
"elf" : "~1.2.3",
"elf" : "^1.2.3", // 兼容版本
"two" : "2.x", // 2.1、2.2、...、2.9皆可用
"thr" : "*", // 任意版本
"thr2": "", // 任意版本
"lat" : "latest", // 当前最新
"dyl" : "file:../dyl", // 本地地址
"xyz" : "git+ssh://git@github.com:npm/npm.git#v1.0.27", // git 地址
"fir" : "git+ssh://git@github.com:npm/npm#semver:^5.0",
"wdy" : "git+https://isaacs@github.com/npm/npm.git",
"xxy" : "git://github.com/npm/npm.git#v1.0.27",
}
}
Copy after login
devDependencies
Development environment dependencies, second only to the frequency of use of dependencies! Its object definition is the same as dependencies, except that the packages in it are only used in the development environment, not in the production environment. These packages are usually unit tests or packaging tools, such as gulp, grunt, webpack, moca, coffee, etc., which can be used The following command is used to install:
npm install packageName --save-dev
Copy after login
For 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 after login
The prepare script will be run before publishing, so users do not need to rely on it when compiling the project. In development mode, running npm install will also execute the prepare script, which can be easily tested during development.
So far, do you understand the difference between --save and --save-dev?
peerDependencies
Equal dependencies, or peer dependencies, are used to specify host versions that are compatible with the current package (that is, the package you wrote). How to understand it? Just imagine, we write a gulp plug-in, but gulp has multiple main versions. We only want to be compatible with the latest version. At this time, we can use peerDependencies to specify:
{
"name": "gulp-my-plugin",
"version": "0.0.1",
"peerDependencies": {
"gulp": "3.x"
}
}
Copy after login
When others use When we add a plug-in, peerDependencies will tell the user clearly which host version you need to install the plug-in.
Normally, we will use many plug-ins of one host (such as gulp) in a project. If there is host incompatibility between each other, when executing npm install, the cli will throw an error message to tell Let's, for example:
npm ERR! peerinvalid The package gulp does not satisfy its siblings' peerDependencies requirements!
npm ERR! peerinvalid Peer gulp-cli-config@0.1.3 wants gulp@~3.1.9
npm ERR! peerinvalid Peer gulp-cli-users@0.1.4 wants gulp@~2.3.0
Copy after login
Run the command npm install gulp-my-plugin --save-dev to install our plug-in. Let's take a look at the dependency graph:
├── gulp-my-plugin@0.0.1
└── gulp@3.9.1
Copy after login
OK, Nice!
Note that npm 1 and npm 2 will automatically install the same dependencies. npm 3 will no longer be automatically installed and a warning will be generated! Manually adding dependencies in package.json file can be solved.
optionalDependencies
Optional dependencies, if there are some dependent packages that fail to install, but the project can still run or you want npm to continue running, you can use optionalDependencies. In addition, optionalDependencies will overwrite the dependency package with the same name in dependencies, so do not write it in both places.
For example, an optional dependency package is like a plug-in for a program. If it exists, the existing logic will be executed. If it does not exist, another logic will be executed.
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 after login
bundledDependencies / bundleDependencies
Packaging dependencies, bundledDependencies is an array object containing dependent package names. When publishing, the packages in this object will be packaged into the final release package. . For example:
{
"name": "fe-weekly",
"description": "ELSE 周刊",
"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 after login
Execute the packaging command npm pack, and the generated fe-weekly-1.0.0.tgz package will include fw2 and hello-else. However, it is worth noting that these two packages must be declared in devDependencies or dependencies first, otherwise the packaging will report an error.
Related recommendations:
Spring Boot introduces dependency package Druid
Detailed explanation of npm and webpack configuration methods in node.js
Share how to use the latest version of nodejs to install npm
The above is the detailed content of Several npm dependency package management sharing. For more information, please follow other related articles on the PHP Chinese website!