建立package.json直接的說:就是管理你本地安裝的npm包
一個package.json文件能夠作以下事情:node展現項目所依賴的npm包
容許你指定一個包的版本[範圍]
讓你創建起穩定,意味着你能夠更好的與其餘開發者共享linux
在你要建立的目錄下執行:npm init,系統會一一提示設置相關配置。提示設置的字段均爲必填字段(有的能夠用回車鍵,即設置爲空帶過)git
這裏說的項目廣義,好比:咱們能夠把咱們的項目發佈成一個npm包github
bugs: 項目問題反饋的Url或email配置,如:
{
「url」 : 「https://github.com/owner/project/issues「,
「email」 : 「project@hostname.com」
}npm
license: 項目許可證,讓使用者知道是如何被容許使用此項目。默認是」ISC」json
author,contributors: 坐着和貢獻者。格式設置以下:
{ 「name」 : 「Barney Rubble」
, 「email」 : 「b@rubble.com」
, 「url」 : 「http://barnyrubble.tumblr.com/」
}數組
files: 包含在項目中的文件數組。若是在數組裏面聲明瞭一個文件夾,那也會包含文件夾中的文件。能夠聲明一些規則來忽略部分文件。能夠在項目根目錄或者子目錄裏聲明一個.npmignore。markdown
Certain files are always included, regardless of settings:架構
package.json
README (and its variants)
CHANGELOG (and its variants)
LICENSE / LICENCE
Conversely, some files are always ignored:less.git
CVS
.svn
.hg
.lock-wscript
.wafpickle-N
*.swp
.DS_Store
._*
npm-debug.log
若是隻給man字段提供一個文件,則安裝完畢後,它就是man 的結果,這和此文件名無關
{
「name」: 「foo」,
「version」: 「1.2.3」,
「description」: 「A packaged foo fooer for fooing foos」,
「main」: 「foo.js」,
「man」: 「./man/doc.1」
}
上面這個配置將會在執行man foo時就會使用./man/doc.1這個文件。
若是指定的文件名並未以包名開頭,那麼它會被冠之前綴,像這樣
{
「name」: 「foo」,
「version」: 「1.2.3」,
「description」: 「A packaged foo fooer for fooing foos」,
「main」: 「foo.js」,
「man」: [
「./man/foo.1」,
「./man/bar.1」
]
}
這將會爲man foo和man foo-bar建立文件
man文件必須以一個數字結尾,和一個可選的.gz後綴(當它被壓縮時)。這個數字說明了這個文件被安裝到哪一個節中
{
「name」: 「foo」,
「version」: 「1.2.3」,
「description」: 「A packaged foo fooer for fooing foos」,
「main」: 「foo.js」,
「man」: [
「./man/foo.1」,
「./man/foo.2」
]
}
會爲使用man foo和man 2 foo而建立
directories.bin: 若是你在directories.bin中指定一個bin目錄,在這個目錄中的全部文件都會被當作在bin來使用。
因爲bin指令的工做方式,同時指定一個bin路徑和設置directories.bin將是一個錯誤。若是你想指定獨立的文件,使用bin,若是想執行某個文件夾裏的全部文件,使用directories.bin。
directories.doc: 把markdown文件放在這。也許某一天這些文件將被漂亮地展現出來,不過這僅僅是也許
repository: 項目代碼存放地方
「repository」 :
{ 「type」 : 「git」
, 「url」 : 「https://github.com/npm/npm.git」
}
「repository」 :
{ 「type」 : 「svn」
, 「url」 : 「https://v8.googlecode.com/svn/trunk/」
}
scripts: 聲明一系列npm腳本指令
- prepublish: 在包發佈以前運行,也會在npm install安裝到本地時運行
- publish,postpublish: 包被髮布以後運行
- preinstall: 包被安裝前運行
- install,postinstall: 包被安裝後運行
- preuninstall,uninstall: 包被卸載前運行
- postuninstall: 包被卸載後運行
- preversion: bump包版本前運行
- postversion: bump包版本後運行
- pretest,test,posttest: 經過npm test命令運行
- prestop,stop,poststop: 經過npm stop命令運行
- prestart,start,poststart: 經過npm start命令運行
- prerestart,restart,postrestart: 經過npm restart運行
config: 配置項目中須要的配置參數:
{ 「name」 : 「foo」
, 「config」 : { 「port」 : 「8080」 }
, 「scripts」 : { 「start」 : 「node server.js」 } } }
server.js中使用process.env.npm_package_config_port來訪問port
用戶也能夠這樣修改:npm config set foo:port 80
dependencies: 項目在生產環境中依賴的包
peerDependencies: 在某些狀況下,當一個主機沒法require依賴包時,你會想要告訴它還有哪些工具或庫與這個依賴包兼容。這一般被成爲一個插件。尤爲是在host文檔中聲明的模塊會暴露一個特定的接口
{
「name」: 「tea-latte」,
「version」: 「1.3.5」,
「peerDependencies」: {
「tea」: 「2.x」
}
}
這將確保tea-latte這個包只會和2.x版本的tea一塊兒被安裝。執行npm install tea-latte可能產生如下關係圖:
├── tea-latte@1.3.5
└── tea@2.2.0
bundledDependencies: {Array},發佈時會被一塊兒打包的模塊
optionalDependencies: 若是一個依賴模塊能夠被使用, 同時你也但願在該模塊找不到或沒法獲取時npm繼續運行,你能夠把這個模塊依賴放到optionalDependencies配置中。這個配置的寫法和dependencies的寫法同樣,不一樣的是這裏邊寫的模塊安裝失敗不會致使npm install失敗。固然,這種模塊就須要你本身在代碼中處理模塊確實的狀況了,例如:
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()
}
engines: 聲明項目須要的node或npm版本範圍
{ 「engines」 : { 「npm」 : 「~1.0.20」 } }
{ 「engines」 : { 「node」 : 「>=0.10.3 <0.12」 } }
os: 指定你的項目將運行在什麼操做系統上
https://docs.npmjs.com/files/package.json