说说包管理工具 NPM 的基本用法
NPM 是随同 NodeJS 一起安装的包管理工具,可以解决 NodeJS 代码部署上的很多问题,常见的使用场景有以下几种:
- 允许我们从 NPM 服务器下载第三方编写的包到本地使用。
- 允许我们从 NPM 服务器下载并安装第三方编写的命令行程序到本地使用。
- 允许我们将自己编写的包或命令行程序上传到 NPM 服务器供他人使用。
新版的 nodejs 已经集成了 npm ,所以 npm 不需要单独安装。
1 淘宝 NPM 镜像
因为国内直接使用 npm 的官方镜像非常慢,所以推荐使用淘宝 NPM 镜像。这是一个完整 npmjs.org 镜像,我们可以用此代替官方版本(只读),同步频率目前为 10分钟 一次,保证与官方服务同步。
安装指令:
npm install -g cnpm --registry=http://registry.npm.taobao.org
也可以用此指令,更新淘宝 NPM 镜像包。
安装成功后,就可以使用 cnpm 命令来安装模块了:
cnpm install [name]
淘宝 NPM 镜像完整说明:http://npm.taobao.org/
2 安装模块
npm 安装 Node.js 模块语法格式如下:
npm install <Module Name>
比如想安装 request 模块,就可以这样:
npm install request
不过还是建议用 cnpm 指令来安装模块,会更快些。
然后就可以通过 require 调用我们刚刚安装的模块。在 WebStorm 中,会弹出提示,让我们把模块写到 package.json 文件中。
通过 Alt + Shift + Enter 指令,就可以自动创建 package.json 文件并添加好对应的模块:
{"name": "deniro-nodejs","version": "1.0.0","dependencies": {"request": "latest"}
}
3 全局安装与本地安装
npm 的包安装分为本地安装(local)、全局安装(global)两种,全局安装命令比本地安装命令,多了一个 -g
。
cnpm install request # 本地安装
cnpm install request -g #全局安装
二者区别如下:
安装包路径 | 调用方式 | |
---|---|---|
本地安装 | 放在 ./node_modules 下(运行 npm 命令时所在的目录),如果没有 node_modules 目录,会在当前执行 npm 命令的目录下生成 node_modules 目录。 | 通过 require() 来引入本地安装的包。 |
全局安装 | 放在 /usr/local 下或者 node.js 的安装目录。 | 可以直接在命令行里使用。 |
全局安装 request 模块时,会输出如下内容:
[1/20] aws-sign2@~0.7.0 installed at node_modules\_aws-sign2@0.7.0@aws-sign2
[2/20] aws4@^1.8.0 installed at node_modules\_aws4@1.9.1@aws4
[3/20] caseless@~0.12.0 installed at node_modules\_caseless@0.12.0@caseless
[4/20] is-typedarray@~1.0.0 installed at node_modules\_is-typedarray@1.0.0@is-typedarray
[5/20] forever-agent@~0.6.1 installed at node_modules\_forever-agent@0.6.1@forever-agent
[6/20] isstream@~0.1.2 installed at node_modules\_isstream@0.1.2@isstream
[7/20] oauth-sign@~0.9.0 installed at node_modules\_oauth-sign@0.9.0@oauth-sign
[8/20] performance-now@^2.1.0 installed at node_modules\_performance-now@2.1.0@performance-now
[9/20] json-stringify-safe@~5.0.1 installed at node_modules\_json-stringify-safe@5.0.1@json-stringify-safe
[10/20] combined-stream@~1.0.6 installed at node_modules\_combined-stream@1.0.8@combined-stream
[11/20] form-data@~2.3.2 installed at node_modules\_form-data@2.3.3@form-data
[12/20] extend@~3.0.2 installed at node_modules\_extend@3.0.2@extend
[13/20] qs@~6.5.2 installed at node_modules\_qs@6.5.2@qs
[14/20] mime-types@~2.1.19 installed at node_modules\_mime-types@2.1.26@mime-types
[15/20] safe-buffer@^5.1.2 installed at node_modules\_safe-buffer@5.2.0@safe-buffer
[16/20] tunnel-agent@^0.6.0 installed at node_modules\_tunnel-agent@0.6.0@tunnel-agent
[17/20] uuid@^3.3.2 installed at node_modules\_uuid@3.4.0@uuid
[18/20] tough-cookie@~2.5.0 installed at node_modules\_tough-cookie@2.5.0@tough-cookie
[19/20] har-validator@~5.1.3 installed at node_modules\_har-validator@5.1.3@har-validator
[20/20] http-signature@~1.2.0 installed at node_modules\_http-signature@1.2.0@http-signature
All packages installed (47 packages installed from npm registry, used 1s(network 1s), speed 857.68kB/s, json 46(93.67kB), tarball 968.13kB)
4 常用指令
(1)查看所有全局安装的模块
npm list -g
输出形如:
| | | +-- aproba@2.0.0 deduped
| | | +-- figgy-pudding@3.5.1 deduped
| | | `-- npm-registry-fetch@4.0.3 deduped
| | +-- npm-registry-fetch@4.0.3
| | | +-- bluebird@3.5.5 deduped
| | | +-- figgy-pudding@3.5.1 deduped
| | | +-- JSONStream@1.3.5 deduped
| | | +-- lru-cache@5.1.1 deduped
...
(2)查看某个模块的版本号
npm list [name]
输出形如:
deniro-nodejs@1.0.0 F:\WebstormProjects\deniro-nodejs
`-- request@2.88.2
输出格式为:projectName@projectVersion /path/to/project/folder。
(3)更新模块指令
npm update [name]
(4)搜索模块指令
npm search [name]
比如搜索 request 模块,会输出以下内容:
NAME | DESCRIPTION | AUTHOR | DATE | VERSION | KEYWORDSsuperagent | elegant & feature… | =defunctzombie… | 2020-02-17 | 5.2.2 | agent ajax ajax api async await axios cancel client frisbee got http http h
tt
request-promise | The simplified HTTP… | =analog-nico… | 2019-11-04 | 4.2.5 | xhr http https promise request then thenable bluebird
request-promise-native | The simplified HTTP… | =analog-nico… | 2019-11-04 | 1.0.8 | xhr http https promise request then thenable native
needle | The leanest and… | =tomas | 2020-04-03 | 2.4.1 | http https simple request client multipart upload proxy deflate timeout char
s
request-progress | Tracks the download… | =satazor | 2016-12-01 | 3.0.0 | progress request mikeal size bytes percent percentage speed eta etr
supertest | SuperAgent driven… | =defunctzombie… | 2019-03-15 | 4.0.2 | superagent request tdd bdd http test testing
got | Human-friendly and… | =sindresorhus… | 2020-03-24 | 10.7.0 | http https http2 get got url uri request simple curl wget fetch net networke
graphql-request | [![CircleCI](https:… | =schickling… | 2018-08-10 | 1.8.2 | graphql request fetch graphql-client apollo
morgan | HTTP request logger… | =dougwilson | 2020-03-20 | 1.10.0 | express http logger middleware
axios-mock-adapter | Axios adapter that… | =ctimmerm | 2020-03-22 | 1.18.1 | axios test mock request stub adapter
cookie-parser | Parse HTTP request… | =defunctzombie… | 2020-03-15 | 1.4.5 | cookie middleware
raf | requestAnimationFra… | =chrisdickinson… | 2018-11-02 | 3.4.1 | requestAnimationFrame polyfill
request-promise-core | Core Promise… | =analog-nico | 2019-11-03 | 1.1.3 | xhr http https promise request then thenable core
popsicle | Advanced HTTP… | =blakeembrey | 2019-11-03 | 12.0.5 | request http middleware node ajax browser promise
sync-request | Make synchronous… | =forbeslindesay | 2019-03-17 | 6.1.0 | request http https cache browserify synchronous sync
http-signature | Reference… | =arekinath… | 2020-04-02 | 1.3.4 | https request
cacheable-request | Wrap native HTTP… | =lukechilds | 2020-01-21 | 7.0.1 | HTTP HTTPS cache caching layer cacheable RFC 7234 RFC 7234 compliant
light-my-request | Fake HTTP injection… | =delvedor… | 2020-03-26 | 3.7.4 | http inject fake request server
proxy-addr | Determine address… | =dougwilson | 2020-02-24 | 2.0.6 | ip proxy x-forwarded-for
make-fetch-happen | Opinionated,… | =ruyadorno… | 2020-03-12 | 8.0.4 | http request fetch mean girls caching cache subresource integrity
(5)查看 npm 版本号
npm -v
5 package.json
package.json 位于模块包的根目录下(模块安装成功后,会放在 node_modules 下),用于定义模块包的属性。比如,request 模块的 package.json 配置如下:
{"name": "request","description": "Simplified HTTP request client.","keywords": ["http","simple","util","utility"],"version": "2.88.2","author": "Mikeal Rogers <mikeal.rogers@gmail.com>","repository": {"type": "git","url": "https://github.com/request/request.git"},"bugs": {"url": "http://github.com/request/request/issues"},"license": "Apache-2.0","engines": {"node": ">= 6"},"main": "index.js","files": ["lib/","index.js","request.js"],"dependencies": {"aws-sign2": "~0.7.0","aws4": "^1.8.0","caseless": "~0.12.0","combined-stream": "~1.0.6","extend": "~3.0.2","forever-agent": "~0.6.1","form-data": "~2.3.2","har-validator": "~5.1.3","http-signature": "~1.2.0","is-typedarray": "~1.0.0","isstream": "~0.1.2","json-stringify-safe": "~5.0.1","mime-types": "~2.1.19","oauth-sign": "~0.9.0","performance-now": "^2.1.0","qs": "~6.5.2","safe-buffer": "^5.1.2","tough-cookie": "~2.5.0","tunnel-agent": "^0.6.0","uuid": "^3.3.2"},"scripts": {"test": "npm run lint && npm run test-ci && npm run test-browser","test-ci": "taper tests/test-*.js","test-cov": "nyc --reporter=lcov tape tests/test-*.js","test-browser": "node tests/browser/start.js","lint": "standard"},"devDependencies": {"bluebird": "^3.2.1","browserify": "^13.0.1","browserify-istanbul": "^2.0.0","buffer-equal": "^1.0.0","codecov": "^3.0.4","coveralls": "^3.0.2","function-bind": "^1.0.2","karma": "^3.0.0","karma-browserify": "^5.0.1","karma-cli": "^1.0.0","karma-coverage": "^1.0.0","karma-phantomjs-launcher": "^1.0.0","karma-tap": "^3.0.1","nyc": "^14.1.1","phantomjs-prebuilt": "^2.1.3","rimraf": "^2.2.8","server-destroy": "^1.0.1","standard": "^9.0.0","tape": "^4.6.0","taper": "^0.5.0"},"greenkeeper": {"ignore": ["hawk","har-validator"]},"__npminstall_done": "Sat Apr 18 2020 10:38:30 GMT+0800 (GMT+08:00)","_from": "request@2.88.2","_resolved": "https://registry.npm.taobao.org/request/download/request-2.88.2.tgz?cache=0&sync_timestamp=1581439349114&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Frequest%2Fdownload%2Frequest-2.88.2.tgz"
}
具体属性说明如下:
属性 | 说明 |
---|---|
name | 包名。 |
version | 包的版本号。 |
description | 描述。 |
homepage | 官网主页 url 。 |
author | 作者姓名。 |
contributors | 贡献者姓名。 |
dependencies | 依赖包列表。如果依赖包没有安装,npm 会自动将依赖包安装在 node_module 目录下。 |
repository | 包代码托管类型,可以是 git 或 svn,git 可在 Github 上。 |
main | 指定了程序的主入口文件,require(‘moduleName’) 就会加载这个文件。这个字段的默认值是模块根目录下面的 index.js。 |
keywords | 关键字 |
6 创建模块
我们所开发的项目,其实本质上也是模块,因此也是通过 npm 进行管理的。
我们可以使用 NPM 生成 package.json 文件,生成的文件包含了这个项目的一些基本的内容。
创建模块指令为:npm init
,执行过程中会以交互的方式,让我们输入包名、版本号、描述、入口文件、测试命令、git 仓库地址、关键字、作者、许可证等信息。必填项会提供默认值,我们可以修改这些默认值。
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: (deniro-nodejs)
version: (1.0.0)
description: study node.js
entry point: (create_app.js) index.js
test command: make test
git repository:
keywords:
author: Deniro Lee
license: (ISC)
About to write to F:\WebstormProjects\deniro-nodejs\package.json:{"name": "deniro-nodejs","version": "1.0.0","dependencies": {"request": "latest"},"description": "study node.js","main": "index.js","devDependencies": {},"scripts": {"test": "make test"},"author": "Deniro Lee","license": "ISC"
}Is this OK? (yes) yes
执行后,就会在 package.json 中,加入以上这些配置项:
{"name": "deniro-nodejs","version": "1.0.0","dependencies": {"request": "latest"},"description": "study node.js","main": "index.js","devDependencies": {},"scripts": {"test": "make test"},"author": "Deniro Lee","license": "ISC"
}
7 版本号
使用 NPM 下载和发布代码时都会接触到版本号。 NPM 使用语义版本号来管理代码。语义版本号分为 X.Y.Z 三位,分别代表主版本号 、 次版本号和补丁版本号。当代码变更时,版本号按以下原则更新。
- 如果只是修复 bug ,需要更新 Z 位;
- 如果是新增了功能,但是向下兼容,需要更新 Y 位;
- 如果有大变动,向下不兼容,需要更新 X 位。
说说包管理工具 NPM 的基本用法相关推荐
- node包管理工具npm的更多用法
node的包管理工具npm node现在的火热程度一点都没有下降,而成为了前端必备工具,特别是npm的包库已经成为了前端必备的,即使你不接触node作为后端的存在,现在各种第三方包依然需要使用npm来 ...
- 模块和包管理工具npm
模块和包管理工具npm 什么是模块化 模块化的优点 模块化规范 nodejs中模块的分类 加载模块用require方法 模块作用域 向外共享模块作用域中的成员 require()模块时,得到的永远是m ...
- Nodejs入门 (四) 包管理工具(npm、cnpm、yarn、nvm)
目录 一.什么是包管理工具? 二.npm的使用 1.初始化 2.搜索包 3.下载安装包 4.安装包的使用 5.全局安装 6.安装包依赖 7.安装指定版本的包 8.删除依赖 9.配置命令别名 10.np ...
- 前端工程化-包管理工具npm-yarn-cnpm-pnpm详细介绍以及如何选择
文章目录 包管理工具详解 npm包管理工具 1.代码共享的方案 2.npm包管理工具介绍 3.npm的配置文件 常见配置文件 常见配置文件属性 版本号的说明 4.npm install npm ins ...
- Vue3学习笔记- NPM包管理工具
导语 这篇分享是关于Vue3 系列的学习知识的整理的开始,能够对大家学习带来帮助.也是对自己能力的一种提升. 学习大纲 认识NPM 安装NPM 使用NPM NPM中级用法 了解package.js ...
- npm nodejs包管理工具 简介
NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器下载并 ...
- node的包管理工具:yarn和npm
yarn是Facebook发布的一款依赖管理工具,它比npm更快.更高效. NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题. 一.yarn官方网站: 英文官网:h ...
- NPM:nodejs官方包管理工具的简介、安装、使用方法之详细攻略
NPM:nodejs官方包管理工具的简介.安装.使用方法之详细攻略 目录 NPM之nodejs官方包管理工具的简介 NPM之nodejs官方包管理工具的安装 NPM之nodejs官方包管理工具的使用方 ...
- (18) Node.js npm包管理工具
一.npm概述 npm (Node Package Manager)是 Node.js 的包管理工具. 什么是包?包就是一坨代码,就是 Node.js 的第三方模块. 例如:JQuery模块,Boot ...
最新文章
- Linux Kernel5.10的核间通信(SGI中断)的本质
- RabbitMQ添加用户
- tomcat7+nginx+memcached 配置tomcat 集群以及负载均衡
- 当不能用for等循环时,可以考虑递归
- Springboot/Cloud集成Sentinel 和 入门实战
- 超级计算机子系统,大规模并行巨型机的并行I/O子系统
- WinRAR去除打开后弹出广告的方法
- 【Java】生成随机数的两种方法
- 【小白写代码之九九乘法表,用C/C++来实现】
- pentaho开源商业智能平台的搭建
- C语言实现小猫钓鱼游戏项目,堪称最强垂钓系统!
- 数据结构 三:树(Tree)
- P1149 火柴棒等式
- 向外国大师学习敏捷式开发?嫦娥掩面而笑
- 使用nrm切换本地npm源
- 台式电脑怎么卸载计算机,有些电脑软件卸载不了怎么办?电脑安装的软件无法卸载解决方法...
- 一淘报告引发2012电商首战 当当再战京东
- 车载毫米波雷达天线罩设计问题
- 4W字的Redis面试教程 再不会我可就锤你了
- Ubuntu16.04 在nginx中增加网站的http与https服务
热门文章
- Git、GitHub、Gitee、GitLab
- 我的世界服务器国际版显示坐标,我的世界国际版怎么一直显示坐标 | 手游网游页游攻略大全...
- 【转】关于linux中wps出现系统字体缺失的解决方法
- tpl怎么口_解决tplogin.cn打开是电信登录页面的办法是什么?
- web前端高级工程师,面试题
- DAY23:命令执行代码执行漏洞
- 转移C盘中Pycharm缓存文件
- Python爬虫:彼岸图网图片爬取
- ubuntu服务器卸载mysql_在Ubuntu或Debian系统的服务器上卸载MySQL的方法
- VA01销售订单批导问题解决