name
必須字段。提示:
- 不要在name中包含 js、 node 字樣;
- 這個名字最終會是 URL 的一部分,命令行的參數,目錄名,所以不能以點號或下劃線開頭;
- 這個名字可能在
require
方法中被調用,所以應該盡可能短; - 如果要發布為一個 NPM 包,則該名字必須為唯一;
version
必須字段,版本號,字符串格式,如:
"version": "1.0.0"
description
可選字段,字符串格式,如:
"description": "簡單描述當前應用或 package"
keywords
可選字段,數組格式,如:
"keywords": [“關鍵字”,“keyword”]
homepage
可選字段,項目主頁 URL,如:
"homepage": "https://github.com/github/github"
bugs
可選字段,問題追蹤系統的URL或郵箱地址;npm bugs用的上。
{
"url" :"http://github.com/owner/project/issues",
"email" :"project@hostname.com"
}
license
可選字段。如果是使用一個普遍的license,比如 BSD-3-Clause 或 MIT,直接使用:
{ "license" : "BSD-3-Clause" }
author, contributors
都是可選字段。author
作者,contributors
貢獻者。
author的格式如下:
{
"name" : "Barney Rubble",
"email" : "b@rubble.com",
"url" : "http://barnyrubble.tumblr.com/"
}
這種格式也可以:
"Barney Rubble <b@rubble.com> (http://barnyrubble.tumblr.com/)"
貢獻者:
"contributors": [
"?ndrew Rininsland <aendrew.rininsland@thetimes.co.uk> (http://www.aendrew.com)",
"Aurelio De Rosa <a.derosa@audero.it> (http://www.audero.it/)",
"Clay Reimann <clayreimann@gmail.com> (http://clayreimann.me)",
"Michael Aufreiter (http://substance.io)",
"Mathieu Dutour <mathieu@dutour.me> (https://github.com/mathieudutour)"
],
files
可選字段,項目包含的一組文件。如果是文件夾,文件夾下的文件也會被包含。如果需要把某些文件不包含在項目中,添加一個 .npmignore 文件。這個文件和.gitignore類似,如:
"files": [
"dist/*"
],
main
可選字段,建議必填。這個字段的值是你程序主入口模塊的ID。
- 如果其他用戶需要你的包,當用戶調用
require()
方法時,返回的就是這個模塊的導出(exports); - 如果發布為一個 NPM Package,則
require
為該屬性指定的文件; - 如果未指定該屬性,則默認為根目錄下的 index.js 或 index.node;
示例:
"main": "dist/components/GitHub.js",
"main": "index.js",
"main": "app.js",
scripts
可選,對象格式,如:
"scripts":{
"start": "node app.js",
"server": "node server/server.js",
"preinstall":"node-gyp rebuild"
}
可在終端使用 npm run start
來執行指定的腳本。如果包里有 binding.gyp,npm 默認在 preinstall 命令時,使用 node-gyp 做編譯。
bin
可選字段。很多的包都會有執行文件需要安裝到 PATH 中去。
這個字段對應的是一個Map,每個元素對應一個{ 命令名:文件名 }。
{
"bin" : {
"npm": "./cli.js"
}
}
directories
用于指示包的目錄結構:
directories.lib
指示庫文件的位置。
directories.bin
和前面的bin是一樣的,但如果前面已經有bin,那么這個就無效。
除了以上兩個,還有Directories.doc
、Directories.man
、Directories.example
。
repository
可選字段。用于指示代碼存放的位置。
"repository": {
"type": "git",
"url": "http://github.com/npm/npm.git"
}
或使用SVN:
"repository": {
"type": "svn",
"url": "http://v8.googlecode.com/svn/trunk/"
}
config
可選字段,object。config 對象中的值在 Scripts 的整個周期中皆可用,專門用于給 Scripts 提供配置參數。
dependencies
可選字段,指示當前包所依賴的其他包。
{
"dependencies": {
"foo": "1.0.0 - 2.9999.9999",
"bar": ">=1.0.2 <2.1.2"
}
}
版本格式可以是下面任一種:
- version 完全匹配
- >version 大于這個版本
- >=version 大于或等于這個版本
- <version 小于這個版本
- <=version 小于或等于這個版本
- ~version 非常接近這個版本
- ^version 與當前版本兼容
- 1.2.x X代表任意數字,因此
1.2.1
,1.2.3
等都可以。 - http://... Unix 系統下使用的 tarball 的 URL。
- * 任何版本都可以
- "" 任何版本都可以
- version1 - version2 等價于 >=version1 <=version2
- range1 || range2 滿足任意一個即可
- git... Git地址
- user/repo
devDependencies
可選字段,開發環境下的依賴包,格式同 dependencies 屬性。
peerDependencies
可選字段。兼容性依賴。如果你的包是插件,適合這種方式。
bundledDependencies
可選字段。發布包時同時打包的其他依賴。
optionalDependencies
可選字段。如果你想在某些依賴即使沒有找到,或則安裝失敗的情況下,npm 都繼續執行。那么這些依賴適合放在這里。
engines
可選字段。既可以指定 node 版本:
{ "engines" : {"node" : ">=0.10.3 <0.12" } }
也可以指定npm版本:
{ "engines" : {"npm" : "~1.0.20" } }
engineStrick
可選字段,布爾值。如果你肯定你的程序只能在制定的 engine 上運行,設置為 true
。
os
可選字段。指定模塊可以在什么操作系統上運行:
"os" : [ "darwin","linux" ]
"os" : [ "!win32" ]
CPU
可選字段。指定CPU型號。
"cpu" : [ "x64","ia32" ]
"cpu" : [ "!arm","!mips" ]
preferGlobal
可選字段,布爾值。如果你的包是個命令行應用程序,需要全局安裝,就可以設為 true
。
private
可選字段,布爾值。如果 private 為 true
,npm 會拒絕發布。這可以防止私有 repositories 不小心被發布出去,示例:
"private": true
publishConfig
可選字段。發布時使用的配置值。
資料
- package.json字段全解 作者:清簫
- 《Node.js 實戰》 作者: 趙坤 / 寸志 / 雷宗民 / 吳中驊