NPM使用与安装

NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种:

  • 允许用户从NPM服务器下载别人编写的第三方包到本地使用
  • 允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用
  • 允许用户将自己编写的包或命令行程序上传到NPM服务器供别人使用

nodejs已经集成了npm,以通过输入 "npm -v" 来测试是否成功安装。命令如下,出现版本提示表示安装成功:

$ npm -v
6.4.1

npm 命令来升级,命令如下:

$ sudo npm install npm -g

Window 系统使用以下命令即可:

npm install npm -g

使用 npm 命令安装模块

npm 安装 Node.js 模块语法格式如下:

$ npm install <Module Name>

使用 npm 命令安装常用的 Node.js web框架模块 express:

$ 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

查看安装信息

查看所有全局安装的模块:

$ npm list -g
某个模块的版本号,可以使用命令如下:

$ npm list grunt

package.json

package.json 位于模块的目录下,用于定义包的属性。

express 包的 package.json 文件,位于 node_modules/express/package.json 内容:

{
  "_from": "express@^4.16.2",
  "_id": "express@4.16.4",
  "_inBundle": false,
  "_integrity": "sha512-j12Uuyb4FMrd/qQAm6uCHAkPtO8FDTRJZBDd5D2KOL2eLaz1yUNdUB/NOIyq0iU4q4cFarsUCrnFDPBcnksuOg==",
  "_location": "/express",
  "_phantomChildren": {},
  "_requested": {
    "type": "range",
    "registry": true,
    "raw": "express@^4.16.2",
    "name": "express",
    "escapedName": "express",
    "rawSpec": "^4.16.2",
    "saveSpec": null,
    "fetchSpec": "^4.16.2"
  },
  "_requiredBy": [
    "/webpack-bundle-analyzer",
    "/webpack-dev-server"
  ],
  "_resolved": "https://registry.npmjs.org/express/-/express-4.16.4.tgz",
  "_shasum": "fddef61926109e24c515ea97fd2f1bdbf62df12e",
  "_spec": "express@^4.16.2",
  "_where": "C:\\Windows\\system32\\vue-demo\\node_modules\\webpack-bundle-analyzer",
  "author": {
    "name": "TJ Holowaychuk",
    "email": "tj@vision-media.ca"
  },
  "bugs": {
    "url": "https://github.com/expressjs/express/issues"
  },
  "bundleDependencies": false,
  "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.5",
    "array-flatten": "1.1.1",
    "body-parser": "1.18.3",
    "content-disposition": "0.5.2",
    "content-type": "~1.0.4",
    "cookie": "0.3.1",
    "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.1",
    "fresh": "0.5.2",
    "merge-descriptors": "1.0.1",
    "methods": "~1.1.2",
    "on-finished": "~2.3.0",
    "parseurl": "~1.3.2",
    "path-to-regexp": "0.1.7",
    "proxy-addr": "~2.0.4",
    "qs": "6.5.2",
    "range-parser": "~1.2.0",
    "safe-buffer": "5.1.2",
    "send": "0.16.2",
    "serve-static": "1.13.2",
    "setprototypeof": "1.1.0",
    "statuses": "~1.4.0",
    "type-is": "~1.6.16",
    "utils-merge": "1.0.1",
    "vary": "~1.1.2"
  },
  "deprecated": false,
  "description": "Fast, unopinionated, minimalist web framework",
  "devDependencies": {
    "after": "0.8.2",
    "connect-redis": "3.4.0",
    "cookie-parser": "~1.4.3",
    "cookie-session": "1.3.2",
    "ejs": "2.6.1",
    "eslint": "2.13.1",
    "express-session": "1.15.6",
    "hbs": "4.0.1",
    "istanbul": "0.4.5",
    "marked": "0.5.1",
    "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"
  },
  "engines": {
    "node": ">= 0.10.0"
  },
  "files": [
    "LICENSE",
    "History.md",
    "Readme.md",
    "index.js",
    "lib/"
  ],
  "homepage": "http://expressjs.com/",
  "keywords": [
    "express",
    "framework",
    "sinatra",
    "web",
    "rest",
    "restful",
    "router",
    "app",
    "api"
  ],
  "license": "MIT",
  "name": "express",
  "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.16.4"
}

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>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (desktop) geekes
version: (1.0.0)
description: node.js 测试模块(www.geekes.cn)
entry point: (index.js)
test command: make test
git repository: https://github.com/tengxu123456/geekes.git
keywords:
author:
license: (ISC)
About to write to C:\Users\zhang\Desktop\package.json:

{
  "name": "geekes", # 模块名
  "version": "1.0.0",
  "description": "node.js 测试模块(www.geekes.cn)", # 描述
  "main": "index.js",
  "directories": {
    "lib": "lib"
  },
  "scripts": {
    "test": "make test"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/tengxu123456/geekes.git"# Github 地址
About to write to ……/node_modules/package.json:      # 生成地址
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/tengxu123456/geekes/issues"
  },
  "homepage": "https://github.com/tengxu123456/geekes#readme"
}

Is this ok? (yes) yes

输入 "yes" 后会生成 package.json 文件

npm 资源库中注册用户(使用邮箱注册):

$ npm tixuan
Username: yixuan
Password:
Email: (this IS public) yixuan@gmail.com

用以下命令来发布模块:

$ npm publish

淘宝 NPM 镜像

  • 当前 registry.npm.taobao.org 是从 r.cnpmjs.org 进行全量同步的.
  • 当前 npm.taobao.org 运行版本是: cnpmjs.org@
  • 本系统运行在 Node.js.org 上.
  • 开源镜像: http://npm.taobao.org/mirrors
  • Node.js 镜像: http://npm.taobao.org/mirrors/node
  • alinode 镜像: http://npm.taobao.org/mirrors/alinode
  • phantomjs 镜像: http://npm.taobao.org/mirrors/phantomjs
  • ChromeDriver 镜像: http://npm.taobao.org/mirrors/chromedriver
  • OperaDriver 镜像: http://npm.taobao.org/mirrors/operadriver
  • Selenium 镜像: http://npm.taobao.org/mirrors/selenium
  • Node.js 文档镜像: http://npm.taobao.org/mirrors/node/latest/docs/api/index.html
  • NPM 镜像: https://npm.taobao.org/mirrors/npm/
  • electron 镜像: https://npm.taobao.org/mirrors/electron/
  • node-inspector 镜像: https://npm.taobao.org/mirrors/node-inspector/

因为npm安装插件是从国外服务器下载,受网络影响大,可能出现异常,

来自官网:“这是一个完整 npmjs.org 镜像,你可以用此代替官方版本(只读)

同步频率目前为 10分钟 一次以保证尽量与官方服务同步。”

cnpm的c 是 copy的意思,即复制 npm 上面的库。
把npm 上面的库复制到 国内的服务器上,当需要用的时候,使用 cnpm 命令获取,就会快很多了

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

安装模块

安装所有模块. 当安装的时候发现安装的模块还没有同步过来, 淘宝 NPM 会自动在后台进行同步, 下次你再安装这个模块的时候, 就会直接从 淘宝 NPM 安装

$ cnpm install [name]

同步模块

直接通过 sync 命令马上同步一个模块, 只有 cnpm 命令行才有此功能:

$ cnpm sync connect

当然, 你可以直接通过 web 方式来同步: /sync/connect

$ open https://npm.taobao.org/sync/connect

其它命令

支持 npm 除了 publish 之外的所有命令, 如:

$ cnpm info connect

版权声明:本文为qq_40861561原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。