Yarn 是一个高效、可靠的 JavaScript 依赖管理工具,但在使用过程中,可能会遇到各种问题。本文总结了一些常见的 Yarn 错误及其排查方法,帮助你快速定位并解决问题。
error An unexpected error occurred: "https://registry.yarnpkg.com/...: ETIMEDOUT".
✅ 网络不稳定,导致请求超时
✅ 公司或学校的网络限制了外部访问
✅ 使用了代理,但代理未正确配置
检查网络:确保能够访问 https://registry.yarnpkg.com/
,可以在浏览器中打开此地址测试。
增加超时时间:
yarn install --network-timeout 600000
配置代理(如有需要):
yarn config set proxy http://your-proxy:port
yarn config set https-proxy http://your-proxy:port
尝试 VPN 或切换网络
Integrity check failed for "esbuild" (computed integrity doesn't match our records)
✅ 下载的包与 yarn.lock
记录的哈希值不一致
✅ Nexus 或私有 npm 仓库中的包文件损坏
✅ 本地缓存数据损坏
清理缓存并重新安装:
yarn cache clean
rm -rf node_modules yarn.lock
yarn install --network-timeout 600000
手动检查包是否损坏: 在浏览器中下载出错的 .tgz
文件,使用 shasum -a 512
计算哈希值,对比 yarn.lock
是否匹配。
联系 Nexus/仓库管理员,确认远程包是否正确。
error An unexpected error occurred: "https://nexus.example.com/repository/npm/...: 404 Not Found".
✅ 配置的私有 npm 仓库地址错误
✅ 该包未正确发布到私有仓库
✅ 没有权限访问该仓库
检查 npm/yarn registry 配置:
yarn config get registry
尝试使用官方 npm 源(如果可以):
yarn config set registry https://registry.npmjs.org/
检查 Nexus 或私有 npm 账号权限
error Your current version of Yarn is out of date.
✅ 项目依赖的 Yarn 版本与本地安装的版本不兼容
✅ 某些新功能或修复未在旧版 Yarn 上支持
升级 Yarn(推荐使用 corepack):
corepack enable
corepack prepare yarn@stable --activate
手动安装最新 Yarn:
npm install -g yarn
查看项目需要的 Yarn 版本(检查 .yarnrc
或 package.json
中的 engines
字段)
rm -rf node_modules yarn.lock
yarn install --network-timeout 600000
yarn install --skip-integrity-check
yarn install --verbose
Yarn 的报错信息通常能提供一定的线索,结合 --verbose
参数和 yarn.lock
文件,可以更快找到问题的根本原因。遇到问题时,可以按照以下步骤排查:
检查网络,是否能访问 registry.yarnpkg.com
或私有仓库。
清理缓存,重新安装 yarn cache clean && yarn install
。
检查仓库配置,确认 registry 地址和权限。
更新 Yarn 版本,避免旧版本导致的兼容性问题。
查看日志,使用 --verbose
获取更多错误信息。
如果问题仍然无法解决,可以在 GitHub 或官方 Yarn 讨论区查找类似案例,或者向团队/管理员寻求帮助。
欢迎在评论区交流你的问题和解决方案!