node-sass 踩坑记录

node-sass 踩坑记录

一、问题描述

笔者node 版本为20.12.1,需要安装的node-sass版本为9.0.0。

笔者对前端工程执行 npm install指令时,在 npm install node-sass处发生了错误。错误如下:

935 error gyp verb build dir attempting to create "build" dir: D:\work\irp\irp-frontend\node_modules\node-sass\build
935 error gyp verb build dir "build" dir needed to be created? Yes
935 error gyp verb find VS msvs_version was set from command line or npm config
935 error gyp verb find VS - looking for Visual Studio version 2022
935 error gyp verb find VS VCINSTALLDIR not set, not running in VS Command Prompt
935 error gyp verb find VS checking VS2022 (17.9.34902.65) found at:
935 error gyp verb find VS "C:\Program Files\Microsoft Visual Studio\2022\Community"
935 error gyp verb find VS - found "Visual Studio C++ core features"
935 error gyp verb find VS - found VC++ toolset: v143
935 error gyp verb find VS - missing any Windows SDK
935 error gyp verb find VS checking VS2022 (17.9.34902.65) found at:
935 error gyp verb find VS "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools"
935 error gyp verb find VS - found "Visual Studio C++ core features"
935 error gyp verb find VS - found VC++ toolset: v143
935 error gyp verb find VS - missing any Windows SDK
935 error gyp verb find VS could not find a version of Visual Studio 2017 or newer to use
935 error gyp verb find VS looking for Visual Studio 2015
935 error gyp verb find VS - not found
935 error gyp verb find VS not looking for VS2013 as it is only supported up to Node.js 8
935 error gyp ERR! find VS 
935 error gyp ERR! find VS msvs_version was set from command line or npm config
935 error gyp ERR! find VS - looking for Visual Studio version 2022
935 error gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
935 error gyp ERR! find VS checking VS2022 (17.9.34902.65) found at:
935 error gyp ERR! find VS "C:\Program Files\Microsoft Visual Studio\2022\Community"
935 error gyp ERR! find VS - found "Visual Studio C++ core features"
935 error gyp ERR! find VS - found VC++ toolset: v143
935 error gyp ERR! find VS - missing any Windows SDK
935 error gyp ERR! find VS checking VS2022 (17.9.34902.65) found at:
935 error gyp ERR! find VS "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools"
935 error gyp ERR! find VS - found "Visual Studio C++ core features"
935 error gyp ERR! find VS - found VC++ toolset: v143
935 error gyp ERR! find VS - missing any Windows SDK
935 error gyp ERR! find VS could not find a version of Visual Studio 2017 or newer to use
935 error gyp ERR! find VS looking for Visual Studio 2015
935 error gyp ERR! find VS - not found
935 error gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
935 error gyp ERR! find VS 
935 error gyp ERR! find VS valid versions for msvs_version:
935 error gyp ERR! find VS 
935 error gyp ERR! find VS **************************************************************
935 error gyp ERR! find VS You need to install the latest version of Visual Studio
935 error gyp ERR! find VS including the "Desktop development with C++" workload.
935 error gyp ERR! find VS For more information consult the documentation at:
935 error gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows
935 error gyp ERR! find VS **************************************************************
935 error gyp ERR! find VS 
935 error gyp ERR! configure error 
935 error gyp ERR! stack Error: Could not find any Visual Studio installation to use
935 error gyp ERR! stack     at VisualStudioFinder.fail (D:\work\irp\irp-frontend\node_modules\node-gyp\lib\find-visualstudio.js:122:47)
935 error gyp ERR! stack     at D:\work\irp\irp-frontend\node_modules\node-gyp\lib\find-visualstudio.js:75:16
935 error gyp ERR! stack     at VisualStudioFinder.findVisualStudio2013 (D:\work\irp\irp-frontend\node_modules\node-gyp\lib\find-visualstudio.js:363:14)
935 error gyp ERR! stack     at D:\work\irp\irp-frontend\node_modules\node-gyp\lib\find-visualstudio.js:71:14
935 error gyp ERR! stack     at D:\work\irp\irp-frontend\node_modules\node-gyp\lib\find-visualstudio.js:384:16
935 error gyp ERR! stack     at D:\work\irp\irp-frontend\node_modules\node-gyp\lib\util.js:54:7
935 error gyp ERR! stack     at D:\work\irp\irp-frontend\node_modules\node-gyp\lib\util.js:33:16
935 error gyp ERR! stack     at ChildProcess.exithandler (node:child_process:430:5)
935 error gyp ERR! stack     at ChildProcess.emit (node:events:518:28)
935 error gyp ERR! stack     at maybeClose (node:internal/child_process:1105:16)
935 error gyp ERR! stack     at ChildProcess._handle.onexit (node:internal/child_process:305:5)
935 error gyp ERR! System Windows_NT 10.0.22631
935 error gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "D:\\work\\irp\\irp-frontend\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
935 error gyp ERR! cwd D:\work\irp\irp-frontend\node_modules\node-sass
935 error gyp ERR! node -v v20.12.1
935 error gyp ERR! node-gyp -v v8.4.1
935 error gyp ERR! not ok 
935 error Build failed with error code: 1
936 silly unfinished npm timer reify 1716198365841
937 silly unfinished npm timer reify:build 1716198369813
938 silly unfinished npm timer build 1716198369814
939 silly unfinished npm timer build:deps 1716198369814
940 silly unfinished npm timer build:run:postinstall 1716198370636
941 silly unfinished npm timer build:run:postinstall:node_modules/node-sass 1716198370636
942 verbose cwd D:\work\irp\irp-frontend
943 verbose os Windows_NT 10.0.22631
944 verbose node v20.12.1
945 verbose npm  v10.8.0
946 verbose exit 1
947 verbose code 1

二、解决办法

上述报错主要原因如下,找不到任何安装的 Visual Studio

Could not find any Visual Studio installation to use

根据报错,笔者进行了搜索和报错提示进入了GitHub - nodejs/node-gyp: Node.js native addon build tool进行查询。

主要解决办法如下:

2.1 安装Visual Studio

根据报错提示安装最新版本的Visual Studio, 并引入相关的c++桌面开发(此处有大坑)

笔者安装了最新的2022 版本Visual Studio,但是,没有任何效果。

笔者根据报错发现如下

935 error gyp ERR! find VS - looking for Visual Studio version 2022
935 error gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
935 error gyp ERR! find VS checking VS2022 (17.9.34902.65) found at:
935 error gyp ERR! find VS "C:\Program Files\Microsoft Visual Studio\2022\Community"
935 error gyp ERR! find VS - found "Visual Studio C++ core features"
935 error gyp ERR! find VS - found VC++ toolset: v143
935 error gyp ERR! find VS - missing any Windows SDK
935 error gyp ERR! find VS checking VS2022 (17.9.34902.65) found at:
935 error gyp ERR! find VS "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools"
935 error gyp ERR! find VS - found "Visual Studio C++ core features"
935 error gyp ERR! find VS - found VC++ toolset: v143
935 error gyp ERR! find VS - missing any Windows SDK
935 error gyp ERR! find VS could not find a version of Visual Studio 2017 or newer to use
935 error gyp ERR! find VS looking for Visual Studio 2015
935 error gyp ERR! find VS - not found

确实Visual Studio已经安装,但是missing any Windows SDK,于是笔者在Visual Studio中进行查找Windows SDK

笔者公司的电脑windows版本为win11,上面也存在windows 11 SDK,按理来说不会出现上述情况。于是笔者猜测可能是别的原因导致的,便去寻找其他方法,皆不能解决问题。

于是回到原始报错:missing any Windows SDK

既然是missing,笔者想会不会这只存在win11 SDK的原因,于是便安装了win10的SDK,如下:

神奇的是,win10的SDK居然被找到了,该问题就这样被解决了。本次任务时间较紧,此次就不再深究了。

934 error gyp verb build dir attempting to create "build" dir: D:\work\irp\irp-frontend\node_modules\node-sass\build
934 error gyp verb build dir "build" dir needed to be created? Yes
934 error gyp verb find VS msvs_version was set from command line or npm config
934 error gyp verb find VS - looking for Visual Studio version 2022
934 error gyp verb find VS VCINSTALLDIR not set, not running in VS Command Prompt
934 error gyp verb find VS checking VS2022 (17.9.34902.65) found at:
934 error gyp verb find VS "C:\Program Files\Microsoft Visual Studio\2022\Community"
934 error gyp verb find VS - found "Visual Studio C++ core features"
934 error gyp verb find VS - found VC++ toolset: v143
934 error gyp verb find VS - found Windows SDK: 10.0.20348.0

2.2 安装setuptools

尽管对于笔者,还出现新的报错,但那都不是问题。

934 error Traceback (most recent call last):
934 error   File "D:\work\irp\irp-frontend\node_modules\node-gyp\gyp\gyp_main.py", line 42, in <module>
934 error     import gyp  # noqa: E402
934 error     ^^^^^^^^^^
934 error   File "D:\work\irp\irp-frontend\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 9, in <module>
934 error     import gyp.input
934 error   File "D:\work\irp\irp-frontend\node_modules\node-gyp\gyp\pylib\gyp\input.py", line 19, in <module>
934 error     from distutils.version import StrictVersion
934 error ModuleNotFoundError: No module named 'distutils'
934 error gyp ERR! configure error 
934 error gyp ERR! stack Error: `gyp` failed with exit code: 1
934 error gyp ERR! stack     at ChildProcess.onCpExit (D:\work\irp\irp-frontend\node_modules\node-gyp\lib\configure.js:259:16)
934 error gyp ERR! stack     at ChildProcess.emit (node:events:518:28)
934 error gyp ERR! stack     at ChildProcess._handle.onexit (node:internal/child_process:294:12)
934 error gyp ERR! System Windows_NT 10.0.22631
934 error gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "D:\\work\\irp\\irp-frontend\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
934 error gyp ERR! cwd D:\work\irp\irp-frontend\node_modules\node-sass
934 error gyp ERR! node -v v20.12.1
934 error gyp ERR! node-gyp -v v8.4.1
934 error gyp ERR! not ok 
934 error Build failed with error code: 1

这个报错是因为笔者的node-sass依赖于python内的distutils模块,笔者的python版本为3.12.3,已经移除了这个模块,

有两种解决办法:1.降低python的版本,笔者的程序依赖的python版本较高,不能降低版本,因此采取第二种办法

2.安装setuptools模块,“setuptools”是一个处理Python软件包的工具包,它依赖于 distutils。安装 setuptools可以间接解决没有“distutils”的问题。执行以下语句即可安装:

pip install setuptools

至此,前端程序可以正常npm install和npm run build。

三、其他解决办法

3.1 全局安装构建工具

npm install --global windows-build-tools

安装该工具需要python2.7的环境,笔者前端依赖python较高,即使降低版本也会出现其他问题,故此该方法不适合笔者。

3.2 node-sass 与node版本不一致

可在官网(node-sass - npm)查看版本对应关系

  • 21
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值