解决异常:npm ERR! network ‘proxy‘ 配置问题,顺带列举国内常用的镜像地址
AI 摘要salmon-ai
本文详细解析了 npm 安装依赖时因网络异常、proxy 配置错误
问题
昨日拉了公司的商城小程序项目下来,安装依赖时报网络异常问题
bash
$ npm install
npm WARN deprecated babel-preset-es2015@6.24.1: 🙌 Thanks for using Babel: we recommend using babel-preset-env now: please read https://babeljs.io/env to update!
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm ERR! code ENOTFOUND
npm ERR! syscall getaddrinfo
npm ERR! errno ENOTFOUND
npm ERR! network request to https://registry.nlark.com/xmlbuilder/download/xmlbuilder-11.0.1.tgz failed, reason: getaddrinfo ENOTFOUND registry.nlark.com
npm ERR! network This is a problem related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly. See: 'npm help config'
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/chenxiangwen/.npm/_logs/2024-11-15T03_07_32_536Z-debug-0.log
(base)
解决
首先,我们需要查看当前的 npm 配置。可以使用命令 npm config get <config> 来查看特定配置项的值。例如,检查 http-proxy 和 https-proxy 的设置:
bash
npm config get http-proxy
npm config get https-proxy
如果输出为空或者返回了错误信息,说明这些配置尚未设置或设置有误。如果需要设置代理服务,则可以输入以下命令解决:
bash
## 设置 HTTP 代理
npm config set http-proxy "http://user:password@host:port"
## 设置 HTTPS 代理
npm config set https-proxy "https://user:password@host:port"
## 例如
npm config set proxy http://127.0.0.1:7890
npm config set https-proxy http://127.0.0.1:7890
## 取消代理
npm config delete proxy
npm config delete https-proxy
npm config set proxy false
如果是镜像导致的问题,可以尝试设置镜像解决
bash
## 设置镜像
npm config set registry https://registry.npmmirror.com
## 检查是否安装成功
npm config get registry
## 安装
npm install
## 清除缓存
npm cache clean
npm cache verify
## 强制清理缓存
npm cache clean --force
## 查看当前设置
npm config list
## 某个项目 install 时使用指定镜像
npm install --registry=http://registry.npmmirror.com
## 使用镜像安装某个依赖,比如安装express
npm install express --registry=http://registry.npmmirror.com
我的问题是最后使用了 cnpm 来安装依赖解决的
bash
## 使用阿里定制的 cnpm 命令行工具代替默认的 npm,输入以下代码
npm install -g cnpm --registry=http://registry.npmmirror.com
## 检查是否安装成功
cnpm -v
之后使用 cnpm 安装依赖
bash
cnpm install
国内常见的镜像地址
bash
## 官方镜像
npm config set https://registry.npmjs.org/
## 淘宝镜像
npm config set https://registry.npmmirror.com/
## 腾讯云镜像
npm config set https://mirrors.cloud.tencent.com/npm
## 中国镜像源
npm config set https://r.cnpmjs.org
评论
0 条—
还没有评论,来说第一句