nodejs package.json詳細解讀

 package.json詳細內容html

它是這樣一個json文件(注意:json文件內是不能寫註釋的,複製下列內容請刪除註釋): JavaScript  { "name": "test",   //項目名稱(必須)
  "version": "1.0.0",   //項目版本(必須)
  "description": "This is for study gulp project !",   //項目描述(必須)
  "homepage": "",   //項目主頁
  "repository": {    //項目資源庫
    "type": "git", "url": "https://git.oschina.net/xxxx" }, "author": {    //項目做者信息
    "name": "surging", "email": "surging2@qq.com" }, "license": "ISC",    //項目許可協議
  "devDependencies": {    //項目依賴的插件
    "gulp": "^3.8.11", "gulp-less": "^3.0.0" } }

 

使用 package.json

package.json 位於模塊的目錄下,用於定義包的屬性。接下來讓咱們來看下 express 包的 package.json 文件,位於 node_modules/express/package.json 內容:java

{ "name": "express", "description": "Fast, unopinionated, minimalist web framework", "version": "4.13.3", "author": { "name": "TJ Holowaychuk", "email": "tj@vision-media.ca" }, "contributors": [ { "name": "Aaron Heckmann", "email": "aaron.heckmann+github@gmail.com" }, { "name": "Ciaran Jessup", "email": "ciaranj@gmail.com" }, { "name": "Douglas Christopher Wilson", "email": "doug@somethingdoug.com" }, { "name": "Guillermo Rauch", "email": "rauchg@gmail.com" }, { "name": "Jonathan Ong", "email": "me@jongleberry.com" }, { "name": "Roman Shtylman", "email": "shtylman+expressjs@gmail.com" }, { "name": "Young Jae Sim", "email": "hanul@hanul.me" } ], "license": "MIT", "repository": { "type": "git", "url": "git+https://github.com/strongloop/express.git" }, "homepage": "http://expressjs.com/", "keywords": [ "express", "framework", "sinatra", "web", "rest", "restful", "router", "app", "api" ], "dependencies": { "accepts": "~1.2.12", "array-flatten": "1.1.1", "content-disposition": "0.5.0", "content-type": "~1.0.1", "cookie": "0.1.3", "cookie-signature": "1.0.6", "debug": "~2.2.0", "depd": "~1.0.1", "escape-html": "1.0.2", "etag": "~1.7.0", "finalhandler": "0.4.0", "fresh": "0.3.0", "merge-descriptors": "1.0.0", "methods": "~1.1.1", "on-finished": "~2.3.0", "parseurl": "~1.3.0", "path-to-regexp": "0.1.7", "proxy-addr": "~1.0.8", "qs": "4.0.0", "range-parser": "~1.0.2", "send": "0.13.0", "serve-static": "~1.10.0", "type-is": "~1.6.6", "utils-merge": "1.0.0", "vary": "~1.0.1" }, "devDependencies": { "after": "0.8.1", "ejs": "2.3.3", "istanbul": "0.3.17", "marked": "0.3.5", "mocha": "2.2.5", "should": "7.0.2", "supertest": "1.0.1", "body-parser": "~1.13.3", "connect-redis": "~2.4.1", "cookie-parser": "~1.3.5", "cookie-session": "~1.2.0", "express-session": "~1.11.3", "jade": "~1.11.0", "method-override": "~2.3.5", "morgan": "~1.6.1", "multiparty": "~4.1.2", "vhost": "~3.0.1" }, "engines": { "node": ">= 0.10.0" }, "files": [ "LICENSE", "History.md", "Readme.md", "index.js", "lib/" ], "scripts":{"test":"mocha --require test/support/env --reporter spec --bail --check-leaks test/ test/acceptance/","test-ci":"istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/ test/acceptance/","test-cov":"istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks test/ test/acceptance/","test-tap":"mocha --require test/support/env --reporter tap --check-leaks test/ test/acceptance/"},"gitHead":"ef7ad681b245fba023843ce94f6bcb8e275bbb8e","bugs":{"url":"https://github.com/strongloop/express/issues"},"_id":"express@4.13.3","_shasum":"ddb2f1fb4502bf33598d2b032b037960ca6c80a3","_from":"express@*","_npmVersion":"1.4.28","_npmUser":{"name":"dougwilson","email":"doug@somethingdoug.com"},"maintainers":[{"name":"tjholowaychuk","email":"tj@vision-media.ca"},{"name":"jongleberry","email":"jonathanrichardong@gmail.com"},{"name":"dougwilson","email":"doug@somethingdoug.com"},{"name":"rfeng","email":"enjoyjava@gmail.com"},{"name":"aredridel","email":"aredridel@dinhe.net"},{"name":"strongloop","email":"callback@strongloop.com"},{"name":"defunctzombie","email":"shtylman@gmail.com"}],"dist":{"shasum":"ddb2f1fb4502bf33598d2b032b037960ca6c80a3","tarball":"http://registry.npmjs.org/express/-/express-4.13.3.tgz"},"directories":{},"_resolved":"https://registry.npmjs.org/express/-/express-4.13.3.tgz","readme":"ERROR: No README data found!"}

 

Package.json 屬性說明

  • name - 包名。node

  • version - 包的版本號。git

  • description - 包的描述。github

  • homepage - 包的官網 url 。web

  • author - 包的做者姓名。redis

  • contributors - 包的其餘貢獻者姓名。express

  • dependencies - 依賴包列表。若是依賴包沒有安裝,npm 會自動將依賴包安裝在 node_module 目錄下。npm

  • repository - 包代碼存放的地方的類型,能夠是 git 或 svn,git 可在 Github 上。json

  • main - main 字段是一個模塊ID,它是一個指向你程序的主要項目。就是說,若是你包的名字叫 express,而後用戶安裝它,而後require("express")。

  • keywords - 關鍵字

 

 

2. package.json
在前面例子中的 somepackage 文件夾下,咱們建立一個叫作 package.json 的文件,內容如
下所示:
{
"main" : "./lib/interface.js"
}
而後將 index.js 重命名爲 interface.js 並放入 lib 子文件夾下。以一樣的方式再次調用這個
包,依然能夠正常使用。
Node.js 在調用某個包時,會首先檢查包中 package.json 文件的 main 字段,將其做爲
包的接口模塊,若是 package.json main 字段不存在,會嘗試尋找 index.js index.node
爲包的接口。
package.json CommonJS 規定的用來描述包的文件,徹底符合規範的 package.json
件應該含有如下字段。
name:包的名稱,必須是惟一的,由小寫英文字母、數字和下劃線組成,不能包含
空格。
description:包的簡要說明。
version:符合語義化版本識別規範的版本字符串。
keywords:關鍵字數組,一般用於搜索。
maintainers:維護者數組,每一個元素要包含 nameemail (可選)、 web (可選)
字段。
contributors:貢獻者數組,格式與maintainers相同。包的做者應該是貢獻者
數組的第一個元素。
bugs:提交bug的地址,能夠是網址或者電子郵件地址。
licenses:許可證數組,每一個元素要包含 type (許可證的名稱)和 url (連接到
許可證文本的地址)字段。
repositories:倉庫託管地址數組,每一個元素要包含 type(倉庫的類型,如 git )、
url (倉庫的地址)和 path (相對於倉庫的路徑,可選)字段。
——————————
語義化版本識別Semantic Versioning)是由 Gravatars GitHub 創始人 Tom Preston-Werner 提出的一套版本命名
規範,最初目的是解決各式各樣版本號大小比較的問題,目前被許多包管理系統所採用。
40 3 Node.js 快速入門
dependencies:包的依賴,一個關聯數組,由包名稱和版本號組成。
下面是一個徹底符合 CommonJS 規範的 package.json 示例:
{
"name": "mypackage",
"description": "Sample package for CommonJS. This package demonstrates the required
elements of a CommonJS package.",
"version": "0.7.0",
"keywords": [
"package",
"example"
],
"maintainers": [
{
"name": "Bill Smith",
"email": "bills@example.com",
}
],
"contributors": [
{
"name": "BYVoid",
"web": "http://www.byvoid.com/"
}
],
"bugs": {
"mail": "dev@example.com",
"web": "http://www.example.com/bugs"
},
"licenses": [
{
"type": "GPLv2",
"url": "http://www.example.org/licenses/gpl.html"
}
],
"repositories": [
{
"type": "git",
"url": "http://github.com/BYVoid/mypackage.git"
}
],
"dependencies": {
"webkit": "1.2",
"ssl": {
"gnutls": ["1.0", "2.0"],
"openssl": "0.9.8"
}

原文http://www.runoob.com/nodejs/nodejs-npm.html

相關文章
相關標籤/搜索