NPM 使用介紹

NPM是隨同NodeJS一塊兒安裝的包管理工具,能解決NodeJS代碼部署上的不少問題,常見的使用場景有如下幾種:html

  • 容許用戶從NPM服務器下載別人編寫的第三方包到本地使用。java

  • 容許用戶從NPM服務器下載並安裝別人編寫的命令行程序到本地使用。node

  • 容許用戶將本身編寫的包或命令行程序上傳到NPM服務器供別人使用。git


因爲新版的nodejs已經集成了npm,因此以前npm也一併安裝好了。一樣能夠經過輸入 "npm -v" 來測試是否成功安裝。命令以下,出現版本提示表示安裝成功:github

$ npm -v 2.3.0

若是你安裝的是舊版本的 npm,能夠很容易得經過 npm 命令來升級,命令以下:web

$ 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 系統使用如下命令便可:
redis

npm install npm -g

使用淘寶鏡像的命令:
express

cnpm install npm -g

使用 npm 命令安裝模塊

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

$ npm install <Module Name>

如下實例,咱們使用 npm 命令安裝經常使用的 Node.js web框架模塊 express:
json

$ 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 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 ……


若是要查看某個模塊的版本號,可使用命令以下:

$ 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 內容:

{  "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 字段指定了程序的主入口文件,require('moduleName') 就會加載這個文件。這個字段的默認值是模塊根目錄下面的 index.js。

  • 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) 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

以上的信息,你須要根據你本身的狀況輸入。在最後輸入 "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提供了不少命令,例如installpublish,使用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>能夠撤銷發佈本身發佈過的某個版本代碼。

使用淘寶 NPM 鏡像

你們都知道國內直接使用 npm 的官方鏡像是很是慢的,這裏推薦使用淘寶 NPM 鏡像。

淘寶 NPM 鏡像是一個完整 npmjs.org 鏡像,你能夠用此代替官方版本(只讀),同步頻率目前爲 10分鐘 一次以保證儘可能與官方服務同步。

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

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

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

$ cnpm install [name]

更多信息能夠查閱:http://npm.taobao.org/。

本文轉自:

http://www.runoob.com/nodejs/nodejs-npm.html#taobaonpm

本文分享自微信公衆號 - 源代碼社區(ydmsq666)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索