D:\JAVAPROJECT\shixun\vue3.0-template-admin-main>yarn yarn install v1.22.22 $ npx only-allow yarn [1/4] Resolving packages... [2/4] Fetching packages... info There appears to be trouble with your network connection. Retrying... info There appears to be trouble with your network connection. Retrying... info There appears to be trouble with your network connection. Retrying... info There appears to be trouble with your network connection. Retrying... error Error: getaddrinfo ENOTFOUND registry.nlark.com at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:122:26) info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
时间: 2025-06-20 18:46:14 浏览: 20
### 解决 Yarn 安装时网络连接问题以及特定错误代码的解决方案
#### 1. 确保正确的镜像源配置
当遇到 `error ECONNREFUSED` 或 `ENOTFOUND` 错误时,可能是因为当前使用的镜像源无法访问。可以尝试将镜像源切换为国内的淘宝源或其他可用源。使用以下命令设置淘宝源:
```bash
yarn config set registry https://registry.npmmirror.com
```
如果需要切换回官方源,可以运行以下命令:
```bash
yarn config set registry https://registry.yarnpkg.com
```
此外,对于某些特定依赖(如 `node-sass`),可以通过设置其二进制文件的下载地址来避免网络问题:
```bash
yarn config set sass-binary-site http://npm.taobao.org/mirrors/node-sass
```
此方法适用于解决与 `node-sass` 相关的网络超时问题[^4]。
#### 2. 检查代理设置
如果本地启用了 HTTP 或 HTTPS 代理,可能会导致 Yarn 的请求被拒绝。可以通过以下命令检查当前的代理配置:
```bash
yarn config get proxy
yarn config get https-proxy
```
如果输出了代理地址,则需要禁用代理以避免冲突:
```bash
yarn config delete proxy
yarn config delete https-proxy
```
此操作可以移除代理配置,从而避免因代理导致的连接失败[^1]。
#### 3. 增加超时时间
Yarn 默认的网络超时时间可能不足以完成某些依赖的下载。可以通过增加超时时间来解决此问题:
```bash
yarn install --network-timeout 600000
```
如果该方法有效,可以将超时时间配置到 `.yarnrc` 文件中,以便在后续安装中自动应用:
```yaml
network-timeout 600000
```
此方法特别适用于网络条件较差的情况[^5]。
#### 4. 手动下载并安装依赖
如果某些依赖项始终无法通过 Yarn 自动下载,可以手动下载并放置到正确的位置。例如,对于 Electron 依赖,可以在淘宝镜像上下载对应版本的文件,并解压到 `node_modules/electron/dist` 目录下。
#### 5. 检查 DNS 和防火墙设置
DNS 解析失败或防火墙阻止可能导致 `ENOTFOUND` 错误。可以尝试修改系统的 DNS 设置为公共 DNS 服务器(如 Google DNS 或阿里云 DNS)。此外,清除 DNS 缓存也可能有所帮助:
```bash
sudo systemd-resolve --flush-caches
```
或者在 Windows 系统中运行:
```cmd
ipconfig /flushdns
```
#### 6. 使用离线模式
如果网络环境极其不稳定,可以考虑使用 Yarn 的离线模式。确保所有依赖项已预先下载后,运行以下命令:
```bash
yarn install --offline
```
此方法仅适用于依赖项已在缓存中存在的情况[^3]。
---
```python
# 示例代码:检查当前 Yarn 配置
import subprocess
def check_yarn_config():
try:
result = subprocess.run(['yarn', 'config', 'list'], capture_output=True, text=True)
print(f"Current Yarn configuration:\n{result.stdout.strip()}")
except Exception as e:
print(f"Error checking Yarn configuration: {e}")
check_yarn_config()
```
---
阅读全文
相关推荐


















