将 npm/yarn 的源地址替换成国内镜像的源地址

18 篇文章 0 订阅

我的 .npmrc

registry=https://registry.npmmirror.com/

canvas_binary_host_mirror=https://registry.npmmirror.com/-/binary/canvas/
sass_binary_site=https://registry.npmmirror.com/-/binary/node-sass
nodejs_org_mirror=https://registry.npmmirror.com/-/binary/node/
electron_mirror=https://registry.npmmirror.com/-/binary/electron/
electron_builder_binaries_mirror=https://registry.npmmirror.com/-/binary/electron-builder-binaries/
chromedriver_cdnurl=https://registry.npmmirror.com/-/binary/chromedriver/

npm

查看当前地址

npm config get registry

修改源地址为淘宝 NPM 镜像

npm config set registry https://registry.npmmirror.com/

修改源地址为官方源 

npm config set registry https://registry.npmjs.org/

http://npm.taobao.org 和 http://registry.npm.taobao.org 将在 2022.06.30 号正式下线和停止 DNS 解析。

npm config set registry https://registry.npm.taobao.org/ 

yarn

查看当前地址

yarn config get registry

修改源地址为淘宝 NPM 镜像​​​​​​

yarn config set registry https://registry.npmmirror.com/

修改源地址为官方源 

yarn config set registry https://registry.yarnpkg.com

其他(华为开源镜像

NPM的配置文件为用户根目录下的:~/.npmrc(Windows路径为:C:\Users\<UserName>\.npmrc),您可以点击此处直接下载配置文件,或者运行如下命令设置:

npm config set registry https://repo.huaweicloud.com/repository/npm/
npm cache clean -f

华为开源镜像站同时也提供了工具类加速镜像,可以通过如下的命令设置加速地址:

※ 设置nodejs工具的镜像地址

npm config set disturl https://repo.huaweicloud.com/nodejs

※ 设置Node-Sass的镜像地址

npm config set sass_binary_site https://repo.huaweicloud.com/node-sass

※ 设置浏览器引擎驱动镜像地址,如果需要安装Selenium,请点击此处

npm config set phantomjs_cdnurl https://repo.huaweicloud.com/phantomjs
npm config set chromedriver_cdnurl https://repo.huaweicloud.com/chromedriver
npm config set operadriver_cdnurl https://repo.huaweicloud.com/operadriver

※ 设置ElectronPython的镜像地址

npm config set electron_mirror https://repo.huaweicloud.com/electron/
npm config set python_mirror https://repo.huaweicloud.com/python

参考:华为开源镜像(语言类-NPM

从源头构建安装

npm install --build-from-source

npm安装淘宝镜像源是为了加速在中国访问npm仓库的速度并避免潜在的网络问题。步骤如下: ### 步骤一:更新 npm 设置 首先需要通过 `npm` 更新本地的配置文件 `.npmrc`,添加淘宝(taobao)镜像作为全局默认的镜像源。 ```bash npm config set registry https://registry.npm.taobao.org ``` 这条命令会将默认的 npm 注册表设置为淘宝的镜像源地址。 ### 步骤二:验证设置是否成功 为了确认上述操作已经生效,可以运行下面的命令查看当前的注册表配置: ```bash npm config get registry ``` 输出应该显示的是 `https://registry.npm.taobao.org`。 ### 相关问题: 1. **如何撤销设置**? - 若要恢复原始的配置,可以执行以下命令清除自定义的 `.npmrc` 配置项: ```bash npm config delete registry ``` - 然后再通过原始的命令来更新配置以使用官方的镜像源: ```bash npm config set registry https://registry.npmjs.org ``` 2. **为什么有时候还需要额外指定镜像源**? - 虽然全局配置改变了默认的镜像源,但在特定场景下(比如只针对某个项目),可能仍然需要手动指定到淘宝镜像或其他指定的镜像源。例如,在创建新项目的初始化过程中,可以通过 `yarn` 或 `pnpm` 来指定镜像源: ```bash yarn add package-name --registry=https://registry.npm.taobao.org ``` 3. **淘宝镜像源的优势是什么**? - 使用淘宝镜像是由于它为中国用户提供了高速下载服务,减少网络延迟,提高下载效率,并且通常提供较稳定的版本和更好的用户体验。此外,对于国内用户来说,使用该镜像还可以解决访问速度慢、部分依赖包不可用等问题。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱宇阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值