在安装 node-sass 时出现的编译问题

swift 复制代码
npm ERR! gyp verb `which` failed     at FSReqCallback.oncomplete (node:fs:191:21) {
npm ERR! gyp verb `which` failed   code: 'ENOENT'
npm ERR! gyp verb `which` failed }
npm ERR! gyp verb check python checking for Python executable "python" in the PATH
npm ERR! gyp verb `which` succeeded python F:\Python\Python310\python.EXE
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Command failed: F:\Python\Python310\python.EXE -c import sys; print "%s.%s.%s" % sys.version_info[:3];
npm ERR! gyp ERR! stack   File "<string>", line 1
npm ERR! gyp ERR! stack     import sys; print "%s.%s.%s" % sys.version_info[:3];
npm ERR! gyp ERR! stack                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
npm ERR! gyp ERR! stack SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
npm ERR! gyp ERR! stack
npm ERR! gyp ERR! stack     at ChildProcess.exithandler (node:child_process:419:12)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:512:28)
npm ERR! gyp ERR! stack     at maybeClose (node:internal/child_process:1098:16)
npm ERR! gyp ERR! stack     at ChildProcess._handle.onexit (node:internal/child_process:304:5)
npm ERR! gyp ERR! System Windows_NT 10.0.19045
npm ERR! gyp ERR! command "F:\\nvm\\nodejs\\node.exe" "E:\\xinye\\work\\acm\\abTestDemo\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
npm ERR! gyp ERR! cwd E:\xinye\work\acm\abTestDemo\node_modules\node-sass
npm ERR! gyp ERR! node -v v19.6.0
npm ERR! gyp ERR! node-gyp -v v3.8.0
npm ERR! gyp ERR! not ok
npm ERR! Build failed with error code: 1

核心原因与 node-gyp 依赖的 Python 环境和 Node.js 版本兼容性有关。具体分析如下:

错误核心原因

  1. Python 版本与语法不兼容

    错误日志中明确提到:

    plaintext

    vbnet 复制代码
    SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?

    这是因为旧版本的 node-gyp(此处为 v3.8.0)使用的是 Python 2.x 的语法(print "xxx"),而你的系统中安装的是 Python 3.x(F:\Python\Python310\python.EXE)。Python 3 要求 print 必须带括号(print(...)),导致语法报错。

  2. Node.js 版本过高

    日志显示当前使用的 Node.js 版本是 v19.6.0,而 node-sass 对高版本 Node 支持有限(尤其是旧版本 node-sass 不兼容 Node 16+),这会进一步加剧编译失败。

  3. node-gyp 版本过旧

    项目中使用的 node-gyp@3.8.0 是较旧的版本,对现代 Node 和 Python 3 的支持不完善。

错误发生流程

  1. 安装 node-sass 时,需要通过 node-gyp 编译原生模块。
  2. node-gyp 首先查找 Python 环境,尝试了 python2(未找到),最终找到 python3.10
  3. 执行 Python 命令时,因 node-gyp 旧版本使用 Python 2 语法,与 Python 3 不兼容,导致语法错误。
  4. 编译过程中断,最终 node-sass 安装失败,返回错误码 1

总结关键问题点

  • node-sass 依赖的 node-gyp 版本过旧,不支持 Python 3 语法。

  • Node.js 版本(v19.6.0)过高,与 node-sass 兼容性差。

  • Python 环境为 3.x,与旧版 node-gyp 的 Python 2 语法冲突。

解决思路:统一 Node、node-sassnode-gyp 和 Python 的版本兼容性(推荐使用 Node 14.x + Python 2.7,或替换为 sass 避免编译问题)。

相关推荐
前端双越老师5 小时前
【干货】使用 langChian.js 实现掘金“智能总结” 考虑大文档和 token 限制
人工智能·langchain·node.js
一枚小小程序员哈15 小时前
基于Vue + Node能源采购系统的设计与实现/基于express的能源管理系统#node.js
vue.js·node.js·express
海上彼尚17 小时前
使用 npm-run-all2 简化你的 npm 脚本工作流
前端·npm·node.js
开发者小天17 小时前
为什么 /deep/ 现在不推荐使用?
前端·javascript·node.js
一枚小小程序员哈19 小时前
基于Vue的个人博客网站的设计与实现/基于node.js的博客系统的设计与实现#express框架、vscode
vue.js·node.js·express
刘永胜是我21 小时前
node版本切换
前端·node.js
我是哈哈hh1 天前
【Node.js】ECMAScript标准 以及 npm安装
开发语言·前端·javascript·node.js
HWL56791 天前
pnpm(Performant npm)的安装
前端·vue.js·npm·node.js
Sammyyyyy1 天前
2025年,Javascript后端应该用 Bun、Node.js 还是 Deno?
开发语言·javascript·node.js