技术帝的平台展示,网站建设、网站推广、SEO技术、网站优化、电子商务推广、平面美工和软件测试等技术干货分享。

npm安装包时报错npm ERR! Failed at the gifsicle@4.0.1 postinstall script

建站技术 abel-lan 376℃ 0评论

问题概述

近日在升级npm安装包imagemin-webpack-plugin和imagemin-mozjpeg时,分别报错npm ERR! Failed at the gifsicle@4.0.1 postinstall script.和npm ERR! Failed at the mozjpeg@7.0.0 postinstall script. 将npm register改为国内淘宝镜像还是无法解决该问题。

npm安装包报错ERR! Failed at the mozjpeg@7.0.0 postinstall script界面
npm安装包报错ERR! Failed at the mozjpeg@7.0.0 postinstall script界面

解决方法

经过分析后发现是由于安装这些包时需要安装依赖包,而其中部分依赖包需要从GitHub上下载,而GitHub的资源库DNS有问题,导致这些依赖包无法安装而报错。

找到原因是由于Github DNS的问题。我们在本地hosts文件中(路径C:\Windows\System32\drivers\etc)添加以下内容(Github相关域名的解析地址),然后用npm清楚所有缓存(清除命令为:>npm cache clean -f),之后再重新安装就成功了。

192.30.255.112	gist.github.com
192.30.255.112	github.com
192.30.255.112	www.github.com
151.101.56.133	avatars0.githubusercontent.com
151.101.56.133	avatars1.githubusercontent.com
151.101.56.133	avatars2.githubusercontent.com
151.101.56.133	avatars3.githubusercontent.com
151.101.56.133	avatars4.githubusercontent.com
151.101.56.133	avatars5.githubusercontent.com
151.101.56.133	avatars6.githubusercontent.com
151.101.56.133	avatars7.githubusercontent.com
151.101.56.133	avatars8.githubusercontent.com
151.101.56.133	camo.githubusercontent.com
151.101.56.133	cloud.githubusercontent.com
151.101.56.133	gist.githubusercontent.com
151.101.56.133	marketplace-screenshots.githubusercontent.com
151.101.56.133	raw.githubusercontent.com
151.101.56.133	repository-images.githubusercontent.com
151.101.56.133	user-images.githubusercontent.com

特别强调:以上Github DNS内容可能会变动。如果修改后再次重装还是报错,可以在github.com搜索最新的hosts文件,尝试用不同的hosts文件内容,直到执行成功为止。

更改hosts文件后记得刷新本地DNS。打开cmd,输入以下命令:

ipconfig /flushdns
清除缓存和包imagemin-mozjpeg安装成功界面
清除缓存和包imagemin-mozjpeg安装成功界面

有很多网友问我,修改后仍旧执行不成功是怎么回事。我再给大家分享下,由于墙的原因,Github目前国内无法访问,而修改hosts并不一定能绕过墙执行成功,大家要尝试最新有效的hosts文件。如果多次尝试还是不行,就需要换另外一种方法:用翻墙软件(使用时,请将github.com和githubusercontent.com加入到用户自定义PAC中;如果还是不行,请开启全局模式)。如需这方面好用的软件或使用方法,请留言或发邮件。

下图就是我于2021年3月10日搭建gatsby站点时,报错:npm ERR! Failed at the mozjpeg@7.0.0 postinstall script. 我将hosts修改为最新的,就安装成功了。

更改hosts文件后再刷新本地DNS,执行成功
更改hosts文件后再刷新本地DNS,执行成功

转载请注明:半亩方塘 » npm安装包时报错npm ERR! Failed at the gifsicle@4.0.1 postinstall script

赞 (43)支付宝扫码打赏微信扫码打赏分享
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(4)个小伙伴在吐槽
  1. 太强了,我gatsby 创建 文件老是失败,作者救命了。笔记记下来
    洲际捣蛋2020-10-10 11:35 回复
    • 麻烦你将错误详细信息发过来,我看一下是怎么回事,咱们可以共同努力解决。
      abel-lan2020-10-28 09:29 回复
  2. 作者我下载odbc模块总是失败,这个要怎么去找hosts?
    yyu2021-07-14 16:41 回复
    • hosts文件路径为C:\Windows\System32\drivers\etc,双击选择【记事本】打开,就可编辑修改。
      abel-lan2021-07-21 09:08 回复