1、NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种:
>允许用户从NPM服务器下载别人编写的第三方包到本地使用。
>允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用。
>允许用户将自己编写的包或者命令行程序上传到NPM服务器供别人使用。
注意:由于新版的Nodejs已经集成npm,所以之前NPM也一起安装好的了。可以使用“npm -v”来测试安装是否成功,例如:
2、Ubuntu升级npm的命令为:
$ sudo npm install npm -g
Windows升级npm命令为:
npm install npm -g
3、使用npm命令安装模块:
$ npm install <Module Name>
例如使用npm命令安装常用的Node.js web 框架模块 express:
$ npm install express
安装好之后,express包就放在了工程目录的下面的node_modules目录中,因此在代码中只需要通过require('express')的方式就好,无需指定第三方包的路径。如下图所示:
Windows全局安装express的方法为:
$ npm install express -g
安装成功之后会在第一行显示出其安装的位置:
express@4.16.4 C:\Users\asus\AppData\Roaming\npm\node_modules\express
├── escape-html@1.0.3
├── array-flatten@1.1.1
├── cookie-signature@1.0.6
├── encodeurl@1.0.2
├── utils-merge@1.0.1
├── cookie@0.3.1
├── etag@1.8.1
├── merge-descriptors@1.0.1
├── range-parser@1.2.0
├── path-to-regexp@0.1.7
├── content-type@1.0.4
├── fresh@0.5.2
├── content-disposition@0.5.2
├── methods@1.1.2
├── setprototypeof@1.1.0
├── vary@1.1.2
├── parseurl@1.3.2
├── safe-buffer@5.1.2
├── serve-static@1.13.2
├── statuses@1.4.0
├── depd@1.1.2
├── qs@6.5.2
├── on-finished@2.3.0 (ee-first@1.1.1)
├── debug@2.6.9 (ms@2.0.0)
├── finalhandler@1.1.1 (unpipe@1.0.0)
├── proxy-addr@2.0.4 (forwarded@0.1.2, ipaddr.js@1.8.0)
├── accepts@1.3.5 (negotiator@0.6.1, mime-types@2.1.22)
├── type-is@1.6.16 (media-typer@0.3.0, mime-types@2.1.22)
├── send@0.16.2 (ms@2.0.0, destroy@1.0.4, mime@1.4.1, http-errors@1.6.3)
└── body-parser@1.18.3 (bytes@3.0.0, raw-body@2.3.3, http-errors@1.6.3, iconv-lite@0.4.23)
4、查看安装信息:
E:\Code\HBuilderX\FirstNodejs>npm list -g
C:\Users\asus\AppData\Roaming\npm
└─┬ express@4.16.4
├─┬ accepts@1.3.5
│ ├─┬ mime-types@2.1.22
│ │ └── mime-db@1.38.0
│ └── negotiator@0.6.1
├── array-flatten@1.1.1
├─┬ body-parser@1.18.3
│ ├── bytes@3.0.0
│ ├─┬ http-errors@1.6.3
│ │ └── inherits@2.0.3
│ ├─┬ iconv-lite@0.4.23
│ │ └── safer-buffer@2.1.2
│ └─┬ raw-body@2.3.3
│ └── unpipe@1.0.0
......
5、查看某个模块的版本号:
E:\Code\HBuilderX\FirstNodejs>npm list express
E:\Code\HBuilderX\FirstNodejs
└── express@4.16.4
6、使用package.json
package.json位于模块的目录下,用于定义包的属性。里面的内容是:
{
"name": "express",
"description": "Fast, unopinionated, minimalist web framework",
"version": "4.16.4",
"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": "git+https://github.com/expressjs/express.git"
},
"homepage": "http://expressjs.com/",
"keywords": [
"express",
"framework",
"sinatra",
"web",
"rest",
"restful",
"router",
"app",
"api"
],
"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"
},
"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"
},
"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/"
},
"gitHead": "dc538f6e810bd462c98ee7e6aae24c64d4b1da93",
"bugs": {
"url": "https://github.com/expressjs/express/issues"
},
"_id": "express@4.16.4",
"_npmVersion": "6.4.1",
"_nodeVersion": "8.12.0",
"_npmUser": {
"name": "dougwilson",
"email": "doug@somethingdoug.com"
},
"dist": {
"integrity": "sha512-j12Uuyb4FMrd/qQAm6uCHAkPtO8FDTRJZBDd5D2KOL2eLaz1yUNdUB/NOIyq0iU4q4cFarsUCrnFDPBcnksuOg==",
"shasum": "fddef61926109e24c515ea97fd2f1bdbf62df12e",
"tarball": "https://registry.npmjs.org/express/-/express-4.16.4.tgz",
"fileCount": 16,
"unpackedSize": 206123,
"npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJbvsqSCRA9TVsSAnZWagAAPFwP/iCvznxNrmvgY9ox7w2k\ncS/ej9HZJ5NGjBEWtae1F2bjJ2V7rOxVTGTlqiPMSNIzTgw3fpFkIXp9kCA4\nY03NOsYUjYscGjXR6f2fvOVJ/Si5FKlqr7Ow6WMBClrdo/CMCc8kH9fxtPja\nHla58xiU7ftlzUHIjGmmnHFzAjAeGj+3e3v1omuoeP6mPuxlwYoQ0MuD0sFa\n9qJAFZ0MBrfvoQBI8G++GZZhxalhefuibWi1ErRw3F5cLfvhjKi4HGPh+sDu\nc63D99wQIJIq4HumwX0JNW7OywuL28wgxgtvKyg0iCVR/BnAYiEA0UZUVI4h\nsX1Kuht1oHEp1iGOvGALYotPiovnDCAra+2zPM1p8oZKdXHEpkAygG3mCiD5\nyWlWrFo5jJudULWzMtHp6F0RwQJjpSavnkbusKWZvO717/1Ku5FIM4cnTWVK\nELGmb011jRPMvwFqv1C04SvhBT+UrXe4kd0qwJWQEDT1aWzbjbaroPmVQ+l1\nxzUkHRHm7vYCBE0RxQ4FImNWlYYQVVyBSroYwxvJnP6H8m/DR7oxDPDoJcBn\nXXETuH8Ca+q8KjwdrstVXCwKfB+zs0Z41/oOWKrhsDY2B9HwmyOOA8EtG4QO\np2waBrigD8L4T/Y3II4T144z2MclVid7DulrzKCMiE6yoTqvrH64FfWkLQqG\n11u8\r\n=ZruB\r\n-----END PGP SIGNATURE-----\r\n"
},
"maintainers": [
{
"name": "dougwilson",
"email": "doug@somethingdoug.com"
},
{
"name": "hacksparrow",
"email": "captain@hacksparrow.com"
},
{
"name": "jasnell",
"email": "jasnell@gmail.com"
},
{
"name": "mikeal",
"email": "mikeal.rogers@gmail.com"
}
],
"directories": {},
"_npmOperationalInternal": {
"host": "s3://npm-registry-packages",
"tmp": "tmp/express_4.16.4_1539230354097_0.4680196437483164"
},
"_hasShrinkwrap": false,
"_shasum": "fddef61926109e24c515ea97fd2f1bdbf62df12e",
"_resolved": "https://registry.npmjs.org/express/-/express-4.16.4.tgz",
"_from": "express@latest",
"readme": "ERROR: No README data found!"
}
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 - 关键字
7、卸载模块
$ npm uninstall express
卸载之后可以使用以下命令查看安装的包是否还存在:
$ npm ls
更新模块:
$ npm update express
搜索模块:
$ npm search express
创建模块:
创建模块,package.json 文件是必不可少的。我们可以使用 NPM 生成 package.json 文件,生成的文件包含了基本的结果。
E:\Code\HBuilderX\FirstNodejs>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> --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: (FirstNodejs) toohoo
version: (1.0.0)
description: Node.js 测试模块(www.example.com) # 描述
entry point: (index.js)
test command: make test
git repository: http://github.com/too-hoo/toohoo.git #Github 地址
keywords: 123
author: toohoo
license: (ISC)
About to write to E:\Code\HBuilderX\FirstNodejs\package.json:
{
"name": "toohoo",
"version": "1.0.0",
"description": "Node.js 测试模块(www.example.com) ",
"main": "index.js",
"dependencies": {},
"devDependencies": {},
"scripts": {
"test": "make test"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/too-hoo/toohoo.git%20.git"
},
"keywords": [
"123"
],
"author": "toohoo",
"license": "ISC",
"bugs": {
"url": "https://github.com/too-hoo/toohoo.git%20/issues"
},
"homepage": "https://github.com/too-hoo/toohoo.git%20/tree/Github%20%E5%9C%B0%E5%9D%80#readme"
}
Is this ok? (yes) yes
以上的信息,你需要根据你自己的情况输入。在最后输入 "yes" 后会生成 package.json 文件。
接下来我们可以使用以下命令在 npm 资源库中注册用户(使用邮箱注册):
E:\Code\HBuilderX\FirstNodejs>npm adduser
Username: toohoo
Password:
Email: (this IS public) toohoo@163.com
接下来我们就用以下命令来发布模块:
$ npm publish
8、npm的常用命令:
除了本章介绍的部分外,NPM还提供了很多功能,package.json里也有很多其它有用的字段。
除了可以在npmjs.org/doc/查看官方文档外,这里再介绍一些NPM常用命令。
NPM提供了很多命令,例如install和publish,使用npm help可查看所有命令。
- NPM提供了很多命令,例如
install
和publish
,使用npm help
可查看所有命令。 - 使用
npm help <command>
可查看某条命令的详细帮助,例如npm help install
。 - 在
package.json
所在目录下使用npm install . -g
可先在本地安装当前命令行程序,可用于发布前的本地测试。 - 使用
npm update <package>
可以把当前目录下node_modules
子目录里边的对应模块更新至最新版本。 - 使用
npm update <package> -g
可以把全局安装的对应命令行程序更新至最新版。 - 使用
npm cache clear
可以清空NPM本地缓存,用于对付使用相同版本号发布新版本代码的人。 - 使用
npm unpublish <package>@<version>
可以撤销发布自己发布过的某个版本代码。
9、可以时候用淘宝npm镜像,有点是快,10分钟与官方镜像同步一次。
$ npm install -g cnpm --registry=https://registry.npm.taobao.org
这样就可以使用 cnpm 命令来安装模块了:
$ cnpm install [name]