解决资源下载慢的问题
注意:npm 镜像的配置可以使用 nrm 来做,类似于 nvm 对于 node的管理,nrm 管理的是 npm 的镜像源!
nrm
(npm registry manager)是一个帮助管理和快速切换不同 npm 源(registry)的工具。使用 nrm
可以方便地在如 npm 官方源、cnpm 源、taobao 源等之间进行切换。以下是 nrm
的一些基本使用方法:
安装 nrm: 通过 npm 全局安装
nrm
:bashnpm install -g nrm
列出所有可用的 npm 源: 使用
nrm ls
命令查看所有可用的 npm 源及当前正在使用的源:bashnrm ls
切换 npm 源: 使用
nrm use <registry>
命令切换到指定的 npm 源。例如,切换到 taobao 源:bashnrm use taobao
增加自定义的 npm 源: 如果需要增加一个自定义的 npm 源,可以使用
nrm add <registry> <url>
命令。例如:bashnrm add myregistry http://registry.mydomain.com
删除自定义的 npm 源: 使用
nrm del <registry>
命令删除一个自定义的 npm 源。例如:bashnrm del myregistry
测试 npm 源的响应时间: 使用
nrm test
或者nrm test <registry>
命令测试所有或指定 npm 源的网络响应时间:bashnrm test # 或者测试指定源 nrm test npm
获取当前使用的 npm 源地址: 使用
nrm current
命令可以显示当前使用的 npm 源地址:bashnrm current
使用 nrm
可以很方便地在不同的 npm 源之间切换,特别是在某些源访问速度较慢或不稳定时。在国内,很多开发者倾向于使用 taobao 源,因为它提供了更快的访问速度。请注意,如果你在公司或团队环境中工作,确保切换的源是被团队所接受的,以避免潜在的问题。
一、通过命令配置npm镜像
1、设置淘宝镜像源
npm config set registry https://registry.npm.taobao.org/
2、设置官方镜像源
npm config set registry https://registry.npmjs.org
3、查看镜像使用状态:
npm config get registry
如果返回https://registry.npm.taobao.org/,说明配置的是淘宝镜像。
如果返回https://registry.npmjs.org/,说明配置的是淘宝镜像。
注意:原淘宝 npm 域名即将停止解析!!使用老域名的请尽快更新到新域名!
《淘宝 NPM 镜像站喊你切换新域名啦》https://npm.taobao.org 和 https://registry.npm.taobao.org 将在 2022.06.30 号正式下线和停止 DNS 解析。
域名切换规则:
https://npm.taobao.org => https://npmmirror.comhttps://registry.npm.taobao.org => https://registry.npmmirror.com
所以现在应该是npm config set registry https://registry.npmmirror.com
如何恢复npm的官方源?
注意 npm 更换国内镜像源之后,将无法再使用 npm search
命令,需要恢复为官方源才可以使用,如果恢复官方源后还不可使用,运行删除注册表命令后重试即可。
// 恢复官方源
npm config set registry https://registry.npmjs.org
// 删除注册表
npm config delete registry
二、通过使用cnpm安装
1、安装cnpm
npm install -g cnpm --registry=https://registry.npm.taobao.org
解决安装卡顿或无法安装:——> 这个是重点,可能上面第一种配置完之后也需要这样才可以正常下载生效!
注册模块镜像
npm set registry https://registry.npm.taobao.org
// node-gyp 编译依赖的 node 源码镜像
npm set disturl https://npm.taobao.org/dist
// 清空缓存
npm cache clean --force
// 安装cnpm
npm install -g cnpm --registry=https://registry.npm.taobao.org
2、使用cnpm
cnpm install xxx
三、配置yarn镜像
npm install -g yarn
临时使用
如果只是一次性使用,可以使用下面的命令
npm --registry https://registry.npm.taobao.org install XXX(模块名)
全局配置
这种设置是全局的,以后每次都会自动读取已经设置好的源
// 查询源
yarn config get registry
// 更换国内源
yarn config set registry https://registry.npmmirror.com
// 恢复官方源
yarn config set registry https://registry.yarnpkg.com
// 删除注册表
yarn config delete registry