NPM 使用介紹

NPM 使用介紹

NPM是隨同NodeJS一起安裝的包管理工具鸽捻,能解決NodeJS代碼部署上的很多問題,常見的使用場景有以下幾種:

允許用戶從NPM服務(wù)器下載別人編寫的第三方包到本地使用。

允許用戶從NPM服務(wù)器下載并安裝別人編寫的命令行程序到本地使用蚀腿。

允許用戶將自己編寫的包或命令行程序上傳到NPM服務(wù)器供別人使用宣羊。

由于新版的nodejs已經(jīng)集成了npm,所以之前npm也一并安裝好了毁腿。同樣可以通過輸入"npm -v"?來測試是否成功安裝辑奈。命令如下,出現(xiàn)版本提示表示安裝成功:

$ 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 系統(tǒng)使用以下命令即可:

npm install npm -g

使用淘寶鏡像的命令:

cnpm install npm -g

使用 npm 命令安裝模塊

npm 安裝 Node.js 模塊語法格式如下:

$ npm install <Module Name>

以下實(shí)例,我們使用 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?? # 全局安裝

如果出現(xiàn)以下錯(cuò)誤:

npm err! Error: connect

ECONNREFUSED 127.0.0.1:8087

解決辦法為:

$ npm config set proxy null

本地安裝

[if !supportLists]·???????[endif]1. 將安裝包放在 ./node_modules 下(運(yùn)行 npm 命令時(shí)所在的目錄),如果沒有 node_modules 目錄株婴,會(huì)在當(dāng)前執(zhí)行 npm 命令的目錄下生成 node_modules 目錄怎虫。

[if !supportLists]·???????[endif]2. 可以通過 require() 來引入本地安裝的包。

全局安裝

[if !supportLists]·???????[endif]1. 將安裝包放在 /usr/local 下或者你 node 的安裝目錄困介。

[if !supportLists]·???????[endif]2. 可以直接在命令行里使用揪垄。

如果你希望具備兩者功能,則需要在兩個(gè)地方安裝它或使用npm link逻翁。

接下來我們使用全局方式安裝express

$ npm install express -g

安裝過程輸出如下內(nèi)容饥努,第一行輸出了模塊的版本號及安裝位置。

express@4.13.3node_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 list -g


├─┬ cnpm@4.3.2

│ ├── auto-correct@1.0.0

│ ├── bagpipe@0.3.5

│ ├── colors@1.1.2

│ ├─┬commander@2.9.0

│ │ └── graceful-readlink@1.0.1

│ ├─┬ cross-spawn@0.2.9

│ │ └── lru-cache@2.7.3

……

如果要查看某個(gè)模塊的版本號八回,可以使用命令如下:

$ npm list grunt


projectName@projectVersion /path/to/project/folder

└── grunt@0.4.1

使用package.json

package.json 位于模塊的目錄下酷愧,用于定義包的屬性。接下來讓我們來看下 express 包的 package.json 文件缠诅,位于 node_modules/express/package.json 內(nèi)容:

{

? "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 屬性說明

[if !supportLists]·???????[endif]name?- 包名溶浴。

[if !supportLists]·???????[endif]version?- 包的版本號。

[if !supportLists]·???????[endif]description?- 包的描述管引。

[if !supportLists]·???????[endif]homepage?- 包的官網(wǎng) url 士败。

[if !supportLists]·???????[endif]author?- 包的作者姓名。

[if !supportLists]·???????[endif]contributors?- 包的其他貢獻(xiàn)者姓名。

[if !supportLists]·???????[endif]dependencies?- 依賴包列表谅将。如果依賴包沒有安裝漾狼,npm 會(huì)自動(dòng)將依賴包安裝在 node_module 目錄下。

[if !supportLists]·???????[endif]repository?- 包代碼存放的地方的類型饥臂,可以是 git 或 svn逊躁,git 可在 Github 上。

[if !supportLists]·???????[endif]main?- main 字段指定了程序的主入口文件隅熙,require('moduleName')就會(huì)加載這個(gè)文件稽煤。這個(gè)字段的默認(rèn)值是模塊根目錄下面的 index.js。

[if !supportLists]·???????[endif]keywords?- 關(guān)鍵字

卸載模塊

我們可以使用以下命令來卸載 Node.js 模塊囚戚。

$ npm uninstall express

卸載后酵熙,你可以到 /node_modules/ 目錄下查看包是否還存在,或者使用以下命令查看:

$ npm ls

更新模塊

我們可以使用以下命令更新模塊:

$ npm update express

搜索模塊

使用以下來搜索模塊:

$ npm search express

創(chuàng)建模塊

創(chuàng)建模塊驰坊,package.json 文件是必不可少的绿店。我們可以使用 NPM 生成 package.json 文件,生成的文件包含了基本的結(jié)果庐橙。

$ 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` fordefinitive 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)runoob?????????????????? # 模塊名

version: (1.0.0)

description: Node.js 測試模塊(www.runoob.com)? # 描述

entry point: (index.js)

test command:make test

git repository: https://github.com/runoob/runoob.git? # Github地址

keywords:

author:

license: (ISC)

About to write to ……/node_modules/package.json:????? # 生成地址


{

? "name": "runoob",

? "version": "1.0.0",

? "description": "Node.js 測試模塊(www.runoob.com)",

? ……

}



Is this ok? (yes) yes

以上的信息假勿,你需要根據(jù)你自己的情況輸入。在最后輸入 "yes" 后會(huì)生成 package.json 文件态鳖。

接下來我們可以使用以下命令在 npm 資源庫中注冊用戶(使用郵箱注冊):

$ npm adduser

Username:mcmohd

Password:

Email: (this IS public)mcmohd@gmail.com

接下來我們就用以下命令來發(fā)布模塊:

$ npm publish

如果你以上的步驟都操作正確转培,你就可以跟其他模塊一樣使用 npm 來安裝。

版本號

使用NPM下載和發(fā)布代碼時(shí)都會(huì)接觸到版本號浆竭。NPM使用語義版本號來管理代碼浸须,這里簡單介紹一下。

語義版本號分為X.Y.Z三位邦泄,分別代表主版本號删窒、次版本號和補(bǔ)丁版本號。當(dāng)代碼變更時(shí)顺囊,版本號按以下原則更新肌索。

[if !supportLists]·???????[endif]如果只是修復(fù)bug,需要更新Z位特碳。

[if !supportLists]·???????[endif]如果是新增了功能诚亚,但是向下兼容,需要更新Y位午乓。

[if !supportLists]·???????[endif]如果有大變動(dòng)站宗,向下不兼容,需要更新X位益愈。

版本號有了這個(gè)保證后梢灭,在申明第三方包依賴時(shí),除了可依賴于一個(gè)固定版本號外,還可依賴于某個(gè)范圍的版本號敏释。例如"argv":

"0.0.x"表示依賴于0.0.x系列的最新版argv库快。

NPM支持的所有版本號范圍指定方式可以查看官方文檔

NPM 常用命令

除了本章介紹的部分外颂暇,NPM還提供了很多功能缺谴,package.json里也有很多其它有用的字段但惶。

除了可以在npmjs.org/doc/查看官方文檔外耳鸯,這里再介紹一些NPM常用命令。

NPM提供了很多命令膀曾,例如install和publish县爬,使用npm help可查看所有命令。

[if !supportLists]·???????[endif]NPM提供了很多命令添谊,例如install和publish财喳,使用npm help可查看所有命令。

[if !supportLists]·???????[endif]使用npm help <command>可查看某條命令的詳細(xì)幫助斩狱,例如npm help install耳高。

[if !supportLists]·???????[endif]在package.json所在目錄下使用npm install . -g可先在本地安裝當(dāng)前命令行程序,可用于發(fā)布前的本地測試所踊。

[if !supportLists]·???????[endif]使用npm update <package>可以把當(dāng)前目錄下node_modules子目錄里邊的對應(yīng)模塊更新至最新版本泌枪。

[if !supportLists]·???????[endif]使用npm update <package> -g可以把全局安裝的對應(yīng)命令行程序更新至最新版。

[if !supportLists]·???????[endif]使用npm cache clear可以清空NPM本地緩存秕岛,用于對付使用相同版本號發(fā)布新版本代碼的人碌燕。

[if !supportLists]·???????[endif]使用npm unpublish <package>@<version>可以撤銷發(fā)布自己發(fā)布過的某個(gè)版本代碼。

使用淘寶 NPM 鏡像

大家都知道國內(nèi)直接使用 npm 的官方鏡像是非常慢的继薛,這里推薦使用淘寶 NPM 鏡像修壕。

淘寶 NPM 鏡像是一個(gè)完整 npmjs.org 鏡像,你可以用此代替官方版本(只讀)遏考,同步頻率目前為 10分鐘 一次以保證盡量與官方服務(wù)同步慈鸠。

你可以使用淘寶定制的 cnpm (gzip 壓縮支持) 命令行工具代替默認(rèn)的npm:

$ npm install -g cnpm --registry=https://registry.npm.taobao.org

這樣就可以使用 cnpm 命令來安裝模塊了:

$ cnpm install [name]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市灌具,隨后出現(xiàn)的幾起案子林束,更是在濱河造成了極大的恐慌,老刑警劉巖稽亏,帶你破解...
    沈念sama閱讀 222,946評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件壶冒,死亡現(xiàn)場離奇詭異,居然都是意外死亡截歉,警方通過查閱死者的電腦和手機(jī)胖腾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,336評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人咸作,你說我怎么就攤上這事锨阿。” “怎么了记罚?”我有些...
    開封第一講書人閱讀 169,716評論 0 364
  • 文/不壞的土叔 我叫張陵墅诡,是天一觀的道長。 經(jīng)常有香客問我桐智,道長末早,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,222評論 1 300
  • 正文 為了忘掉前任说庭,我火速辦了婚禮然磷,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘刊驴。我一直安慰自己姿搜,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,223評論 6 398
  • 文/花漫 我一把揭開白布捆憎。 她就那樣靜靜地躺著舅柜,像睡著了一般。 火紅的嫁衣襯著肌膚如雪躲惰。 梳的紋絲不亂的頭發(fā)上致份,一...
    開封第一講書人閱讀 52,807評論 1 314
  • 那天窟哺,我揣著相機(jī)與錄音袁滥,去河邊找鬼按傅。 笑死贬堵,一個(gè)胖子當(dāng)著我的面吹牛笋粟,可吹牛的內(nèi)容都是我干的鲤桥。 我是一名探鬼主播壳炎,決...
    沈念sama閱讀 41,235評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼贬蛙,長吁一口氣:“原來是場噩夢啊……” “哼僚焦!你這毒婦竟也來了锰提?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,189評論 0 277
  • 序言:老撾萬榮一對情侶失蹤芳悲,失蹤者是張志新(化名)和其女友劉穎立肘,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體名扛,經(jīng)...
    沈念sama閱讀 46,712評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡谅年,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,775評論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了肮韧。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片融蹂。...
    茶點(diǎn)故事閱讀 40,926評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡旺订,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出超燃,到底是詐尸還是另有隱情区拳,我是刑警寧澤,帶...
    沈念sama閱讀 36,580評論 5 351
  • 正文 年R本政府宣布意乓,位于F島的核電站樱调,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏届良。R本人自食惡果不足惜笆凌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,259評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望伙窃。 院中可真熱鬧菩颖,春花似錦样漆、人聲如沸为障。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,750評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鳍怨。三九已至,卻和暖如春跪妥,著一層夾襖步出監(jiān)牢的瞬間鞋喇,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,867評論 1 274
  • 我被黑心中介騙來泰國打工眉撵, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留侦香,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,368評論 3 379
  • 正文 我出身青樓纽疟,卻偏偏與公主長得像罐韩,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子污朽,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,930評論 2 361