类别: 建站技术

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

问题概述

近日在升级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界面

解决方法

经过分析后发现是由于安装这些包时需要安装依赖包,而其中部分依赖包需要从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安装成功界面

有很多网友问我,修改后仍旧执行不成功是怎么回事。我再给大家分享下,由于墙的原因,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,执行成功

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

查看评论