npm安装node-sass报msbuild相关错误的解决办法

报错信息

gyp verb command build []
gyp verb build type Release
gyp verb architecture x64
gyp verb node dev dir C:\Users\XXX\.node-gyp\10.15.0
gyp verb found first Solution file build/binding.sln
gyp verb could not find "msbuild.exe" in PATH - finding location in registry
gyp ERR! build error
gyp ERR! stack Error: Command failed: C:\Windows\System32\reg.exe query "HKLM\Software\Microsoft\MSBuild\ToolsVersions" /s /reg:32
gyp ERR! stack ����: ע����༭������Ա���á�
gyp ERR! stack
gyp ERR! stack Can't find "msbuild.exe". Do you have Microsoft Visual Studio C++ 2008+ installed?
gyp ERR! stack     at D:\DayDayUp\testpro\testpro-admin\node_modules\node-gyp\lib\build.js:142:25
gyp ERR! stack     at ChildProcess.exithandler (child_process.js:301:5)
gyp ERR! stack     at ChildProcess.emit (events.js:182:13)
gyp ERR! stack     at maybeClose (internal/child_process.js:962:16)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:251:5)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "D:\\nodejs\\node.exe" "D:\\DayDayUp\\testpro\\testpro-admin\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd D:\DayDayUp\testpro\testpro-admin\node_modules\node-sass
gyp ERR! node -v v10.15.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
Build failed with error code: 1
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.7 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.7: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-sass@4.11.0 postinstall: `node scripts/build.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-sass@4.11.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!     D:\nodejs\node_cache\_logs\2019-02-26T01_59_23_671Z-debug.log

提取关键信息:
gyp verb could not find “msbuild.exe” in PATH - finding location in registry
gyp ERR! stack Can’t find “msbuild.exe”. Do you have Microsoft Visual Studio C++ 2008+ installed?


带来的思考

  • 这日志从哪里打印出来的

经过分析发现,该日志是node-gyp包的build.js打印出来的,该文件路径为{YOUR DIR}\node_modules\node-gyp\lib。网上针对该问题是修改build.js文件,如下:

var cmd = 'reg.exe query "HKLM\\Software\\Microsoft\\MSBuild\\ToolsVersions" /s'
改为
var cmd = 'C:\\Windows\\System32\\reg.exe query "HKLM\\Software\\Microsoft\\MSBuild\\ToolsVersions" /s'

然而,我试了下,没有解决问题!!!

  • 既然npm安装不了node-sass,那我们换cnpm是否可以

于是我就尝试安装cnpm,再用cnpm安装node-sass,命令如下:

$ npm install -g cnpm
$ cnpm install -g node-sass

竟然没有报错有没有!!!,于是我们思考一下,npm与cnpm的安装区别到底在哪里,是下载的源不一样,还是实现安装逻辑不一样?

  • 我们给npm换源试一试?

我们都知道怎么给npm换taobao源,但是我们没有给sass单独设置过数据源,说做就做:

$ npm config set sass-binary-site http://npm.taobao.org/mirrors/node-sass
$ npm install node-sass

竟然也成功了!!!

总结

看过很多大佬在github上开源代码,都建议使用npm install安装依赖包,不建议使用cnpm,说是会遇到未知问题。所以,如果你也遇到该问题,建议使用最后一种方法解决(npm config set sass-binary-site http://npm.taobao.org/mirrors/node-sass)。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值