1. 检查网络连接
-
确认互联网连接正常。 尝试访问一些网站,例如 Google 或 Baidu,确保浏览器可以正常上网。
-
尝试 Ping 一下默认的注册表地址:
ping registry.npmjs.org看看是否能正常解析并收到回复。
2. 更换 NPM/Yarn 注册表镜像
这是解决国内网络连接 npm/Yarn 注册表慢或不稳定的常见方法。可以将其切换到国内的镜像源,例如淘宝 NPM 镜像。
-
临时切换(仅对当前命令):
yarn install --registry=https://registry.npmmirror.com/ -
全局切换到淘宝镜像:
yarn config set registry https://registry.npmmirror.com/或者使用 npm 命令行工具设置:
npm config set registry https://registry.npmmirror.com/(Yarn 通常会读取 npm 的配置,但直接设置 Yarn 自己的配置更可靠)
-
如果需要恢复默认注册表:
yarn config set registry https://registry.npmjs.org/或者
npm config set registry https://registry.npmjs.org/
3. 清除 NPM/Yarn 缓存
有时损坏的缓存也会导致一些奇怪的问题。
yarn cache clean --force
4. 检查代理设置
如果在网络中或使用了代理服务器:
-
检查当前的代理配置:
npm config get proxy npm config get https-proxy -
设置代理(如果需要):
npm config set proxy http://your.proxy.com:port npm config set https-proxy http://your.proxy.com:port npm config set strict-ssl false # 如果你的代理使用了自签名证书,可能需要这个(不推荐长期使用)请将
http://your.proxy.com:port替换为实际的代理地址和端口。 -
如果代理不需要认证,并且是系统级别的,Yarn 可能会自动使用。如果需要认证,需要确保
npm或yarn配置了正确的认证信息。