NPM 使用介紹
NPM是隨同NodeJS一起安裝的套件管理工具,能解決NodeJS程式碼部署上的許多問題,常見的使用場景有以下幾種:
允許用戶從NPM伺服器下載別人寫的第三方套件到本地使用。
允許使用者從NPM伺服器下載並安裝別人編寫的命令列程式到本機使用。
允許使用者將自己編寫的套件或命令列程式上傳到NPM伺服器供別人使用。
由於新版本的nodejs已經整合了npm,所以之前npm也一併安裝好了。同樣可以輸入 "npm -v" 來測試是否成功安裝。指令如下,出現版本提示表示安裝成功:
$ npm -v 2.3.0
如果你安裝的是舊版的npm,可以很容易得透過npm 指令來升級,指令如下:
$ sudo npm install npm -g /usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js npm@2.14.2 /usr/local/lib/node_modules/npm
如果是Window系統使用以下指令即可:
npm install npm -g
使用npm 指令安裝模組
npm 安裝Node.js 模組語法格式如下:
$ npm install <Module Name>
以下實例,我們使用npm 指令安裝常用的Node.js web框架模組express:
$ npm install express
安裝好之後,express 套件就放在了工程目錄下的node_modules 目錄中,因此在程式碼中只需要透過require('express') 的方式就好,無須指定第三方套件路徑。
var express = require('express');
全域安裝與本機安裝
npm 的套件安裝分為本機安裝(local)、全域安裝(global)兩種,從敲的命令列來看,差異只是有沒有-g而已,例如
npm install express # 本地安装 npm install express -g # 全局安装
如果出現以下錯誤:
npm err! Error: connect ECONNREFUSED 127.0.0.1:8087
解決方法為:
$ npm config set proxy null
本機安裝
1. 將安裝套件放在./node_modules 下(執行npm 指令時所在的目錄),如果沒有node_modules 目錄,會在目前執行npm 指令的目錄下產生node_modules 目錄。
2. 可以透過 require() 來引入本機安裝的套件。
全域安裝
1. 將安裝套件放在 /usr/local 或你 node 的安裝目錄。
2. 可以直接在命令列裡使用。
如果你希望具備兩者功能,則需要在兩個地方安裝它或使用 npm link。
接下來我們使用全域方式安裝 express
$ npm install express -g
安裝過程輸出如下內容,第一行輸出了模組的版本號及安裝位置。
express@4.13.3 node_modules/express ├── escape-html@1.0.2 ├── range-parser@1.0.2 ├── merge-descriptors@1.0.0 ├── array-flatten@1.1.1 ├── cookie@0.1.3 ├── utils-merge@1.0.0 ├── parseurl@1.3.0 ├── cookie-signature@1.0.6 ├── methods@1.1.1 ├── fresh@0.3.0 ├── vary@1.0.1 ├── path-to-regexp@0.1.7 ├── content-type@1.0.1 ├── etag@1.7.0 ├── serve-static@1.10.0 ├── content-disposition@0.5.0 ├── depd@1.0.1 ├── qs@4.0.0 ├── finalhandler@0.4.0 (unpipe@1.0.0) ├── on-finished@2.3.0 (ee-first@1.1.1) ├── proxy-addr@1.0.8 (forwarded@0.1.0, ipaddr.js@1.0.1) ├── debug@2.2.0 (ms@0.7.1) ├── type-is@1.6.8 (media-typer@0.3.0, mime-types@2.1.6) ├── accepts@1.2.12 (negotiator@0.5.3, mime-types@2.1.6) └── send@0.13.0 (destroy@1.0.3, statuses@1.2.1, ms@0.7.1, mime@1.3.4, http-errors@1.3.1)
你可以使用以下指令來檢視所有全域安裝的模組:
$ npm ls -g
使用package.json
package.json 位於模組的目錄下,用於定義包的屬性。接下來讓我們來看下express 套件的package.json 文件,位於node_modules/express/package.json 內容:
{ "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 - 套件名稱。
version - 套件的版本號碼。
description - 套件的描述。
homepage - 套件的官網 url 。
author - 套件的作者姓名。
contributors - 套件的其他貢獻者姓名。
dependencies - 依賴套件清單。如果依賴套件沒有安裝,npm 會自動將依賴套件安裝在 node_module 目錄下。
repository - 套件程式碼存放的地方的類型,可以是 git 或 svn,git 可在 Github 上。
main - main 欄位是一個模組ID,它是一個指向你程式的主要項目。就是說,如果你包裝的名字叫 express,然後用戶安裝它,然後require("express")。
keywords - 關鍵字
解除安裝模組
我們可以使用下列指令來卸載Node.js 模組。
$ npm uninstall express
卸載後,你可以到/node_modules/ 目錄下查看套件是否還存在,或者使用以下命令查看:
$ npm ls
#更新模組
我們可以使用以下命令更新模組:
$ npm update express
搜尋模組
使用以下來搜尋模組:
$ npm search express
創建模組
建立模組,package.json 檔案是不可或缺的。我們可以使用 NPM 產生 package.json 文件,產生的文件包含了基本的結果。
$ npm init This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults. See `npm help json` for definitive documentation on these fields and exactly what they do. Use `npm install <pkg> --save` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. name: (node_modules) php # 模块名 version: (1.0.0) description: Node.js 测试模块(www.php.cn) # 描述 entry point: (index.js) test command: make test git repository: https://github.com/php/php.git # Github 地址 keywords: author: license: (ISC) About to write to ……/node_modules/package.json: # 生成地址 { "name": "php", "version": "1.0.0", "description": "Node.js 测试模块(www.php.cn)", …… } Is this ok? (yes) yes
以上的信息,你需要根據你自己的情況輸入。在最後輸入 "yes" 後會產生 package.json 檔案。
接下來我們可以使用以下命令在npm 資源庫中註冊使用者(使用郵箱註冊):
$ npm adduser Username: mcmohd Password: Email: (this IS public) mcmohd@gmail.com
接下來我們就用以下命令來發布模組:
$ npm publish
如果你以上的步驟都操作正確,你就可以跟其他模組一樣使用npm 來安裝。
版本號
使用NPM下載和發佈程式碼時都會接觸到版本號。 NPM使用語意版本號來管理程式碼,這裡簡單介紹一下。
語意版本號分為X.Y.Z三位,分別代表主版本號、次版本號、補丁版本號。當程式碼變更時,版本號碼會依照下列原則更新。
如果只是修復bug,需要更新Z位元。
如果是新增了功能,但是向下相容,需要更新Y位元。
如果有大變動,向下不相容,需要更新X位元。
版本號有了這個保證後,在申明第三方套件依賴時,除了可依賴一個固定版本號外,還可依賴某個範圍的版本號。例如"argv": "0.0.x"表示依賴0.0.x系列的最新版argv。
NPM支援的所有版本號範圍指定方式可以查看官方文件。
NPM 常用指令
除了本章介紹的部分外,NPM也提供了許多功能,package.json裡也有很多它有用的欄位。
除了可以在npmjs.org/doc/查看官方文件外,這裡再介紹一些NPM常用指令。
NPM提供了許多指令,例如install和publish,使用npm help可查看所有指令。
NPM提供了許多指令,例如
install
和publish
,使用npm help
可檢視所有指令。使用
npm help <command>
可查看某條指令的詳細協助,例如npm help install
。在
package.json
所在目錄下使用npm install . -g
可先在本機安裝目前命令列程序,可用於發布前的本地測試。使用
npm update <package>
可以把目前目錄下node_modules
子目錄裡邊的對應模組更新至最新版本。使用
npm update <package> -g
可以把全域安裝的對應命令列程式更新至最新版。使用
npm cache clear
可以清空NPM本機緩存,用於對付使用相同版本號發布新版本程式碼的人。使用
npm unpublish <package>@<version>
可以撤銷發布自己發布過的某個版本程式碼。