dependencies devDependencies peerDependencies optionalDependencies區別

原文連接: https://zhuanlan.zhihu.com/p/29855253
在一個Node.js項目中,package.json幾乎是一個必須的文件,它的主要做用就是管理項目中所使用到的外部依賴包,同時它也是npm命令的入口文件。

npm 目前支持如下幾類依賴包管理:

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

若是你想使用哪一種依賴管理,那麼你能夠將它放在package.json中對應的依賴對象中,好比:webpack

"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": [] 

下面咱們一一來看:git

dependencies

應用依賴,或者叫作業務依賴,這是咱們最經常使用的依賴包管理對象!它用於指定應用依賴的外部包,這些依賴是應用發佈後正常執行時所須要的,但不包含測試時或者本地打包時所使用的包。可以使用下面的命令來安裝:github

npm install packageName --save 

dependencies是一個簡單的JSON對象,包含包名與包版本,其中包版本能夠是版本號或者URL地址。好比:web

{ "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.一、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

開發環境依賴,僅次於dependencies的使用頻率!它的對象定義和dependencies同樣,只不過它裏面的包只用於開發環境,不用於生產環境,這些包一般是單元測試或者打包工具等,例如gulp, grunt, webpack, moca, coffee等,可以使用如下命令來安裝:shell

npm install packageName --save-dev 

舉個栗子:npm

{ "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腳本,開發時能夠很容易的測試。json

至此,你理解了--save和--save-dev的區別了嗎?

peerDependencies

同等依賴,或者叫同伴依賴,用於指定當前包(也就是你寫的包)兼容的宿主版本。如何理解呢? 試想一下,咱們編寫一個gulp的插件,而gulp卻有多個主版本,咱們只想兼容最新的版本,此時就能夠用同等依賴(peerDependencies)來指定:gulp

{ "name": "gulp-my-plugin", "version": "0.0.1", "peerDependencies": { "gulp": "3.x" } } 

當別人使用咱們的插件時,peerDependencies就會告訴明確告訴使用方,你須要安裝該插件哪一個宿主版本。數組

一般狀況下,咱們會在一個項目裏使用一個宿主(好比gulp)的不少插件,若是相互之間存在宿主不兼容,在執行npm install時,cli會拋出錯誤信息來告訴咱們,好比:dom

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 

運行命令npm install gulp-my-plugin --save-dev來安裝咱們插件,咱們來看下依賴圖譜:

├── gulp-my-plugin@0.0.1 └── gulp@3.9.1 

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聲明過,不然打包會報錯。

總結

以上就是目前npm支持的依賴管理,若有不明白或者錯誤之處,請在評論區留言。

  

更多參考:

相關文章
相關標籤/搜索