node-sass 安装失败 Command failed 报错

lxf2023-05-22 01:57:30

通过yarn install安装依赖包node-sass时总是报错,具体的错误原因为Command failed。应该如何解决这个问题呢?

报错信息

D:\testspace\test\node_modules\node-sass: Command failed.
Exit code: 1
Command: node scripts/build.js
Arguments:
Directory: D:\testspace\test\node_modules\node-sass
Output:
Building: C:\Program Files\nodejs\node.exe D:\testspace\test\node_modules\node-gyp\bin\node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=
gyp info it worked if it ends with ok
gyp verb cli [
gyp verb cli   'C:\\Program Files\\nodejs\\node.exe',
gyp verb cli   'D:\\testspace\\test\\node_modules\\node-gyp\\bin\\node-gyp.js',
gyp verb cli   'rebuild',
gyp verb cli   '--verbose',
gyp verb cli   '--libsass_ext=',
gyp verb cli   '--libsass_cflags=',
gyp verb cli   '--libsass_ldflags=',
gyp verb cli   '--libsass_library='
gyp verb cli ]
gyp info using node-gyp@3.8.0
gyp info using node@16.14.2 | win32 | x64
gyp verb command rebuild []
gyp verb command clean []
gyp verb clean removing "build" directory
gyp verb command configure []
gyp verb download using dist-url https://npm.taobao.org/dist
gyp verb check python checking for Python executable "python2" in the PATH
gyp verb `which` failed Error: not found: python2
gyp verb `which` failed     at getNotFoundError (D:\testspace\test\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (D:\testspace\test\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (D:\testspace\test\node_modules\which\which.js:80:29)
gyp verb `which` failed     at D:\testspace\test\node_modules\which\which.js:89:16
gyp verb `which` failed     at D:\testspace\test\node_modules\isexe\index.js:42:5
gyp verb `which` failed     at D:\testspace\test\node_modules\isexe\windows.js:36:5
gyp verb `which` failed     at FSReqCallback.oncomplete (node:fs:198:21)
gyp verb `which` failed  python2 Error: not found: python2
gyp verb `which` failed     at getNotFoundError (D:\testspace\test\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (D:\testspace\test\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (D:\testspace\test\node_modules\which\which.js:80:29)
gyp verb `which` failed     at D:\testspace\test\node_modules\which\which.js:89:16
gyp verb `which` failed     at D:\testspace\test\node_modules\isexe\index.js:42:5
gyp verb `which` failed     at D:\testspace\test\node_modules\isexe\windows.js:36:5
gyp verb `which` failed     at FSReqCallback.oncomplete (node:fs:198:21) {
gyp verb `which` failed   code: 'ENOENT'
gyp verb `which` failed }
gyp verb check python checking for Python executable "python" in the PATH
gyp verb `which` succeeded python C:\Users\PC\AppData\Local\Programs\Python\Python36\python.EXE
gyp ERR! configure error
gyp ERR! stack Error: Command failed: C:\Users\PC\AppData\Local\Programs\Python\Python36\python.EXE -c import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack   File "<string>", line 1
gyp ERR! stack     import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack                                ^
gyp ERR! stack SyntaxError: invalid syntax
gyp ERR! stack
gyp ERR! stack     at ChildProcess.exithandler (node:child_process:399:12)
gyp ERR! stack     at ChildProcess.emit (node:events:526:28)
gyp ERR! stack     at maybeClose (node:internal/child_process:1092:16)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:302:5)
gyp ERR! System Windows_NT 10.0.22621
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "D:\\testspace\\test\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd D:\testspace\test\node_modules\node-sass
gyp ERR! node -v v16.14.2
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
Build failed with error code: 1
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

错误原因

我的当前环境是node v16.14.2,我安装的版本node-sass v4.14.1。报错的根本原因是nodenode-sass的版本不匹配。

指引

以下是node-sass支持最低到最高版本的快速指引:

NodeJSSupported node-sass versionNode Module
Node 198.0+111
Node 188.0+108
Node 177.0+, <8.0102
Node 166.0+93
Node 155.0+, <7.088
Node 144.14+83
Node 134.13+, <5.079
Node 124.12+, <8.072
Node 114.10+, <5.067
Node 104.9+, <6.064
Node 84.5.3+, <5.057
Node <8<5.0<57

其他问题

node-sass 安装慢,安装不下来

注意: 安装慢,安装不下来和本文中提到的版本不匹配是不同的问题,不可混淆

npm install -g mirror-config-china --registry=https://registry.npmmirror.com
npm install node-sass

或者

npm config set sass_binary_site=https://registry.npmmirror.com/binary.html?path=node-sass
npm install node-sass

yarn同理。(#^.^#)我在更新这部分内容的时候,用的是npm

Vue Loader - Scoped CSS

Vue Loader - Scoped CSS

深度作用选择器:

操作符csssass (Dart Sass)node-sassless
>>>支持---
::v-deep-支持支持支持
/deep/--支持-

结论:

  • 深度作用选择器在css中使用>>>,在sass/less之类的预处理器中使用::v-deep。不使用/deep/
  • 使用sass预处理器时,建议用sass代替node-sass

参考

  • node-sass - npm
  • node-sass - npmmirror
  • sass-loader - webpack

欢迎关注我的微信公众号:乘风破浪的Coder

本网站是一个以CSS、JavaScript、Vue、HTML为核心的前端开发技术网站。我们致力于为广大前端开发者提供专业、全面、实用的前端开发知识和技术支持。 在本网站中,您可以学习到最新的前端开发技术,了解前端开发的最新趋势和最佳实践。我们提供丰富的教程和案例,让您可以快速掌握前端开发的核心技术和流程。 本网站还提供一系列实用的工具和插件,帮助您更加高效地进行前端开发工作。我们提供的工具和插件都经过精心设计和优化,可以帮助您节省时间和精力,提升开发效率。 除此之外,本网站还拥有一个活跃的社区,您可以在社区中与其他前端开发者交流技术、分享经验、解决问题。我们相信,社区的力量可以帮助您更好地成长和进步。 在本网站中,您可以找到您需要的一切前端开发资源,让您成为一名更加优秀的前端开发者。欢迎您加入我们的大家庭,一起探索前端开发的无限可能!