node-sass 安装失败如何解决

2020-03-14T00:01:00

在安装node-sass时出现了以下的错误,试了网上的许多方法都没能解决。一开始以为是环境的问题,node.js版本太高,或是sass-loader的版本,但是都没能奏效。
用以下信息搜索也未能解决
gyp ERR! build error
gyp ERR! stack Error: C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe failed with exit code: 1
完整的报错信息如下:

“D:\Project\vue-travel\node_modules\node-sass\build\binding.sln”(默认目标) (1) ->
“D:\Project\vue-travel\node_modules\node-sass\build\binding.vcxproj.metaproj”(默认目标) (2) ->
“D:\Project\vue-travel\node_modules\node-sass\build\binding.vcxproj”(默认目标) (4) ->
(Link 目标) ->
  C:\Users\Troku\.node-gyp\12.16.1\x64\node.lib : fatal error LNK1107: 文件无效或损坏: 无法在 0x46864E 处读取 [D:\Project\vue-travel\node_modules\node-sass\bu
ild\binding.
vcxproj]

    8 个警告
    1 个错误

已用时间 00:01:16.87
gyp ERR! build error
gyp ERR! stack Error: `C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onExit (D:\Project\vue-travel\node_modules\node-gyp\lib\build.js:262:23)
gyp ERR! stack     at ChildProcess.emit (events.js:311:20)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
gyp ERR! System Windows_NT 10.0.18362
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "D:\\Project\\vue-travel\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd D:\Project\vue-travel\node_modules\node-sass
gyp ERR! node -v v12.16.1
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.11 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.11: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-sass@4.13.1 postinstall: `node scripts/build.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-sass@4.13.1 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\Troku\AppData\Roaming\npm-cache\_logs\2020-03-13T16_00_51_588Z-debug.log


最后我通过更换node.lib解决了这个问题。一般情况下这个文件在C:\Users\你的用户名\.node-gyp\你的Node.js的版本号\x64这个文件夹下,然后去官网去下载对应版本的node.lib文件去替换即可。例如我的是Node.js的版本是12.6.1,因为访问速度问题我选择淘宝镜像来下载
替换node.lib文件后重新安装开发依赖。

npm i node-sass --save-dev

当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »