npm设置镜像的方法(包含切换国外国内镜像源)

一.通过命令配置淘宝镜像

  1. 切换为淘宝镜像命令(安装一些package容易报错)
    npm config set registry https://registry.npm.taobao.org
  2. 查看当前使用的镜像地址命令
    npm config get registry
如果返回 https://registry.npm.taobao.org,说明镜像配置成功。

二、切换回原镜像(安装一些package不容易报错)

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

三、其他镜像地址查询

  1. 安装nrm
    npm install nrm -g
  2. 使用nrm查询其他镜像地址(出现报错,可能是因为安装了最新版本的nrm导致冲突)
    nrm ls
报错结果如下
node:internal/validators:119
    throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
    ^

[TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
  at new NodeError (node:internal/errors:363:5)
  at validateString (node:internal/validators:119:11)
  at Object.join (node:path:429:7)
  at Object.<anonymous> (C:\Users\Admin\AppData\Roaming\npm\node_modules\nrm\cli.js:17:20)
  at Module._compile (node:internal/modules/cjs/loader:1109:14)
  at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
  at Module.load (node:internal/modules/cjs/loader:989:32)
  at Function.Module._load (node:internal/modules/cjs/loader:829:14)
  at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
  at node:internal/main/run_main_module:17:47
] {
  code: 'ERR_INVALID_ARG_TYPE'
}
报错解决办法
  1. 使用步骤二所示方法将镜像切换为原始镜像,避免以为切换为其他镜像引起的报错;
  2. 查看nrm可用的版本
    npm view nrm versions
  3. 卸载原有的nrm:
    npm uninstall -g nrm
  4. 安装指定版本的nrm(这里使用1.1.0不会报错,使用的node版本为v16.3.0)
    npm install -g nrm@1.1.0
  5. 查看安装的nrm版本:
    nrm -V或者nrm --version
成功结果显示如下
* npm ---- https://registry.npmjs.org/
  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/

四、通过配置淘宝镜像并使用cnpm安装

  1. 安装cnpm
    npm install -g cnpm --registry=https://registry.npm.taobao.org
  2. 使用cnpm
    cnpm install xxx

其中在node v16.3.0环境下使用淘宝镜像安装一些package时出现报错,后续找到方法后更新,建议先忍一忍,使用原始镜像。

  • 41
    点赞
  • 161
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值