npm 下载 依赖包时出错的解决方式
一.设置 npm 镜像源
设置为国内镜像源
npm config set registry http://registry.npm.taobao.org
1
查看当前镜像源
npm config get registry
1
恢复原来镜像源
npm config set registry http://registry.npmjs.org
二.设置 .npmrc 文件
虽然已设置国内镜像源, 有时候 A 包中需要下载 B 包, 这时还可能到国外站点下载 B 包
.npmrc 文件可以提供「变量」设置某些包的下载地址也为国内镜像.
文件位置一般为 C:/Users/Administrator(当前用户名)/.npmrc
把下面常见包地址复制到 .npmrc 中,从而提高下载成功率
sharp_binary_host = https://npm.taobao.org/mirrors/sharp
sharp_libvips_binary_host = https://npm.taobao.org/mirrors/sharp-libvips
profiler_binary_host_mirror = https://npm.taobao.org/mirrors/node-inspector/
fse_binary_host_mirror = https://npm.taobao.org/mirrors/fsevents
node_sqlite3_binary_host_mirror = https://npm.taobao.org/mirrors
sqlite3_binary_host_mirror = https://npm.taobao.org/mirrors
sqlite3_binary_site = https://npm.taobao.org/mirrors/sqlite3
sass_binary_site = https://npm.taobao.org/mirrors/node-sass
electron_mirror = https://npm.taobao.org/mirrors/electron/
puppeteer_download_host = https://npm.taobao.org/mirrors
chromedriver_cdnurl = https://npm.taobao.org/mirrors/chromedriver
operadriver_cdnurl = https://npm.taobao.org/mirrors/operadriver
phantomjs_cdnurl = https://npm.taobao.org/mirrors/phantomjs
python_mirror = https://npm.taobao.org/mirrors/python
registry = https://registry.npm.taobao.org/
disturl = https://npm.taobao.org/dist
三.设置 hosts 文件
有些包在国内镜像中没有及时更新, 或者根本没有.
国外站点下载不通畅多是因为在「域名 => IP」阶段受阻
我们的解决方案就是提前把「域名与IP的对应关系」准备好,放在本在 hosts 文件中
编辑 C:\Windows\System32\drivers\etc\hosts 文件
52.74.223.119 github.com
199.232.28.133 raw.githubusercontent.com
199.232.96.133 raw.githubusercontent.com
54.169.195.247 api.github.com
140.82.112.25 live.github.com
59.24.3.173 gist.github.com
185.199.108.154 github.githubassets.com
# 185.199.109.154 github.githubassets.com
# 185.199.110.154 github.githubassets.com
# 185.199.111.154 github.githubassets.com
34.196.246.152 collector.githubapp.com
# 52.206.227.240 collector.githubapp.com
52.216.207.115 github-cloud.s3.amazonaws.com
140.82.112.21 central.github.com
151.101.108.133 raw.githubusercontent.com
151.101.108.133 user-images.githubusercontent.com
151.101.108.133 desktop.githubusercontent.com
151.101.76.133 raw.githubusercontent.com
151.101.196.133 raw.githubusercontent.com
151.101.108.133 avatars.githubusercontent.com
151.101.108.133 avatars0.githubusercontent.com
151.101.108.133 avatars1.githubusercontent.com
151.101.108.133 avatars2.githubusercontent.com
151.101.108.133 avatars3.githubusercontent.com
151.101.108.133 avatars4.githubusercontent.com
151.101.108.133 avatars5.githubusercontent.com
151.101.108.133 avatars6.githubusercontent.com
151.101.108.133 avatars7.githubusercontent.com
151.101.108.133 avatars8.githubusercontent.com
151.101.108.133 avatars9.githubusercontent.com
151.101.108.133 avatars10.githubusercontent.com
151.101.108.133 avatars11.githubusercontent.com
151.101.108.133 avatars12.githubusercontent.com
151.101.108.133 avatars13.githubusercontent.com
151.101.108.133 avatars14.githubusercontent.com
151.101.108.133 avatars15.githubusercontent.com
151.101.108.133 avatars16.githubusercontent.com
151.101.108.133 avatars17.githubusercontent.com
151.101.108.133 avatars18.githubusercontent.com
151.101.108.133 avatars19.githubusercontent.com
151.101.108.133 avatars20.githubusercontent.com