推荐使用 nrm 来管理镜像源。 nrm (npm registry manager ) 是 npm 的镜像源管理工具,可以很方便的在不同的下载源之间切换。
1、全局安装
npm install -g nrm
2、查看可选下载源
nrm ls
默认选项,带 * 号表示当前使用的下载源
第一次执行没有默认值
设置默认源
nrm use taobao
SUCCESS The registry has been updated to 'taobao'.
3、切换下载源
// 切换淘宝镜像
nrm use taobao
再次执行
nrm ls
npm -------- https://registry.npmjs.org/
yarn ------- https://registry.yarnpkg.com/
cnpm ------- http://r.cnpmjs.org/
* taobao ----- https://registry.npm.taobao.org/
nj --------- https://registry.nodejitsu.com/
npmMirror -- https://skimdb.npmjs.com/registry/
edunpm ----- http://registry.enpmjs.org/
4、添加下载源
nrm add <registry-name> <registry-url>
// 示例
nrm add myRegistry http://xxxx.com
5、删除下载源
nrm del <registry-name>
// 示例
nrm del myRegistry
6、移除缓存
npm cache clean --force
7、查看版本号
查看服务器最新版本号
npm view \<package> version
查看服务器所有版本号
npm view \<package> versions
查看本地安装包版本号(需在项目目录下执行)
npm ls \<package>
查看全局安装包版本号
npm ls \<package> -g
查看npm配置信息
npm config list
; "user" config from C:\Users\hukunzhen.npmrc
cache = "D:\nodejsmodules\npm_cache"
home = "https://npmmirror.com"
prefix = "D:\nodejsmodules\npm_global"
registry = "https://registry.npmmirror.com/"
sass_binary_site = "https://cdn.npmmirror.com/mirrors/node-sass"
; node bin location = D:\XXX\node-v18.17.1\node.exe
; node version = v18.17.1
; npm local prefix = D:\XXX\vue3\vue3-vite-ts
; npm version = 10.2.0
; cwd = D:\XXX\vue3\vue3-vite-ts
; HOME = C:\Users\XXX
; Run npm config ls -l to show all defaults.
8、npm 依赖包版本号说明
~ 会匹配最近的小版本依赖包。比如~1.2.3会匹配所有1.2.x版本,但是不包括1.3.0。
^ 会匹配最新的大版本依赖包。比如^1.2.3会匹配所有1.x.x的包,包括1.3.0,但是不包括2.0.0。
* 会匹配最新发布的版本。
使用 ^ 时,有的小版本更新后会引入新的问题导致项目不稳定。建议使用 ~ 来标记版本号,这样可以保证项目不会出现大的问题,也能保证包中的小bug可以得到修复