Node.js NPM (程序包管理器)

  • NPM

    Node包管理器(NPM)提供两个主要功能-
    • 可在search.nodejs.org的在线存储库上搜索的node.js软件包/模块
    • 命令行实用程序,用于安装Node.js软件包,执行Node.js软件包的版本管理和依赖关系管理。
    v0.6.3版本之后,NPM与Node.js可安装程序捆绑在一起。要验证相同,请打开控制台并键入以下命令,然后查看结果-
    
    $ npm --version
    6.4.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
    
    这将产生类似的结果,但是模块将被全局安装。在这里,第一行显示模块版本和安装位置。
    
    /usr/lib
    └─┬ express@4.17.1 
      ├─┬ accepts@1.3.7 
      │ ├─┬ mime-types@2.1.27 
      │ │ └── mime-db@1.44.0 
      │ └── negotiator@0.6.2 
      ├── array-flatten@1.1.1 
      ├─┬ body-parser@1.19.0 
      │ ├── bytes@3.1.0 
      │ ├─┬ http-errors@1.7.2 
      │ │ ├── inherits@2.0.3 
      │ │ └── toidentifier@1.0.0 
      │ ├─┬ iconv-lite@0.4.24 
      │ │ └── safer-buffer@2.1.2 
      │ └── raw-body@2.4.0 
      ├── content-disposition@0.5.3 
      ├── content-type@1.0.4 
      ├── cookie@0.4.0 
      ├── cookie-signature@1.0.6 
      ├─┬ debug@2.6.9 
      │ └── ms@2.0.0 
      ├── depd@1.1.2 
      ├── encodeurl@1.0.2 
      ├── escape-html@1.0.3 
      ├── etag@1.8.1 
      ├─┬ finalhandler@1.1.2 
      │ └── unpipe@1.0.0 
      ├── fresh@0.5.2 
      ├── merge-descriptors@1.0.1 
      ├── methods@1.1.2 
      ├─┬ on-finished@2.3.0 
      │ └── ee-first@1.1.1 
      ├── parseurl@1.3.3 
      ├── path-to-regexp@0.1.7 
      ├─┬ proxy-addr@2.0.6 
      │ ├── forwarded@0.1.2 
      │ └── ipaddr.js@1.9.1 
      ├── qs@6.7.0 
      ├── range-parser@1.2.1 
      ├── safe-buffer@5.1.2 
      ├─┬ send@0.17.1 
      │ ├── destroy@1.0.4 
      │ ├── mime@1.6.0 
      │ └── ms@2.1.1 
      ├── serve-static@1.14.1 
      ├── setprototypeof@1.1.1 
      ├── statuses@1.5.0 
      ├─┬ type-is@1.6.18 
      │ └── media-typer@0.3.0 
      ├── utils-merge@1.0.1 
      └── vary@1.1.2 
    
    您可以使用以下命令来检查全局安装的所有模块-
  • 使用package.json

    package.json存在于任何Node应用程序/模块的根目录中,用于定义包的属性。让我们打开存在于node_modules/express/中的Express包的package.json
    
    {
      "_args": [
        [
          {
            "raw": "express",
            "scope": null,
            "escapedName": "express",
            "name": "express",
            "rawSpec": "",
            "spec": "latest",
            "type": "tag"
          },
          "/root"
        ]
      ],
      "_from": "express@latest",
      "_hasShrinkwrap": false,
      "_id": "express@4.17.1",
      "_inCache": true,
      "_location": "/express",
      "_nodeVersion": "8.16.0",
      "_npmOperationalInternal": {
        "host": "s3://npm-registry-packages",
        "tmp": "tmp/express_4.17.1_1558844734329_0.21547943776855627"
      },
      "_npmUser": {
        "name": "dougwilson",
        "email": "doug@somethingdoug.com"
      },
      "_npmVersion": "6.4.1",
      "_phantomChildren": {},
      "_requested": {
        "raw": "express",
        "scope": null,
        "escapedName": "express",
        "name": "express",
        "rawSpec": "",
        "spec": "latest",
        "type": "tag"
      },
      "_requiredBy": [
        "#USER"
      ],
      "_resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz",
      "_shasum": "4491fc38605cf51f8629d39c2b5d026f98a4c134",
      "_shrinkwrap": null,
      "_spec": "express",
      "_where": "/root",
      "author": {
        "name": "TJ Holowaychuk",
        "email": "tj@vision-media.ca"
      },
      "bugs": {
        "url": "https://github.com/expressjs/express/issues"
      },
      "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"
        }
      ],
      "dependencies": {
        "accepts": "~1.3.7",
        "array-flatten": "1.1.1",
        "body-parser": "1.19.0",
        "content-disposition": "0.5.3",
        "content-type": "~1.0.4",
        "cookie": "0.4.0",
        "cookie-signature": "1.0.6",
        "debug": "2.6.9",
        "depd": "~1.1.2",
        "encodeurl": "~1.0.2",
        "escape-html": "~1.0.3",
        "etag": "~1.8.1",
        "finalhandler": "~1.1.2",
        "fresh": "0.5.2",
        "merge-descriptors": "1.0.1",
        "methods": "~1.1.2",
        "on-finished": "~2.3.0",
        "parseurl": "~1.3.3",
        "path-to-regexp": "0.1.7",
        "proxy-addr": "~2.0.5",
        "qs": "6.7.0",
        "range-parser": "~1.2.1",
        "safe-buffer": "5.1.2",
        "send": "0.17.1",
        "serve-static": "1.14.1",
        "setprototypeof": "1.1.1",
        "statuses": "~1.5.0",
        "type-is": "~1.6.18",
        "utils-merge": "1.0.1",
        "vary": "~1.1.2"
      },
      "description": "Fast, unopinionated, minimalist web framework",
      "devDependencies": {
        "after": "0.8.2",
        "connect-redis": "3.4.1",
        "cookie-parser": "~1.4.4",
        "cookie-session": "1.3.3",
        "ejs": "2.6.1",
        "eslint": "2.13.1",
        "express-session": "1.16.1",
        "hbs": "4.0.4",
        "istanbul": "0.4.5",
        "marked": "0.6.2",
        "method-override": "3.0.0",
        "mocha": "5.2.0",
        "morgan": "1.9.1",
        "multiparty": "4.2.1",
        "pbkdf2-password": "1.2.1",
        "should": "13.2.3",
        "supertest": "3.3.0",
        "vhost": "~3.0.2"
      },
      "directories": {},
      "dist": {
        "integrity": "sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==",
        "shasum": "4491fc38605cf51f8629d39c2b5d026f98a4c134",
        "tarball": "https://registry.npmjs.org/express/-/express-4.17.1.tgz",
        "fileCount": 16,
        "unpackedSize": 208133,
        "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpj\r\n...."
      },
      "engines": {
        "node": ">= 0.10.0"
      },
      "gitHead": "e1b45ebd050b6f06aa38cda5aaf0c21708b0c71e",
      "homepage": "http://expressjs.com/",
      "keywords": [
        "express",
        "framework",
        "sinatra",
        "web",
        "rest",
        "restful",
        "router",
        "app",
        "api"
      ],
      "license": "MIT",
      "maintainers": [
        {
          "name": "dougwilson",
          "email": "doug@somethingdoug.com"
        },
        {
          "name": "jasnell",
          "email": "jasnell@gmail.com"
        },
        {
          "name": "mikeal",
          "email": "mikeal.rogers@gmail.com"
        }
      ],
      "name": "express",
      "optionalDependencies": {},
      "readme": "ERROR: No README data found!",
      "repository": {
        "type": "git",
        "url": "git+https://github.com/expressjs/express.git"
      },
      "scripts": {
        "lint": "eslint .",
        "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/"
      },
      "version": "4.17.1"
    }
    
    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) mcmohd@gmail.com
    
    现在是时候发布您的模块了-
    
    $ npm publish
    
    如果您的模块一切正常,那么它将像任何其他Node.js模块一样在存储库中发布并可以使用NPM进行安装。