NPM(全称 Node Package Manager,即“node包管理器”)是Node.js默认的、以JavaScript编写的 软件包管理系统。
以上是维基百科给NPM的定义。理解软件包管理系统。
软件包管理系统是在计算机中自动安装、配制、卸载和升级软件包的工具组合,在各类系统软件和应用软件的安装管理中均有普遍应用。
好比:PHP中composer、JAVA中maven、LIUNX系统中的yumhtml
NPM软件包管理系统由3部分组成:前端
能够经过https://www.npmjs.com网站,搜索包、管理本身的包和我的信息。vue
一个巨大的数据库,保存着每一个包(package)的信息。node
经过命令行和终端运行,开发者经过CLI与NPM打交道。
在安装Node.js时会默认安装NPM包管理器,NPM彻底由Javascript写成,和模块没啥区别,因此咱们能够npm install -g npm
更新安装本身。react
随着前端工程化、模块化的快速发展,前端开发已经离不开npm包管理器了。在咱们使用前端各大框架Vue、React、Angluar时,咱们只须要npm install [package name]
就能够安装使用咱们须要的模块包,很是便捷高效。git
经过下图咱们就能看出npm之火爆。总包量100多万,周下载量114亿。来自各大洲的开源软件开发者都在使用npm借鉴学习或者分享。同时也是Node.js得到成功重要缘由之一。
github
以上图片罗列出NPM全部的命令,捡几个经常使用的命令学习;
web
安装形式:redis
npm install (with no args, in package dir) npm install [<@scope>/]<pkg> npm install [<@scope>/]<pkg>@<tag> npm install [<@scope>/]<pkg>@<version> npm install [<@scope>/]<pkg>@<version range> npm install <folder> npm install <tarball file> npm install <tarball url> npm install <git:// url> npm install <github username>/<github project> alias: i,add common options: [-S|--save|-D|--save-dev|-O|--save-optional] [-E|--save-exact][--no-save][-B|--save-bundle][--dry-run]
其中:
-S与--save 安装到dependenices生产环境中,对应package.json
中的dependencies
数据库
//npm i -S ecpress || npm install --save express "dependencies": { "express": "^4.17.1", }
-D与--save-dev 安装到devDependenices开发环境中,对应package.json
中的devDependencies
//npm i -D ecpress || npm install --save-dev express "devDependencies": { "express": "^4.17.1" }
-O与--save-optional 安装加入到可选阶段依赖对应package.json
中的optionalDependencies
// npm i -O vue || npm install --save-optional "optionalDependenices":{ "vue": "^2.5.2" }
-E与--save-exact 精准安装指定模块版本对应package.json
中的dependenicies
不会是兼容的模式展现^
// npm i -E vue || npm install --save-exact 注意没有^ "dependenices":{ "vue": "2.5.2" }
--no-save 防止保存到依赖项
-B与--save-bundle 保存的依赖项也将添加到您的bundleDependencies
列表中。
"dependenices":{ "vue":"^2.5.2" }, "bundleDependencies":["vue"]
--dry-run 只在命令行能够看到有安装的操做,实际没有安装任何内容到node_modules
和package.json
中
本地安装
npm i vue
全局安装
npm i -g vue || npm install --global vue
即便本地已经下载,强制从远程下载安装
npm i -f vue || npm install --force vue
npm uninstall [<@scope>/]<pkg>[@<version>]... [-S|--save-prod][-D|--save-dev][-O|--save-optional] [--no-save] aliases: un, unlink, remove, rm, r
npm uninstall -g vue //卸载全局包 npm uninstall vue // 卸载本地包
npm search [-l|--long] [--json] [--parseable] [--no-description] [search terms ...] aliases: s, se, find
npm update [-g] [<pkg>...] aliases: up, upgrade, udpate
npm outdated [[<@scope>/]<pkg> ...]
npm ls [[<@scope>/]<pkg> ...] aliases: list, la, ll
npm init [--force|-f|--yes|-y|--scope] npm init <@scope> (same as `npx <@scope>/create`) npm init [<@scope>/]<name> (same as `npx [<@scope>/]create-<name>`)
默认init 跳过提示步骤
npm init -y || npm init -yes
安装react应用到my-react-app目录下
npm init react-app ./my-react-app
init 转成npx对应操做
npm init foo -> npx create-foo npm init @usr/foo -> npx @usr/create-foo npm init @usr -> npx @usr/create
npm help install npm help init npm install -h || npm install --help
npm root # 查看全局安装路径 npm root -g
npm config set <key> <value> [-g|--global] npm config get <key> npm config delete <key> npm config list [-l] [--json] npm config edit npm get <key> npm set <key> <value> [-g|--global] aliases: c
基本配置
npm config list ; cli configs metrics-registry = "https://registry.npmjs.org/" scope = "" user-agent = "npm/6.4.1 node/v10.15.3 darwin x64" ; userconfig /Users/ducen/.npmrc //registry.npm.taobao.org/:always-auth = false init.author.email = "123@.qq.com" registry = "https://registry.npmjs.org/" ; node bin location = /usr/local/bin/node ; cwd = /Users/ducen/Learn/npm ; HOME = /Users/ducen ; "npm config ls -l" to show all defaults.
npm cache add <tarball file> npm cache add <folder> npm cache add <tarball url> npm cache add <name>@<version> npm cache clean [<path>] aliases: npm cache clear, npm cache rm npm cache verify
默认缓存位置mac~/.npm
window %AppData%/npm-cache
最经常使用的是清除本地缓存
npm cache clean
npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git]
npm version { npm: '6.4.1', ares: '1.15.0', cldr: '33.1', http_parser: '2.8.0', icu: '62.1', modules: '64', napi: '3', nghttp2: '1.34.0', node: '10.15.3', openssl: '1.1.0j', tz: '2018e', unicode: '11.0', uv: '1.23.2', v8: '6.8.275.32-node.51', zlib: '1.2.11' }
npm view [<@scope>/]<name>[@<version>] [<field>[.<subfield>]...] aliases: info, show, v
npm view vue vue@2.6.10 | MIT | deps: none | versions: 250 Reactive, component-oriented view layer for modern web interfaces. https://github.com/vuejs/vue#readme keywords: vue dist .tarball: https://registry.npmjs.org/vue/-/vue-2.6.10.tgz .shasum: a72b1a42a4d82a721ea438d1b6bf55e66195c637 .integrity: sha512-ImThpeNU9HbdZL3utgMCq0oiMzAkt1mcgy3/E6zWC/G6AaQoeuFdsl9nDhTDU3X1R6FK7nsIUuRACVcjI+A2GQ== .unpackedSize: 3.0 MB maintainers: - yyx990803 <yyx990803@gmail.com> dist-tags: beta: 2.6.0-beta.3 csp: 1.0.28-csp latest: 2.6.10 published 3 months ago by yyx990803 <yyx990803@gmail.com>
npm view <pkg> dependencies查看模块的依赖关系
npm view express dependecies { 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' }
npm view <pkg> repository.url 查看源文件地址
npm view qs repository.url git+https://github.com/ljharb/qs.git
npm view <pkg> contributors 查看模块的贡献值
npm view express contributors [ 'Aaron Heckmann <aaron.heckmann+github@gmail.com>', 'Ciaran Jessup <ciaranj@gmail.com>', 'Douglas Christopher Wilson <doug@somethingdoug.com>', 'Guillermo Rauch <rauchg@gmail.com>', 'Jonathan Ong <me@jongleberry.com>', 'Roman Shtylman <shtylman+expressjs@gmail.com>', 'Young Jae Sim <hanul@hanul.me>' ]
npm adduser [--registry=url] [--scope=@orgname] [--always-auth] [--auth-type=legacy] aliases: login, add-user
npm adduser Username: alan-du Password: Email: (this IS public) 316358726@qq.com Logged in as alan-du on https://registry.npmjs.org/.
npm publish [<tarball>|<folder>] [--tag <tag>] [--access <public|restricted>] [--otp otpcode] [--dry-run] Publishes '.' if no argument supplied Sets tag 'latest' if no --tag specified
简单发一个包
#1 mkdir alan-npm-test #2 cd alan-npm-test #3 npm init -y #4 touch index.js #5 echo "function(){console.log('这是发布的测试包')}()">index.js #6 npm adduser Username: alan-du Password: Email: (this IS public) 316358726@qq.com Logged in as alan-du on https://registry.npmjs.org/. #7 npm publish npm notice npm notice 📦 alan-npm-test@1.0.0 npm notice === Tarball Contents === npm notice 227B package.json npm notice 58B index.js npm notice === Tarball Details === npm notice name: alan-npm-test npm notice version: 1.0.0 npm notice package size: 353 B npm notice unpacked size: 285 B npm notice shasum: 4c84a14ed93f4e1e6d2a603da3e70868f71035e4 npm notice integrity: sha512-IgzS/4dB/vFVD[...]lzv4Fq4Pw2rnA== npm notice total files: 2 npm notice + alan-npm-test@1.0.0
npm dist-tag add <pkg>@<version> [<tag>] npm dist-tag rm <pkg> <tag> npm dist-tag ls [<pkg>] aliases: dist-tags
#1 npm dist-tag ls alan-npm-test@1.0.2-0: 1.0.2-0 latest: 1.0.3-1 npm-test@1.0.5: 1.0.5 #2 npm dist-tag rm alan-npm-test alan-npm-test@1.0.2-0 -alan-npm-test@1.0.2-0: alan-npm-test@1.0.2-0
npm deprecate <pkg>[@<version>] <message>
npm deprecate alan-npm-test@1.0.3-1 "this package has been deprecated"
<message>
不能为汉字等特殊字符
删除某个版本
npm unpublish alan-npm-test@1.0.5
删除整个npm市场的包
npm unpublish alan-npm-test --force
24小时内你能够删除本身发布的库,而且没有被下载,以后你就不再能删除了。
package.json 介绍
{ "name": "express", "description": "Fast, unopinionated, minimalist web framework", "version": "4.17.1", "author": "TJ Holowaychuk <tj@vision-media.ca>", "contributors": [ "Aaron Heckmann <aaron.heckmann+github@gmail.com>", "Ciaran Jessup <ciaranj@gmail.com>", "Douglas Christopher Wilson <doug@somethingdoug.com>", "Guillermo Rauch <rauchg@gmail.com>", "Jonathan Ong <me@jongleberry.com>", "Roman Shtylman <shtylman+expressjs@gmail.com>", "Young Jae Sim <hanul@hanul.me>" ], "license": "MIT", "repository": "expressjs/express", "homepage": "http://expressjs.com/", "keywords": [ "express", "framework", "sinatra", "web", "rest", "restful", "router", "app", "api" ], "dependencies": { "accepts": "~1.3.7", "array-flatten": "1.1.1", ... }, "devDependencies": { "after": "0.8.2", "connect-redis": "3.4.1", ... }, "engines": { "node": ">= 0.10.0" }, "files": [ "LICENSE", "History.md", "Readme.md", "index.js", "lib/" ], "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/" } }
在package.json中最重要的就是name和version字段。他们都是必须的,若是没有就没法install。name和version一块儿组成的标识在假设中是惟一的。改变包应该同时改变version。
version必须能被node-semver解析,它被包在npm的依赖中。(要本身用能够执行npm install semver
)
可用的“数字”或者“范围”见下文语义化版本控制范围SemVer.
项目提交问题的url和(或)邮件地址
"bugs": { "url": "https://github.com/vuejs/vue/issues" }
指定一个许可证,让人知道使用的权利和限制的。
最简单的方法是,假如你用一个像BSD或者MIT这样通用的许可证,就只须要指定一个许可证的名字,像这样:
"license": "MIT",
若是你又更复杂的许可条件,或者想要提供给更多地细节,能够这样:
"licenses" : [ { "type" : "MyLicense", "url" : "http://github.com/owner/project/path/to/license" } ]
指定你的代码存放的地方。这个对但愿贡献的人有帮助。若是git仓库在github上,那么npm docs
命令能找到你。
这样作:
"repository": { "type": "git", "url": "git+https://github.com/vuejs/vue.git" },
URL应该是公开的(即使是只读的)能直接被未通过修改的版本控制程序处理的url。不该该是一个html的项目页面。由于它是给计算机看的。
“scripts”是一个由脚本命令组成的hash对象,他们在包不一样的生命周期中被执行。key是生命周期事件,value是要运行的命令。
"scripts": { "dev": "rollup -w -c scripts/config.js --environment TARGET:web-full-dev", ... },
更多详细请看 npm-scripts(7)
"config": { "commitizen": { "path": "./node_modules/cz-conventional-changelog" } }
config对象可用于设置在升级过程当中持久存在的程序包脚本中使用的配置参数。例如,若是包具备如下内容:
生产依赖包,请不要将测试或过渡性的依赖放在dependencies
中。
"dependencies": { "accepts": "~1.3.7", "array-flatten": "1.1.1", ... },
开发依赖包,若是有人要使用你的模块,那么他们可能不须要你开发使用的外部测试或者文档框架。
在这种状况下,最好将这些附属的项目列在devDependencies
中。
"devDependencies": { "after": "0.8.2", "connect-redis": "3.4.1", ... },
版本格式:主版本号.次版本号.修订号,版本号递增规则以下:
先行版本号及版本编译元数据能够加到“主版本号.次版本号.修订号”的后面,做为延伸。
当主版本号升级后,次版本号和修订号须要重置为0,次版本号进行升级后,修订版本须要重置为0。
先行版本号能够做为发布正式版以前的版本,格式是在修订版本号后面加上一个链接号(-),再加上一连串以点(.)分割的标识符,标识符能够由英文、数字和链接号([0-9A-Za-z-])组成。
1.0.0-alpha 1.0.0-alpha.1 1.0.0-0.3.7 1.0.0-x.7.z.92
常见的先行版本号名称:
npm依赖的规则,有^
、~
、>
、<
、=
、>=
、<=
、-
、||
、x
、X
、*
等符号;
**^**
:表示同一主版本号,不小于当前指定版本号的版本好号。
dependencies:{ vue:"^2.6.2" } // npm install 安装最新依赖,会下载2.6.三、2.7.1 不会下载2.6.一、3.0.0
~
:表示同一主版本号和次版本号中,不小于指定版本号的版本号。
dependencies:{ vue:"~2.6.2" } // npm install 安装最新依赖,会下载2.6.三、2.6.6 不会下载2.6.1 2.7.0
<、=、>=、<=、-
:用来指定一个版本号范围。
dependencies:{ vue:">2.6.2", }, "engines": { node: ">= 0.10.0" }, //`1.1.1 - 1.2.0` //注意使用 `-` 的时候,必须两边都有空格。
||
:表示或
dependencies:{ vue:"<2.6.2 || >2.5.1", },
x
、X
、*
:表示通配符
dependencies:{ qs:"*", vue:"2.x" }, //`*` 对应全部版本号 //`2.x` 对应全部主版本号为 2 的版本号
npm经常使用命令详解
一分钟教你发布一个npm包
NPM模块的TAG管理
package.json英文文档
package.json中文文档
Semver 中文网
Semver 语义化版本规范