Node.js npm

Node程序包管理器(NPM)提供了以下兩個主要功能:

  • 在線存儲庫的Node.js包/模塊,可搜索 search.nodejs.org

  • 命令行實用程序來安裝Node.js的包,做版本管理和Node.js包依賴管理。

NPM捆綁v0.6.3版本在一起以後,Node.js可直接安裝。爲了驗證一致性,打開控制檯,然後輸入以下命令,看到的結果:

$ npm --version
2.7.1

如果您正在運行舊版本npm,那麼可以將其更新到最新版本。只要從root使用以下命令:

$ sudo npm install npm -g
/usr/bin/npm -> /usr/lib/node_modules/npm/bin/npm-cli.js
npm@2.7.1 /usr/lib/node_modules/npm

使用NPM安裝模塊

有一個簡單安裝任何Node.js模塊,語法如下:

$ npm install

例如,下面是安裝一個著名的Node.jsweb框架模塊的命令叫 express:

$ npm install express

現在,你可以在js文件中使用此模塊如下:

var express = require('express');

全局VS本地安裝

默認情況下,NPM安裝任何依賴在本地模式。在這裏,本地模式是指包安裝在node_modules目錄位於的地方節點應用程序存在的文件夾中。本地部署的包都可以通過 require()方法進行訪問。例如,當我們安裝Express模塊,它安裝Express模塊當前目錄中創建node_modules目錄。

$ ls -l
total 0 drwxr-xr-x 3 root root 20 Mar 17 02:23 node_modules

另外,您可以使用npm ls命令列出了所有本地安裝的模塊。

全局範圍內已安裝的軟件包/依賴性都存儲在系統目錄中。這種依賴關係可以在任何Node.js的CLI(命令行界面)功能可以使用,但不能直接使用require()的Node應用程序中導入。 現在,讓我們嘗試使用全局安裝Express模塊。

$ npm install express -g

這將產生類似的結果,模塊將在全局範圍內安裝。在這裏,第一行講述了在那裏得到安裝模塊版本和它的位置。

express@4.12.2 /usr/lib/node_modules/express
├── merge-descriptors@1.0.0
├── utils-merge@1.0.0
├── cookie-signature@1.0.6
├── methods@1.1.1
├── fresh@0.2.4
├── cookie@0.1.2
├── escape-html@1.0.1
├── range-parser@1.0.2
├── content-type@1.0.1
├── finalhandler@0.3.3
├── vary@1.0.0
├── parseurl@1.3.0
├── content-disposition@0.5.0
├── path-to-regexp@0.1.3
├── depd@1.0.0
├── qs@2.3.3
├── on-finished@2.2.0 (ee-first@1.1.0)
├── etag@1.5.1 (crc@3.2.1)
├── debug@2.1.3 (ms@0.7.0)
├── proxy-addr@1.0.7 (forwarded@0.1.0, ipaddr.js@0.1.9)
├── send@0.12.1 (destroy@1.0.3, ms@0.7.0, mime@1.3.4)
├── serve-static@1.9.2 (send@0.12.2)
├── accepts@1.2.5 (negotiator@0.5.1, mime-types@2.0.10)
└── type-is@1.6.1 (media-typer@0.3.0, mime-types@2.0.10)

您可以使用下面的命令來檢查所有全局安裝的模塊:

$ npm ls -g

使用package.json

package.json是存在於任何Node應用程序/模塊的根目錄和用於定義一個包的屬性。讓我們打開package.json在當前 node_modules/express/

{ "name": "express", "description": "Fast, unopinionated, minimalist web framework", "version": "4.11.2", "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": "https://github.com/strongloop/express" }, "homepage": "http://expressjs.com/", "keywords": [ "express", "framework", "sinatra", "web", "rest", "restful", "router", "app", "api" ], "dependencies": { "accepts": "1.2.3", "content-disposition": "0.5.0", "cookie-signature": "1.0.5", "debug": "2.1.1", "depd": "1.0.0", "escape-html": "1.0.1", "etag": "1.5.1", "finalhandler": "0.3.3", "fresh": "0.2.4", "media-typer": "0.3.0", "methods": "1.1.1", "on-finished": "2.2.0", "parseurl": "1.3.0", "path-to-regexp": "0.1.3", "proxy-addr": "1.0.6", "qs": "2.3.3", "range-parser": "1.0.2", "send": "0.11.1", "serve-static": "1.8.1", "type-is": "1.5.6", "vary": "1.0.0", "cookie": "0.1.2", "merge-descriptors": "0.0.2", "utils-merge": "1.0.0" }, "devDependencies": { "after": "0.8.1", "ejs": "2.1.4", "istanbul": "0.3.5", "marked": "0.3.3", "mocha": "2.1.0", "should": "4.6.2", "supertest": "0.15.0", "hjs": "0.0.6", "body-parser": "1.11.0", "connect-redis": "2.2.0", "cookie-parser": "1.3.3", "express-session": "1.10.2", "jade": "1.9.1", "method-override": "2.3.1", "morgan": "1.5.1", "multiparty": "4.1.1", "vhost": "~3.0.0" }, "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-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/", "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/ test/acceptance/" }, "gitHead": "63ab25579bda70b4927a179b580a9c580b6c7ada", "bugs": { "url": "https://github.com/strongloop/express/issues" }, "_id": "express@4.11.2", "_shasum": "8df3d5a9ac848585f00a0777601823faecd3b148", "_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": "shtylman", "email": "shtylman@gmail.com" }, { "name": "dougwilson", "email": "doug@somethingdoug.com" }, { "name": "aredridel", "email": "aredridel@nbtsc.org" }, { "name": "strongloop", "email": "callback@strongloop.com" }, { "name": "rfeng", "email": "enjoyjava@gmail.com" } ], "dist": { "shasum": "8df3d5a9ac848585f00a0777601823faecd3b148", "tarball": "http://registry.npmjs.org/express/-/express-4.11.2.tgz" }, "directories": {}, "_resolved": "https://registry.npmjs.org/express/-/express-4.11.2.tgz", "readme": "ERROR: No README data found!" }

Package.json的屬性

  • name - 包的名稱

  • version - 包的版本

  • description - 包的描述

  • homepage - 包主頁

  • author - 包的作者

  • contributors - 貢獻者到包的名字

  • dependencies - 依賴關係的列表。NPM自動安裝所有在這裏的包node_module文件夾中提到的依賴關係。

  • repository - 包的庫類型和URL

  • main - 包的入口點

  • keywords - 關鍵字

卸載模塊

使用下面的命令卸載Node.js的模塊。

$ npm uninstall express

一旦NPM卸載包,可以通過查看/node_modules/目錄下的內容或驗證鍵入以下命令:

$ npm ls

更新模塊

更新package.json並改變其被更新並運行以下命令依賴的版本。

$ npm update express

搜索模塊

搜索使用NPM包名。

$ npm search express

創建模塊

創建模塊的需要要生成的package.json。讓我們使用NPM產生package.json,這將產生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 sane defaults.

See 'npm help json' for definitive documentation on these fields
and exactly what they do.

Use 'npm install --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: (webmaster)

需要提供有關你的模塊所需的所有信息。 你可以從上述的package.json文件的幫助,瞭解所要求的各種信息的含義。 一旦的package.json被產生。使用下面的命令將一個有效的電子郵件地址在NPM庫網站上註冊自己信息。

$ npm adduser
Username: mcmohd
Password:
Email: (this IS public) mcmohd@gmail.com

現在,發佈你的模塊的時間:

$ npm publish

如果你的模塊正常使用,然後將它發表在庫中並且能夠使用NPM就像任何其他其他Node.js的模塊安裝。