前端开发环境搭建踩坑笔记——npm install node-sass安装失败的解决方案_全球资讯

2023-05-06 16:20:04 来源: 博客园

目录
  • 问题背景
  • 问题描述
  • 解决方案
    • 1. 检查node-sass安装版本是否正确。
    • 2. 检查是否是网络问题
  • 总结

问题背景

开发新的前端项目时,总少不了搭建开发环境和执行npm install安装依赖包,但npm install的过程总是充满着玄学,很难保证一次性成功,其中尤其以node-sass这个包的安装失败问题最为常见。像其他npm包安装失败,通常是因为网络问题,可以采用使用更好的网络环境或者切换源的方式进行安装,比如使用淘宝的源进行安装,命令如下:


【资料图】

npm install --registry=https://registry.npm.taobao.org

node-sass安装失败这个问题似乎无法通过上述命令解决。本文将总结遇到此类问题时的解决方案。

问题描述

笔者的测试环境如下:

操作系统:Windows 11Node版本:v14.16.0Npm版本:6.14.11

遇到的node-sass安装失败的报错如下:

gyp ERR! find VSgyp ERR! find VS msvs_version not set from command line or npm configgyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Promptgyp ERR! find VS checking VS2022 (17.5.33530.505) found at:gyp ERR! find VS "D:\Program Files\Microsoft Visual Studio\2022\Community"gyp ERR! find VS - found "Visual Studio C++ core features"gyp ERR! find VS - found VC++ toolset: v143gyp ERR! find VS - missing any Windows SDKgyp ERR! find VS could not find a version of Visual Studio 2017 or newer to usegyp ERR! find VS looking for Visual Studio 2015gyp ERR! find VS - not foundgyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8gyp ERR! find VSgyp ERR! find VS **************************************************************gyp ERR! find VS You need to install the latest version of Visual Studiogyp ERR! find VS including the "Desktop development with C++" workload.gyp ERR! find VS For more information consult the documentation at:gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windowsgyp ERR! find VS **************************************************************gyp ERR! find VSgyp ERR! configure errorgyp ERR! stack Error: Could not find any Visual Studio installation to usegyp ERR! stack     at VisualStudioFinder.fail (D:\temp\npm-temp\node_modules\node-gyp\lib\find-visualstudio.js:122:47)gyp ERR! stack     at D:\temp\npm-temp\node_modules\node-gyp\lib\find-visualstudio.js:75:16gyp ERR! stack     at VisualStudioFinder.findVisualStudio2013 (D:\temp\npm-temp\node_modules\node-gyp\lib\find-visualstudio.js:363:14)gyp ERR! stack     at D:\temp\npm-temp\node_modules\node-gyp\lib\find-visualstudio.js:71:14gyp ERR! stack     at D:\temp\npm-temp\node_modules\node-gyp\lib\find-visualstudio.js:384:16gyp ERR! stack     at D:\temp\npm-temp\node_modules\node-gyp\lib\util.js:54:7gyp ERR! stack     at D:\temp\npm-temp\node_modules\node-gyp\lib\util.js:33:16gyp ERR! stack     at ChildProcess.exithandler (child_process.js:315:5)gyp ERR! stack     at ChildProcess.emit (events.js:315:20)gyp ERR! stack     at maybeClose (internal/child_process.js:1048:16)gyp ERR! System Windows_NT 10.0.22621gyp ERR! command "C:\\Users\\zzcoder\\AppData\\Local\\nvs\\default\\node.exe" "D:\\temp\\npm-temp\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="gyp ERR! cwd D:\temp\npm-temp\node_modules\node-sassgyp ERR! node -v v14.16.0gyp ERR! node-gyp -v v8.4.1gyp ERR! not okBuild failed with error code: 1npm WARN npm-test@1.0.0 No descriptionnpm WARN npm-test@1.0.0 No repository field.npm ERR! code ELIFECYCLEnpm ERR! errno 1npm ERR! node-sass@8.0.0 postinstall: `node scripts/build.js`npm ERR! Exit status 1npm ERR!npm ERR! Failed at the node-sass@8.0.0 postinstall script.npm ERR! This is probably not a problem with npm. There is likely additional logging output above.npm ERR! A complete log of this run can be found in:npm ERR!     C:\Users\zzcoder\AppData\Roaming\npm-cache\_logs\2023-05-06T06_13_42_083Z-debug.log

附上报错截图:

解决方案

1. 检查node-sass安装版本是否正确。

本地安装的node版本不同,需要安装的node-sass版本也是不一样的。node-sass官方给出了不同版本的nodenode-sass的对应关系,读者可访问node-sass github仓库或者node-sass npm仓库进行查看。本地安装的node版本,可以使用如下命令进行查看:

node -v

下图为截至node-sass@8.0.0版本时的对应关系。

在项目中还没有package.json或者有package.json但是此文件没有指定node-sass的版本时,默认会安装node-sass的最新版本。由于笔者安装的node版本时14.16.0,执行npm install node-sass命令默认安装了撰写本文时的node-sass最新版本8.0.0,所以才有了前文中报错。

根据版本对应关系,笔者应该安装node-sass4.14+版本,因此需要将安装命令改为:

npm install node-sass@^4.14.0 --registry=https://registry.npm.taobao.org

这样就是使用淘宝源来安装node-sass4.14+中的最新版本。

2. 检查是否是网络问题

在使用npm install安装node-sass时,会从 github.com上下载 .node文件。由于国内网络环境的问题,这个下载时间可能会很长,甚至导致超时失败。

npm WARN deprecated @npmcli/move-file@2.0.1: This functionality has been moved to @npmcli/fsnpm WARN deprecated @npmcli/move-file@1.1.2: This functionality has been moved to @npmcli/fs> node-sass@8.0.0 install D:\temp\npm-temp\node_modules\node-sass> node scripts/install.jsDownloading binary from https://github.com/sass/node-sass/releases/download/v8.0.0/win32-x64-83_binding.nodeCannot download "https://github.com/sass/node-sass/releases/download/v8.0.0/win32-x64-83_binding.node":HTTP error ETIMEDOUT request to https://github.com/sass/node-sass/releases/download/v8.0.0/win32-x64-83_binding.node failed, reason: connect ETIMEDOUT 20.205.243.166:443Hint: If github.com is not accessible in your location      try setting a proxy via HTTP_PROXY, e.g.      export HTTP_PROXY=http://example.com:1234or configure npm proxy via      npm config set proxy http://example.com:8080> node-sass@8.0.0 postinstall D:\temp\npm-temp\node_modules\node-sass> node scripts/build.js

附上报错截图。

这种情况下仅使用--registry参数指定npm包的下载源是不够的,还需要使用--sass_binary_site参数指定sass二进制文件的下载源,因此需要将安装命令改为:

npm install node-sass@^4.14.0 --registry=https://registry.npm.taobao.org --sass_binary_site=https://npm.taobao.org/mirrors/node-sass/

这样一来就是指定node-sass版本,指定npm包下载源为淘宝源,指定sass下载源为淘宝源,使用此命令基本就都能安装成功了。

总结

遇到node-sass安装失败时,可以使用以下命令:

# 查看本地node版本node -v# 安装node-sassnpm install node-sass@[本地node所对应的node-sass版本] --registry=https://registry.npm.taobao.org --sass_binary_site=https://npm.taobao.org/mirrors/node-sass/

通过下表查看版本对应关系,也可访问node-sass github仓库或者node-sass npm仓库进行查看。

欢迎批评指正。

关键词

精选 导读

公积金贷款上征信吗 使用公积金贷款有什么缺点 环球微头条

实际生活中,由于公积金贷款比商业贷款利率低,不少人都会优先选择公积金贷款。那么,使用公积金贷款时,是

发布时间: 2023-06-01 13:16
宏观   2023-06-01

天天观焦点:乔任梁女友是谁?网传乔任梁女友盘点 附个人资

9月16日晚,网上突然惊现大批爆料,称演员乔任梁在上海桃浦离世。晚上10点许,上海警方微博发布消息,称“9

发布时间: 2023-06-01 13:15
资讯   2023-06-01

仙佑膏药:膏药行业企业积极扩大市场份额,竞争加剧

河南仙佑集团了解到,随着人们对于健康关注的日益增加,膏药行业也得到了快速的发展。膏药作为一种传统的中药制剂,其简便易用、副作用小等

发布时间: 2023-06-01 13:28
行业   2023-06-01

仙佑膏药:膏药行业企业积极扩大市场份额,竞争加剧

河南仙佑集团了解到,随着人们对于健康关注的日益增加,膏药行业也得到了快速的发展。膏药作为一种传统的中药制剂,其简便易用、副作用小等

发布时间: 2023-06-01 13:25
资讯   2023-06-01

时讯:船到桥头自然直

本次数据主要有三大看点,一是制造业PMI持续下降,产需两端都受到影响;二是非制造业PMI指标仍在高景气区间

发布时间: 2023-06-01 12:58
宏观   2023-06-01

【当前热闻】2023时事政治模拟题每日一练(06.01)

1 交通运输部发布数据,2023年4月,全国交通固定资产投资达3150亿元,同比()。A 增长13 6%B 增长11 6%C 增

发布时间: 2023-06-01 13:02
关注   2023-06-01

“你们完成了一项了不起的任务”——中国第三批赴阿卜耶伊维

5月2日-4日,中国第三批赴阿卜耶伊维和直升机分队出动一架直升机赴苏丹卡杜格利撤离滞留的联合国工作人员。

发布时间: 2023-06-01 12:48
关注   2023-06-01

秦牧智库专家宋宇轩研究员参加亚洲 乳业对话论坛并作报告

中国农科新闻网是农业科技报社顺应网络时代新媒体发展趋势,在三农领域倾心打造的集资讯、互动、网上展示于

发布时间: 2023-06-01 12:41
资讯   2023-06-01

魔兽世界瞬移百度云_魔兽世界瞬间潜行

1、有时候就是站在面前也不会触发任务,那就需要你在水里面来回游几次就可以了在水边找一个木板,这个有提示

发布时间: 2023-06-01 12:17
资讯   2023-06-01

淘快乐 淘成长!来郑州市管城回族区腾飞路七里河小学跳蚤市

为提高学生劳动实践能力,让学生有更多机会体验社会生活,5月31日下午,管城回族区腾飞路七里河小学一年一

发布时间: 2023-06-01 12:06
热点   2023-06-01

低成本手机附件测血压“触手可及”_每日观点

科技日报讯(记者张佳欣)美国加州大学圣地亚哥分校的工程师创造了一种廉价的解决方案来降低血压监测的门槛

发布时间: 2023-06-01 11:44
热点   2023-06-01

青岛:税费红利让企业发展更添“绿意”

青岛有着“红瓦绿树、碧海蓝天”的独特城市风貌,绿色低碳、青青之岛一直是这座城市孜孜不倦的追求和发展底

发布时间: 2023-06-01 11:35
宏观   2023-06-01

天天热消息:合资品牌新车型“低姿态”定价 能挽回多少份额

【合资品牌新车型低姿态定价】最近两周,合资大厂推新车,引发广泛关注。东风日产、上汽通用别克等合资品牌

发布时间: 2023-06-01 11:34
关注   2023-06-01

聊城市直、开发区、高新区教师招聘成绩可查询!面试名单公布

关于公布2023年聊城市教育和体育局直属学校公开招聘、开发区和高新区分局所属事业单位公开招聘笔试成绩等有

发布时间: 2023-06-01 11:15
行业   2023-06-01

捐赠送温暖 关爱伴成长——信阳市平桥农信联社开展“六一”

中国网讯 5月31日,在“六一”儿童节来临之际,平桥农信联社党委、工会组织工作人员前往困难职工家中以及

发布时间: 2023-06-01 11:05
资讯   2023-06-01

世界热议:摩根大通重磅发声!

中国基金报吴娟娟5月31日,2023年摩根大通全球中国峰会于上海开幕,汇聚全球37个国家和地区,超过2600名参

发布时间: 2023-06-01 10:52
宏观   2023-06-01

天天关注:百余名专家到场!本周五,济南泉城广场有大型义诊

百余名大专家来了!6月2日(周五)上午9时,山东第一医科大学(山东省医学科学院)联合济南国际医学中心,

发布时间: 2023-06-01 10:55
关注   2023-06-01

环球速看:喜君贻我一枝春中的一枝春指的是_喜君贻我一枝春

1、《刘邦直送早梅水仙花四首》其一簸船綪缆北风嗔,霜落千林憔悴人。2、欲问江南近消息,喜君贻我一枝春。

发布时间: 2023-06-01 10:47
热点   2023-06-01

最快8分钟拿到事故认定书!四川两地试点轻微道路交通事故远

川观新闻记者张庭铭5月31日,川观新闻记者从四川省公安厅交通警察总队获悉,6月1日起公安交管服务群众服务

发布时间: 2023-06-01 10:33
资讯   2023-06-01

工信部组织开展全国人大建议《关于支持四川建设钒钛战略材料

工信部组织开展全国人大建议《关于支持四川建设钒钛战略材料、新能源汽车、集成电路等重要产业备份基地》专

发布时间: 2023-06-01 10:43
宏观   2023-06-01