Node.js-NPM


节点包管理器(NPM)提供两个主要功能 -

  • Node.js 包/模块的在线存储库,可在search.nodejs.org上搜索

  • 用于安装 Node.js 包、对 Node.js 包进行版本管理和依赖项管理的命令行实用程序。

v0.6.3 版本之后,NPM 与 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 <Module Name>

例如,以下是安装著名的 Node.js Web 框架模块express 的命令 -

$ npm install express

现在您可以在 js 文件中使用此模块,如下所示 -

var express = require('express');

全局安装与本地安装

默认情况下,NPM 在本地模式下安装任何依赖项。这里的本地模式是指将包安装在 Node 应用程序所在文件夹中的 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(命令行界面)功能中使用,但不能直接在 Node 应用程序中使用 require() 导入。现在让我们尝试使用全局安装来安装 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 应用程序/模块的根目录中,用于定义包的属性。让我们打开node_modules/express/中的express包的package.json

{
   "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": "https://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": "https://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 - 包的版本

  • 描述- 包的描述

  • 主页- 包的主页

  • 作者- 包的作者

  • 贡献者- 包的贡献者姓名

  • 依赖项- 依赖项列表。NPM 会自动将此处提到的所有依赖项安装到包的 node_module 文件夹中。

  • 存储库- 存储库类型和包的 URL

  • main - 包的入口点

  • 关键词- 关键词

卸载模块

使用以下命令卸载 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) mcmohd@gmail.com

现在是时候发布您的模块了 -

$ npm publish

如果您的模块一切正常,那么它将发布在存储库中,并且可以像任何其他 Node.js 模块一样使用 NPM 进行安装。