NPM安装node-sass失败

52

我正在尝试在一个项目中安装node-sass,在我的电脑上可以完美地构建和运行,但在我的Surface上安装包时出现了严重问题。

注意:我已经尝试过多次重新安装和重建项目。

错误摘要:

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 (C:\Users\Max-T\Documents\Sources\StaffMangerV2\black-dashboard-pro-react-v1.0.0\node_modules\node-gyp\lib\build.js:262:23)
gyp ERR! stack     at ChildProcess.emit (events.js:210:5)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:272:12)
gyp ERR! System Windows_NT 10.0.18362
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\Max-T\\Documents\\Sources\\StaffMangerV2\\black-dashboard-pro-react-v1.0.0\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd C:\Users\Max-T\Documents\Sources\StaffMangerV2\black-dashboard-pro-react-v1.0.0\node_modules\node-sass
gyp ERR! node -v v12.13.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.4 (node_modules\react-scripts\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: 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!     C:\Users\Max-T\AppData\Roaming\npm-cache\_logs\2019-10-30T10_05_01_386Z-debug.log

package.json => https://hastebin.com/azetegukes.pl 代码太多,所以我不得不将它粘贴到其他地方。


你尝试过使用yarn吗?大多数情况下,yarn都能解决我的这个问题。 - Rehan Sattar
你检查过你的npm配置文件了吗?因为有时它会损坏并导致这样的问题。你可以运行npm run --config。 - Mohit
@Mohit 尝试了那个,但输出对我来说看起来很好 => hastebin - Maxdola
@RehanSattar 我之前也尝试过,现在又尝试了一遍,并提供了完整的错误日志 => hastebin - Maxdola
3
使用 NODE VERSION 10,它可以正常工作。要在同一台计算机上使用多个 NODE 版本,您可以使用 NVM - Parth Developer
7个回答

46

谢谢,那实际上解决了安装错误,但在浏览器中仍遇到同样的错误 => hastebin - Maxdola
1
那看起来像是一个单独的问题。 - nschonni
实际上是真的...所以我会问一个新的。 - Maxdola
9
值得一提的是,每个 Node 版本都有特定的 node-sass 版本要求,更多信息请见这里:https://github.com/sass/node-sass#node-version-support-policy。 - Piotr Jaworski
还值得一提的是node版本管理器,这样您就可以快速切换节点版本以运行旧项目! - adamdport
@adamdport 谢谢!nvm是一个很酷的工具,可以快速切换不同的npm和node版本,而不会弄乱事情。 - Sohail Saha

23
如果最近有人遇到这个问题...... 最简单的解决方法是在依赖项中用ass 替换 node-sass。 这是更新版本,我只需要更改代码中的一行初始化内容,现有的所有代码都已兼容。
有更多细节,请参见https://sass-lang.com/blog/libsass-is-deprecated迁移指南

1
你是最棒的。你救了我。谢谢伙计。 - Saima Haji
太棒了!我必须使用 nvm 将我的 Node 版本降级到 14。但是没错,它起作用了。 - Sudarshan
这也可能是一种更具未来性的方法,因为如上所述,__node-sass正在被弃用__。 - Robert Dundon

17

不再使用node-sass

node-sass已弃用,应该使用新版本的sass

您可以使用以下命令卸载旧版本并安装新版本

npm uninstall node-sass
npm install sass



1
完全同意。我曾经使用node-sass和laravel-mix,但现在node-sass不在必需依赖项列表中了。 - Vlad
1
最终,我一直因为在Windows和Alpine Linux构建容器中出现node-gyp构建错误而苦恼。直到现在,解决方案是在Windows上使用node14、python2和vs-2017构建工具。虽然有很多资源可以解决错误,但没有一个给出替换的提示。感谢你啊伙计! - Jürgen Steinblock

7

尝试运行这个命令,对我有效

sudo npm install -g --unsafe-perm node-sass

我目前无法验证,但希望能对某人有所帮助。 - Maxdola
这对我有效。在我的情况下,不需要 -g - Karthik Rao

3

经过两个多小时的尝试各种方法(清理npm缓存,删除node_modules,尝试使用全局开关-g安装等等)都无法成功安装node-sass,最后我终于将镜像源改成了中国,这样安装就立即成功了!(虽然考虑到我之前遇到的错误,这有点说不通)

仅供参考,在安装成功之前我一直收到以下错误提示:

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-sass@4.14.0 postinstall: `node scripts/build.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-sass@4.14.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\****\AppData\Roaming\npm-cache\_logs\2020-06-06T13_56_56_768Z-debug.log

我的Node版本:v12.18.0
package.json中node-sass的版本:"^4.14.0"

我按照node-sass npm页面中的说明,更改了镜像。

npm install -g mirror-config-china --registry=http://registry.npm.taobao.org
npm install node-sass

希望这有所帮助。

1

这里是替换 node-sasssass 的步骤,这解决了我的具体问题:

  1. 首先删除 'node-sass'。在您的 package.json 中移除它(从依赖项中删除)
  2. 然后,删除 package-lock.json 文件和 node_modules
  3. npm install
  4. npm install sass --save

'npm start' 将是您的下一步。


0
最好的选择可能是用 sass 库替换 node-sass 库。 但在我们的情况下,我通过安装 nvm 并将本地 node 版本设置为 14.17.6 来解决了这个问题。 我的 node-sass 版本是 4.14.1。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接