本文内容基于 npm 4.0.5
概述
npm (node package manager)
,即 node 包管理器。這裡的 node 包就是指各種 javascript 庫。
npm
是随同
Node.js
一起安裝的包管理工具,是以不需要獨立安裝。
可以通過檢視版本号來檢測
npm
是否已經安裝成功:
npm -v
如果已經安裝了
npm
,想要更新,隻需要一條指令即可:
npm install npm -g
package.json
簡介
使用 npm 來管理的 javascript 項目一般都有一個
package.json
檔案。它定義了這個項目所依賴的各種包,以及項目的配置資訊(比如名稱、版本、依賴等中繼資料)。
package.json
中的内容就是 json 形式。
重要字段
- name - 包名。
- version - 包的版本号。
- description - 包的描述。
- homepage - 包的官網 url 。
- author - 包的作者姓名。
- contributors - 包的其他貢獻者姓名。
- dependencies - 指定項目運作所依賴的子產品。
- devDependencies - 指定項目開發所依賴的子產品。
- repository - 包代碼存放的地方的類型,可以是 git 或 svn,git 可在 Github 上。
- main - main 字段是一個子產品ID,它是一個指向你程式的主要項目。就是說,如果你包的名字叫 express,然後使用者安裝它,然後require("express")。
- keywords - 關鍵字
- bin - 用來指定各個内部指令對應的可執行檔案的位置。
- scripts - 指定了運作腳本指令的npm指令行縮寫。
例:一個完整的package.json
{
"name": "reactnotes",
"version": "1.0.0",
"description": "react 教程",
"main": "./index.js",
"dependencies": {
"react": "^15.4.1",
"react-dom": "^15.4.1"
},
"devDependencies": {
"webpack-dev-server": "^1.16.2"
},
"scripts": {
"start": "node index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/atlantis1024/ReactNotes.git"
},
"author": "victor",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/atlantis1024/ReactNotes/issues"
},
"homepage": "https://github.com/atlantis1024/ReactNotes#readme"
}
package 版本
上文介紹 package.json 檔案中的
dependencies
和
devDependencies
字段,這二者都是 json 數組。它們的每個 json 子對象,key 表示包名,value 表示版本。
npm 允許的版本聲明方式十分多樣。下面将為你介紹一二。
說明
-
:安裝一個确定的版本,遵循“大版本.次要版本.小版本”的格式規定。如:1.0.0。version
-
:以~version
來舉例,表示安裝1.0.x的最新版本(不低于1.0.0)。但是大版本号和次要版本号不能變。~1.0.0
-
^version
來舉例,表示安裝1.x.x的最新版本(不低于1.0.0),但是大版本号不能變。^1.0.0
-
:表示安裝1.2.x。1.2.x
-
:可以像數組比較一樣,使用比較符來限定版本範圍。>、>=、<、<=
-
:相當于version1 - version2
.>=version1 <=version2
-
:版本滿足range1 或 range2 兩個限定條件中任意一個即可。range1 || range2
-
:一個指定 tag 對應的版本。tag
-
或*
(空字元串):任意版本。""
-
:最新版本。latest
-
http://...
:你可以指定http或本地檔案路徑下的包作為版本。file://...
-
:參考下面的“直接将 Git Url 作為依賴包版本”git...
-
user/repo
例:下面的版本聲明都是有效的
{ "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"
, "two" : "2.x"
, "thr" : "3.3.x"
, "lat" : "latest"
, "dyl" : "file:../dyl"
}
}
直接将 Git Url 作為依賴包版本
Git Url形式可以如下:
git://github.com/user/project.git#commit-ish
git+ssh://user@hostname:project.git#commit-ish
git+ssh://user@hostname/project.git#commit-ish
git+http://user@hostname/project/blah.git#commit-ish
git+https://user@hostname/project/blah.git#commit-ish
常用指令
npm 的指令很多,這裡,我挑選幾條 npm 中比較重要的指令來介紹。
更多詳情可以參考
npm 官方文檔help
個人認為這是最重要的指令。對指令行的用法有問題的時候,還有什麼比檢視幫助資訊更有用呢?
檢視 npm 指令清單**
npm -h
檢視所有指令使用方法
npm -l
檢視某條指令詳細幫助資訊
如:要檢視 npm install 指令的詳細幫助資訊
- 檢視指令的快捷幫助資訊
npm install -h
- 打開指令的幫助文檔
npm help install
npm init
用于初始化一個新的
npm init
檔案。
package.json
指令格式
npm init [-f|--force|-y|--yes]
執行指令後,npm 會問你一系列問題,然後在執行指令的目錄下建立一個
package.json
如果使用
-f
/
--force
-y
--yes
,npm 會使用預設值為你建立
package.json
檔案,不再詢問任何問題。
npm install
npm install
用于安裝子產品。
npm install (with no args, in package dir)
npm install [<@scope>/]<name>
npm install [<@scope>/]<name>@<tag>
npm install [<@scope>/]<name>@<version>
npm install [<@scope>/]<name>@<version range>
npm install <tarball file>
npm install <tarball url>
npm install <folder>
alias: npm i
common options: [-S|--save|-D|--save-dev|-O|--save-optional] [-E|--save-exact] [-B|--save-bundle] [--dry-run]
-
(with no args, in package dir)npm install
預設的,将
package.json
中聲明的所有子產品安裝到目前路徑下的 node_modules 目錄中。這稱為本地安裝。
如果,加上 -g, --global 參數,npm 會将目前包安裝到全局(系統目錄下),這稱為全局安裝。
-
npm install <folder>
安裝位于檔案系統上某檔案夾中的包。
-
npm install <tarball file>
安裝位于檔案系統上的包。注意:如果你隻想連結一個 dev 目錄到你的 npm 根目錄,使用
npm link
更容易做到這一點。
例:
npm install ./package.tgz
-
npm install <tarball url>
擷取 url,然後安裝它。為了區分此選項和其他選項,參數必須以“http://”或“https://”開頭。
npm install https://github.com/indexzero/forever/tarball/v0.5.6
-
npm install [<@scope>/] [-S|--save|-D|--save-dev|-O|--save-optional]
npm install sax
npm install
有3個可選參數,用于儲存或更新主package.json中的包版本:
-
:包将被添加到-S, --save
。dependencies
-
-D, --save-dev
devDependencies
-
-O, --save-optional
optionalDependencies
當使用上述任何選項将依賴儲存到package.json時,有兩個額外的可選标志:
-
:會在-E, --save-exact
檔案指定安裝子產品的确切版本。package.json
-
: 包也将被添加到-B, --save-bundle
bundleDependencies
-
npm install [<@scope>/]<name>@<tag>
安裝被 tag 引用的包的版本。如果 tag 不存在于該包的系統資料庫資料中,則失敗。
npm install sax@latest npm install @myorg/mypackage@latest
-
npm install [<@scope>/]<name>@<version>
安裝指定的包的版本。如果版本尚未釋出到系統資料庫,則失敗。
npm install [email protected] npm install @myorg/[email protected]
-
安裝與指定版本範圍相比對的包版本。npm install [<@scope>/]<name>@<version range>
npm install sax@">=0.1.0 <0.2.0" npm install @myorg/privatepackage@">=0.1.0 <0.2.0"
npm uninstall
npm uninstall
用于解除安裝包。
npm uninstall [<@scope>/]<pkg>[@<version>]... [-S|--save|-D|--save-dev|-O|--save-optional]
aliases: remove, rm, r, un, unlink
在全局模式下(即,在指令中附加
-g
--global
),它将目前包上下文作為全局包解除安裝。
npm uninstall
有3個可選參數,用于儲存或更新主 package.json 中的包版本:
-
-S, --save
dependencies
-
-D, --save-dev
devDependencies
-
-O, --save-optional
optionalDependencies
npm uninstall sax
npm uninstall sax --save
npm uninstall @myorg/privatepackage --save
npm uninstall node-tap --save-dev
npm uninstall dtrace-provider --save-optional
npm update
npm update
用于更新本地安裝的子產品。
npm update [-g] [<pkg>...]
aliases: up, upgrade
注:
從[email protected]開始,
npm update
僅更新頂級包。舊版本的 npm 會遞歸檢查所有的依賴。如果要達到舊版本的行為,請使用
npm --depth 9999 update
npm config
npm config
指令用于管理配置檔案。
npm config set <key> <value> [-g|--global]
npm config get <key>
npm config delete <key>
npm config list
npm config edit
npm get <key>
npm set <key> <value> [-g|--global]
aliases: c
-
: 設定一個配置參數。npm config set <key> <value> [-g|--global]
npm config set foo:port 80
-
:擷取一個配置參數。npm config get <key>
npm config get foo:port
-
:删除一個配置參數。npm config delete <key>
npm config delete foo:port
-
:列印配置參數清單。npm config list
-
:直接編輯配置檔案。npm config edit
-
:npm get <key>
的簡寫。npm config get <key>
-
npm set <key> <value> [-g|--global]
npm config set <key> <value> [-g|--global]
npm publish
npm publish
用于釋出一個包。
npm publish [<tarball>|<folder>] [--tag <tag>] [--access <public|restricted>]
Publishes '.' if no argument supplied
Sets tag 'latest' if no --tag specified
将包釋出到系統資料庫,以便可以按名稱安裝。如果沒有本地
.gitignore
.npmignore
檔案,則包括軟體包目錄中的所有檔案。如果這兩個過濾檔案都存在時,某個檔案被
.gitignore
忽略,而不被
.npmignore
忽略,則它将被包括。
npm run
如果在
package.json
檔案中的
scripts
字段定義了指令,就可以使用
npm run
來執行腳本指令。
假設
package.json
scripts
字段如下定義:
"scripts": {
"test": "mocha",
"lint": "eslint lib bin hot scripts",
"prepublish": "npm run test && npm run lint",
"start": "node index.js"
}
npm run test
:相當于執行
mocha
指令。它會開始執行測試架構 Mocha 。
npm run lint
eslint lib bin hot scripts
指令。它會開始執行 eslint 檢查。
npm run prepublish
npm run test
npm run lint
兩條指令。現在你了解如何複合指令了吧。
npm start
node index.js
。Node.js 啟動一個服務的入口腳本。