问题描述
当你执行 npm install xxx 时,是否经常遇到以下超时错误:
npm ERR! network timeout at: https://registry.npmjs.org/package-name
npm ERR! network This is a problem related to network connectivity
或者:
Command timed out after 120 seconds
这种情况通常是由于网络连接不稳定或访问npm官方源速度慢导致的。
解决方案:配置淘宝镜像
操作提示:请在您的本地终端(Terminal/命令行)中执行以下命令
方法一:临时使用(推荐用于测试)
npm install --registry https://registry.npmmirror.com
方法二:全局配置(推荐)
# 设置淘宝镜像
npm config set registry https://registry.npmmirror.com
# 验证配置是否成功
npm config get registry
方法三:使用cnpm(备选方案)
# 安装cnpm
npm install -g cnpm --registry=https://registry.npmmirror.com
# 使用cnpm代替npm
cnpm install package-name
恢复默认配置
如需恢复npm官方源:
npm config set registry https://registry.npmjs.org
验证配置效果
配置完成后,可以通过以下命令测试:
npm install lodash
如果安装速度明显提升且没有timeout错误,说明配置成功。
注意事项
-
建议在遇到timeout问题前就预先配置镜像源
-
淘宝镜像通常每10分钟同步一次npm官方源
-
某些私有包可能需要切换回官方源