npm安装依赖失败

2020-12-01  本文已影响0人  浅忆_0810

1. 下载二进制文件失败

下载二进制文件失败

解决办法:

1.1 设置 .npmrc 文件

虽然已设置国内镜像源, 有时候 A 包中需要下载 B 包, 这时还可能到国外站点下载 B 包

.npmrc 文件可以提供「变量」设置某些包的下载地址也为国内镜像.

windows 系统:文件位置一般为 C:/Users/Administrator(当前用户名)/.npmrc

macOS 系统:文件位置一般为用户家目录,cd回车直接切到家目录,ls -al 查看

把下面常见包地址复制到.npmrc 中,从而提高下载成功率

# mirror config
sharp_binary_host = https://npmmirror.com/mirrors/sharp
sharp_libvips_binary_host = https://npmmirror.com/mirrors/sharp-libvips
profiler_binary_host_mirror = https://npmmirror.com/mirrors/node-inspector/
fse_binary_host_mirror = https://npmmirror.com/mirrors/fsevents
node_sqlite3_binary_host_mirror = https://npmmirror.com/mirrors
sqlite3_binary_host_mirror = https://npmmirror.com/mirrors
sqlite3_binary_site = https://npmmirror.com/mirrors/sqlite3
sass_binary_site = https://npmmirror.com/mirrors/node-sass
electron_mirror = https://npmmirror.com/mirrors/electron/
puppeteer_download_host = https://npmmirror.com/mirrors
chromedriver_cdnurl = https://npmmirror.com/mirrors/chromedriver
operadriver_cdnurl = https://npmmirror.com/mirrors/operadriver
phantomjs_cdnurl = https://npmmirror.com/mirrors/phantomjs
python_mirror = https://npmmirror.com/mirrors/python
registry = https://registry.npmmirror.com
disturl = https://npmmirror.com/dist

1.2 设置 hosts 文件

有些包在国内镜像中没有及时更新, 或者根本没有

国外站点下载不通畅多是因为在「域名 => IP」阶段受阻

我们的解决方案就是提前把「域名与IP的对应关系」准备好,放在本在 hosts文件中

编辑 C:\Windows\System32\drivers\etc\hosts 文件

# GitHub520 Host Start
140.82.112.26                 alive.github.com
140.82.112.25                 live.github.com
185.199.108.154               github.githubassets.com
140.82.112.21                 central.github.com
185.199.108.133               desktop.githubusercontent.com
185.199.108.153               assets-cdn.github.com
185.199.108.133               camo.githubusercontent.com
185.199.108.133               github.map.fastly.net
199.232.69.194                github.global.ssl.fastly.net
140.82.113.4                  gist.github.com
185.199.108.153               github.io
140.82.112.3                  github.com
192.0.66.2                    github.blog
140.82.113.5                  api.github.com
185.199.108.133               raw.githubusercontent.com
185.199.108.133               user-images.githubusercontent.com
185.199.108.133               favicons.githubusercontent.com
185.199.108.133               avatars5.githubusercontent.com
185.199.108.133               avatars4.githubusercontent.com
185.199.108.133               avatars3.githubusercontent.com
185.199.108.133               avatars2.githubusercontent.com
185.199.108.133               avatars1.githubusercontent.com
185.199.108.133               avatars0.githubusercontent.com
185.199.108.133               avatars.githubusercontent.com
140.82.112.9                  codeload.github.com
52.217.166.89                 github-cloud.s3.amazonaws.com
52.217.172.41                 github-com.s3.amazonaws.com
52.216.28.148                 github-production-release-asset-2e65be.s3.amazonaws.com
52.216.152.124                github-production-user-asset-6210df.s3.amazonaws.com
52.216.169.171                github-production-repository-file-5c1aeb.s3.amazonaws.com
185.199.108.153               githubstatus.com
64.71.144.202                 github.community
23.100.27.125                 github.dev
185.199.108.133               media.githubusercontent.com

补充:里面内容不是百分百固定, 遇到报错提示 某个域名连接失败, 就到 https://ip138.com/ 查找其相应的 IP 地址, 然后把 IP地址与域名的对应信息追加到上面 hosts文件中

以上内容不行, 也可以参考下面网址:

https://github.com/521xueweihan/GitHub520

下载文件试试

激活生效

大部分情况下是直接生效,如未生效可尝试下面的办法,刷新 DNS:

  1. Windows:在 CMD 窗口输入:ipconfig /flushdns
  2. Linux 命令:sudo nscd restart,如报错则须安装:sudo apt install nscdsudo /etc/init.d/nscd restart
  3. Mac 命令:sudo killall -HUP mDNSResponder

Tips: 上述方法无效可以尝试重启机器。

为什么要配置它,有啥用?

有些时候之所以访问不了,就是 DNS 不告诉你

现在咱自己记着 IP, 跳过这一步,就能访问了

1.3 最后的办法

# 安装 cnpm, 之后使用 cnpm 下载安装包
npm install -g cnpm --registry=https://registry.npm.taobao.org

2. cnpm无法加载文件

cnpm-error
  1. 以管理员身份运行PowerShell
  2. 输入set-ExecutionPolicy RemoteSigned
  3. 更改权限为A
  4. 可通过get-ExecutionPolicy查看当前的状态
cnpm-resolve
上一篇下一篇

猜你喜欢

热点阅读