Home > Article > Web Front-end > Several npm dependency package management sharing
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": { "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": []Let’s look at it one by one: dependencies
npm install packageName --savedependencies 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", } }devDependencies
npm install packageName --save-devFor 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" }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?
{ "name": "gulp-my-plugin", "version": "0.0.1", "peerDependencies": { "gulp": "3.x" } }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.0Run 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.1OK, 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.
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() }bundledDependencies / bundleDependencies
{ "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" ] }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!