00. 概述
本文描述的是安装在macOS Mojave 10.14.4 中的nodejs/npm
npm是随同nodejs一起安装的包管理工具,能解决nodejs代码部署上的很多问题,常见的使用场景有以下几种:
- 允许用户从npm服务器下载别人编写的第三方包到本地使用。
- 允许用户从npm服务器下载并安装别人编写的命令行程序到本地使用。
- 允许用户将自己编写的包或命令行程序上传到npm服务器供别人使用。
安装nodejs时, 会同时安装npm:
/usr/local/Cellar/node
/usr/local/Cellar/node/12.8.0/libexec/lib/node_modules/npm
使用"npm install npm -g"安装的npm, 位于其它目录:
/usr/local/lib/node_modules/npm
01. npm -v
查看npm版本号
1 Weizhens-Mac-mini:nodejs weizhen$ npm -v
2 6.10.3
02. npm install npm -g
重新安装npm, 也就是更新到最新版本
1 Weizhens-Mac-mini:nodejs weizhen$ npm install npm -g 2 /usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js 3 /usr/local/bin/npx -> /usr/local/lib/node_modules/npm/bin/npx-cli.js 4 + npm@6.10.3 5 updated 1 package in 70.494s
03. npm install express / npm install express -g / npm install express -s
本地安装express(没有-g参数), 将express安装到当前目录中(/Users/weizhen/Sites/nodejs/), 生成的文件目录与下面的版本不同, 由于目录中没有package.json文件, 会有警告;
1 Weizhens-Mac-mini:nodejs weizhen$ npm install express
2 npm WARN saveError ENOENT: no such file or directory, open '/Users/weizhen/Sites/nodejs/package.json'
3 npm WARN enoent ENOENT: no such file or directory, open '/Users/weizhen/Sites/nodejs/package.json'
4 npm WARN nodejs No description
5 npm WARN nodejs No repository field.
6 npm WARN nodejs No README data
7 npm WARN nodejs No license field.
8
9 + express@4.17.1
10 updated 1 package and audited 286 packages in 4.899s
11 found 0 vulnerabilities
全局安装express(存在-g参数), 将express装到默认目录中(/usr/local/lib/node_modules/express/)。后续命令中的-g参数与此类似, 不再重复说明
1 Weizhens-Mac-mini:nodejs weizhen$ npm install express -g
2 + express@4.17.1
3 added 49 packages from 31 contributors and updated 1 package in 27.126s
存在-s参数时, 安装结束后, 会自动将依赖关系写入package.json文件中, 参考链接
04. npm list / npm ls
查看安装在当前目录的所有模块
1 Weizhens-Mac-mini:nodejs weizhen$ npm list
2 /Users/weizhen/Sites/nodejs
3 └─┬ express@4.17.1
4 ├─┬ accepts@1.3.7
5 │ ├─┬ mime-types@2.1.24
6 │ │ └── mime-db@1.40.0
7 ... ... 内容过多, 本文省略 ... ...
8 │ └── mime-types@2.1.24 deduped
9 ├── utils-merge@1.0.1
10 └── vary@1.1.2
11
12 npm ERR! extraneous: ms@2.1.1 /Users/weizhen/Sites/nodejs/node_modules/send/node_modules/ms
05. npm list express
查看安装在当前目录的express的版本号
1 Weizhens-Mac-mini:nodejs weizhen$ npm list express
2 /Users/weizhen/Sites/nodejs
3 └── express@4.17.1
06. npm update express
更新当前目录中的express
1 Weizhens-Mac-mini:nodejs weizhen$ npm update express
07. npm search express
获取本地安装的express描述信息
1 Weizhens-Mac-mini:nodejs weizhen$ npm search express
2 NAME | DESCRIPTION | AUTHOR | DATE | VERSION | KEYWORDS
3 express | Fast,… | =dougwilson… | 2019-05-26 | 4.17.1 | express framew ... ... ... uter app api
4 path-to-regexp | Express style path… | =blakeembrey… | 2019-01-14 | 3.0.0 | express regexp route routing
5 cors | Node.js CORS… | =dougwilson… | 2018-11-04 | 2.8.5 | cors express connect middleware
6 morgan | HTTP request logger… | =dougwilson | 2018-09-11 | 1.9.1 | express http logger middleware
7 apollo-server-express | Production-ready… | =apollo-bot… | 2019-07-31 | 2.8.1 | GraphQL Apollo ... ... ... nect Javascript
8 @babel/helper-member-expr | Helper function to… | =danez… | 2019-07-17 | 7.5.5 |
9 ession-to-functions | | | | |
10 helmet | help secure… | =adam_baldwin… | 2019-07-24 | 3.20.0 | security header ... ... ... sts clickjack
11 serve-favicon | favicon serving… | =dougwilson | 2018-03-29 | 2.5.0 | express favicon middleware
12 express-graphql | Production ready… | =ags-… | 2019-07-15 | 0.9.0 | express restify ... ... ... ddleware api
13 express-validator | Express middleware… | =ctavan… | 2019-07-01 | 6.1.1 | express validat ... ... ... nitization xss
14 @babel/plugin-transform-n | Compile regular… | =danez… | 2019-05-21 | 7.4.5 | babel-plugin re ... ... ... pressions
15 amed-capturing-groups-reg | | | | |
16 ex | | | | |
17 @nestjs/platform-express | Nest - modern,… | =brunnerlivio… | 2019-07-18 | 6.5.3 |
18 escape-string-regexp | Escape RegExp… | =sindresorhus | 2019-04-17 | 2.0.0 | escape regex re ... ... ... cial characters
19 emoji-regex | A regular… | =mathias | 2019-03-05 | 8.0.0 | unicode regex r ... ... ... acters emoji
20 anymatch | Matches strings… | =doowb =es128… | 2019-07-07 | 3.0.3 | match any strin ... ... ... ession function
21 aws-serverless-express | This library… | =amzn-oss… | 2019-03-26 | 3.3.6 | aws serverless ... ... ... mbda express
22 ignore | Ignore is a manager… | =kael | 2019-08-14 | 5.1.4 | ignore .gitigno ... ... ... atch glob as
23 @svgr/babel-plugin-remove | Remove JSX empty… | =neoziro | 2019-04-11 | 4.2.0 | babel-plugin
24 -jsx-empty-expression | | | | |
25 express-fileupload | Simple express file… | =richardgirges | 2019-06-07 | 1.1.5 | express file-up ... ... ... iddleware
26 express-session | Simple session… | =dougwilson… | 2019-06-12 | 1.16.2 |
08. npm uninstall express
卸载本地安装的express. 如果当前目录没有提供正确的package.json文件, 会导致卸载失败
1 Weizhens-Mac-mini:nodejs weizhen$ npm uninstall express
2 npm WARN saveError ENOENT: no such file or directory, open '/Users/weizhen/Sites/nodejs/package.json'
3 npm WARN enoent ENOENT: no such file or directory, open '/Users/weizhen/Sites/nodejs/package.json'
4 npm WARN nodejs No description
5 npm WARN nodejs No repository field.
6 npm WARN nodejs No README data
7 npm WARN nodejs No license field.
8
9 removed 1 package and audited 160 packages in 2.791s
10 found 0 vulnerabilities
09. npm view cookie-parser versions
查看npm服务器上所有的cookie-parser版本信息
1 Weizhens-Mac-mini:nodejs weizhen$ npm view cookie-parser versions
2 [
3 '1.0.0', '1.0.1', '1.1.0',
4 '1.2.0', '1.3.0', '1.3.1',
5 '1.3.2', '1.3.3', '1.3.4',
6 '1.3.5', '1.4.0', '1.4.1',
7 '1.4.2', '1.4.3', '1.4.4'
8 ]
10. npm view cookie-parser version
查看cookie-parser的最新的版本是哪一个
1 Weizhens-Mac-mini:nodejs weizhen$ npm view cookie-parser version
2 1.4.4
11. npm info cookie-parser
这种方式和第一种类似,也可以查看cookie-parser所有的版本, 但是能查出更多的关于cookie-parser的信息
1 Weizhens-Mac-mini:nodejs weizhen$ npm info cookie-parser
2
3 cookie-parser@1.4.4 | MIT | deps: 2 | versions: 15
4 Parse HTTP request cookies
5 https://github.com/expressjs/cookie-parser#readme
6
7 keywords: cookie, middleware
8
9 dist
10 .tarball: https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.4.tgz
11 .shasum: e6363de4ea98c3def9697b93421c09f30cf5d188
12 .integrity: sha512-lo13tqF3JEtFO7FyA49CqbhaFkskRJ0u/UAiINgrIXeRCY41c88/zxtrECl8AKH3B0hj9q10+h3Kt8I7KlW4tw==
13 .unpackedSize: 11.2 kB
14
15 dependencies:
16 cookie-signature: 1.0.6 cookie: 0.3.1
17
18 maintainers:
19 - defunctzombie <shtylman@gmail.com>
20 - dougwilson <doug@somethingdoug.com>
21
22 dist-tags:
23 latest: 1.4.4
24
25 published 6 months ago by dougwilson <doug@somethingdoug.com>
12. npm init
在当前目录,生成并初始化 package.json 文件
1 Weizhens-Mac-mini:webcrawler weizhen$ npm init 2 This utility will walk you through creating a package.json file. 3 It only covers the most common items, and tries to guess sensible defaults. 4 5 See `npm help json` for definitive documentation on these fields 6 and exactly what they do. 7 8 Use `npm install <pkg>` afterwards to install a package and 9 save it as a dependency in the package.json file. 10 11 Press ^C at any time to quit. 12 package name: (webcrawler) 13 version: (1.0.0) 14 description: webcrawler 15 entry point: (index.js) 16 test command: none 17 git repository: none 18 keywords: none 19 author: weizhen 20 license: (ISC) 21 About to write to /Users/weizhen/Sites/nodejs/webcrawler/package.json: 22 23 { 24 "name": "webcrawler", 25 "version": "1.0.0", 26 "description": "webcrawler", 27 "main": "index.js", 28 "scripts": { 29 "test": "none" 30 }, 31 "repository": { 32 "type": "git", 33 "url": "none" 34 }, 35 "keywords": [ 36 "none" 37 ], 38 "author": "weizhen", 39 "license": "ISC" 40 } 41 42 Is this OK? (yes)
除了本章介绍的部分外,NPM还提供了很多功能,package.json里也有很多其它有用的字段。
除了可以在npmjs.org/doc/查看官方文档外,这里再介绍一些NPM常用命令。
- 使用npm help可查看所有命令。
- 使用npm help <command>可查看某条命令的详细帮助,例如npm help install。
- 使用npm publish可以发布自己创建的模块。
- 使用npm unpublish <package>@<version>可以撤销发布自己发布过的某个版本代码。
- 在package.json所在目录下使用npm install . -g可先在本地安装当前命令行程序,可用于发布前的本地测试。
- 使用npm cache clear可以清空NPM本地缓存,用于对付使用相同版本号发布新版本代码的人。