NPM
npm
是一套 node.js 的套件管理工具,也是一個公開的線上套件資料庫,目前廣泛地使用於 node.js、網站前端開發、手機應用程式、機器人等 JavaScript 社群
不論是開發套件,或是使用套件,都需要一個 package.json
套件定義檔來管理。
package.json
package.json
是 npm 用來管理套件,任務,發佈等相關資訊,因為 Angular CLI 也是使用 NPM 來管理套件,所以還是花點時間了解 package.json
,以下是 Angular CLI 所產生出來的 package.json
檔案。
{
"name": "ng-router",
"version": "0.0.0",
"license": "MIT",
"scripts": {
...
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
...
},
"devDependencies": {
...
}
}
設定檔結構
name (必填):套件名稱
- 規則
- 長度必須在214字元以下,包含
scope
名稱 - 名稱的第一個字元不能是
.
和_
- 名稱內不能有大寫字元
- 因為名稱會成為網址的一部分,所以名稱不能含有任何
non-URL-safe
字元
- 長度必須在214字元以下,包含
- 小技巧
- 不使用與
node
核心套件相同的名稱 - 名稱內不要放
js
或是node
,預設就認定是js
套件,如果真的要設定,可以設定在engines
參數 - 因為名稱會被引用 (required 或是 import),所以名稱應該要簡短但同時又賦有意義
- 在命名前,先檢查 npm 套件資料庫裡是否有想相同的名稱 (如果有要發佈到 npm 資料庫中)
- 不使用與
- 規則
version(必填):套件版本
- 版本命名方式採 semver 模式
license:專案授權模式
scripts:設定 npm 可以執行的指令
- scripts 有自己的執行生命週期,生命週期如下
- preinstall、install、postinstall:安裝套件
- preuninstall、uninstall、postuninstall :移除套件
- pretest、test、posttest:執行
npm test
指令 - prestop、stop、poststop:執行
npm stop
指令 - prestart、start、 poststart:執行
npm start
指令 - prerestart、restart、postrestart:執行
npm restart
指令,如果沒有指定restart
動作時,則會執行stop
跟start
指令
- scripts 有自己的執行生命週期,生命週期如下
private:當設定為
true
時,npm 就不會執行任何publish
的動作dependencies:定義使用此專案所依賴的套件及版本
指定的方式可以直接使用套件名稱、tarball 或是 git 的網址
版本指定方式,有很多方式,以下一一列出說明
version
:指定特定版本>version
:版本必須大於特定版本>=version
:版本必須大於或等於特定版本<version
:版本必須小於特定版本<=version
:版本必須小於或等於特定版本~version
:大致等於特定版本^version
:相容於此版本1.2.x
:1.2.0, 1.2.1 等, 但不是 1.3.0http://...
:指定套件的網址,會從該網址下載套件並安裝*
最新版本""
:空白,效果等同於*
version1 - version2
:介於version1
與version2
之間range1 || range2
:二選一版本git…
: 指定 Git URL 格式與範例如下<protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish> | #semver:<semver>]
git+ssh://[email protected]:npm/npm.git#v1.0.27 git+ssh://[email protected]:npm/npm#semver:^5.0 git+https://[email protected]/npm/npm.git git://github.com/npm/npm.git#v1.0.27
user/repo
:指定 GitHub Repo"express": "expressjs/express", "mocha": "mochajs/mocha#4727d357ea", "module": "user/repo#feature\/branch"
tag
path/path/path
:套件來源是本機某資料夾../foo/bar ~/foo/bar ./foo/bar /foo/bar
範例
{ "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" } }
devDependencies:定義開發此專案所需的其它套件及版本
- 版本設定的方式與
dependencies
相同 - 可以使用
npm link
的方式使用套件
- 版本設定的方式與
應用技巧
升級 npm
mac
npm install -g npm