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
[email protected] /usr/lib/node_modules/npm

使用NPM安裝模組

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

$ npm install <Module Name>

例如,下面是安裝一個著名的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

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

[email protected] /usr/lib/node_modules/express
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
└── [email protected] ([email protected], [email protected])

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

$ 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": "[email protected]"
  },
  "contributors": [
    {
      "name": "Aaron Heckmann",
      "email": "[email protected]"
    },
    {
      "name": "Ciaran Jessup",
      "email": "[email protected]"
    },
    {
      "name": "Douglas Christopher Wilson",
      "email": "[email protected]"
    },
    {
      "name": "Guillermo Rauch",
      "email": "[email protected]"
    },
    {
      "name": "Jonathan Ong",
      "email": "[email protected]"
    },
    {
      "name": "Roman Shtylman",
      "email": "[email protected]"
    },
    {
      "name": "Young Jae Sim",
      "email": "[email protected]"
    }
  ],
  "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": "[email protected]",
  "_shasum": "8df3d5a9ac848585f00a0777601823faecd3b148",
  "_from": "express@*",
  "_npmVersion": "1.4.28",
  "_npmUser": {
    "name": "dougwilson",
    "email": "[email protected]"
  },
  "maintainers": [
    {
      "name": "tjholowaychuk",
      "email": "[email protected]"
    },
    {
      "name": "jongleberry",
      "email": "[email protected]"
    },
    {
      "name": "shtylman",
      "email": "[email protected]"
    },
    {
      "name": "dougwilson",
      "email": "[email protected]"
    },
    {
      "name": "aredridel",
      "email": "[email protected]"
    },
    {
      "name": "strongloop",
      "email": "[email protected]"
    },
    {
      "name": "rfeng",
      "email": "[email protected]"
    }
  ],
  "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 <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: (webmaster)

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

$ npm adduser
Username: mcmohd
Password:
Email: (this IS public) [email protected]

現在,發布你的模組的時間:

$ npm publish

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