你需要知道的幾類npm依賴包管理
在一個Node.js項目中,package.json幾乎是一個必須的文件,它的主要作用就是管理項目中所使用到的外部依賴包,同時它也是npm命令的入口文件。
npm 目前支持以下幾類依賴包管理:
- dependencies
- devDependencies
- peerDependencies
- optionalDependencies
- bundledDependencies / bundleDependencies
- 如果你想使用哪種依賴管理,那麼你可以將它放在package.json中對應的依賴對象中,比如:
"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": []
dependencies
應用依賴,或者叫做業務依賴,這是我們最常用的依賴包管理對象!它用於指定應用依賴的外部包,這些依賴是應用發佈後正常執行時所需要的,但不包含測試時或者本地打包時所使用的包。可使用下面的命令來安裝:
- npm install packageName --save
- dependencies是一個簡單的JSON對象,包含包名與包版本,其中包版本可以是版本號或者URL地址。比如:
{
"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://[email protected]:npm/npm.git#v1.0.27", // git 地址
"fir" : "git+ssh://[email protected]:npm/npm#semver:^5.0",
"wdy" : "git+https://[email protected]/npm/npm.git",
"xxy" : "git://github.com/npm/npm.git#v1.0.27",
}
}
devDependencies
開發環境依賴,僅次於dependencies的使用頻率!它的對象定義和dependencies一樣,只不過它裏面的包只用於開發環境,不用於生產環境,這些包通常是單元測試或者打包工具等,例如gulp, grunt, webpack, moca, coffee等,可使用以下命令來安裝:
- npm install packageName --save-dev
舉個栗子:
{ "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"
}
prepare腳本會在發佈前運行,因此使用者在編譯項目時不用依賴它。在開發模式下,運行npm install, 同時也會執行prepare腳本,開發時可以很容易的測試。
至此,你理解了–save和–save-dev的區別了嗎?
peerDependencies
同等依賴,或者叫同伴依賴,用於指定當前包(也就是你寫的包)兼容的宿主版本。如何理解呢? 試想一下,我們編寫一個gulp的插件,而gulp卻有多個主版本,我們只想兼容最新的版本,此時就可以用同等依賴(peerDependencies)來指定:
{
"name": "gulp-my-plugin",
"version": "0.0.1",
"peerDependencies": {
"gulp": "3.x"
}
}
當別人使用我們的插件時,peerDependencies就會告訴明確告訴使用方,你需要安裝該插件哪個宿主版本。
通常情況下,我們會在一個項目裏使用一個宿主(比如gulp)的很多插件,如果相互之間存在宿主不兼容,在執行npm install時,cli會拋出錯誤信息來告訴我們,比如:
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.0
運行命令npm install gulp-my-plugin --save-dev來安裝我們插件,我們來看下依賴圖譜:
├── [email protected]
└── [email protected]
OK, Nice!
注意,npm 1 與 npm 2 會自動安裝同等依賴,npm 3 不再自動安裝,會產生警告!手動在package.json文件中添加依賴項可以解決。
optionalDependencies
可選依賴,如果有一些依賴包即使安裝失敗,項目仍然能夠運行或者希望npm繼續運行,就可以使用optionalDependencies。另外optionalDependencies會覆蓋dependencies中的同名依賴包,所以不要在兩個地方都寫。
舉個栗子,可選依賴包就像程序的插件一樣,如果存在就執行存在的邏輯,不存在就執行另一個邏輯。
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
打包依賴,bundledDependencies是一個包含依賴包名的數組對象,在發佈時會將這個對象中的包打包到最終的發佈包裏。如:
{
“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”
]
}
執行打包命令npm pack, 在生成的fe-weekly-1.0.0.tgz包中,將包含fw2和hello-else。 但是值得注意的是,這兩個包必須先在devDependencies或dependencies聲明過,否則打包會報錯。