You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

npm install --force执行失败,node-sass构建时node-gyp报错

npm install --force执行失败,node-sass构建时node-gyp报错

看起来你碰到的是典型的node-sass版本与Node.js不兼容,再加上node-gyp依赖的Python版本不匹配导致的构建失败问题,我帮你拆解问题并给出可行的解决办法:

错误原因分析

从你提供的报错日志里,核心问题有两个:

  1. Python语法不兼容:报错里的SyntaxError: Missing parentheses in call to 'print',是因为你当前使用的node-gyp@3.8.0是旧版本,仅支持Python 2.x,但你的系统里安装的是Python 3.x(Python 3要求print()必须加括号,Python 2不需要),所以执行构建脚本时触发语法错误。
  2. node-sass与Node.js版本不兼容:你使用的是Node.js v18.17.1,但node-sass已经停止维护,它的最高兼容版本仅到Node.js v16,所以在Node.js v18下必然会出现构建失败的问题。

解决方案(按推荐优先级排序)

方案1:替换node-sass为官方推荐的sass(最推荐)

node-sass已被官方废弃,现在官方推荐使用dart-sass(npm包名为sass),完全兼容Node.js v18,步骤如下:

# 先卸载旧的node-sass
npm uninstall node-sass

# 安装官方推荐的sass包
npm install sass --save-dev

# 重新执行依赖安装
npm install

这个方案可以彻底解决版本兼容问题,后续也不会再遇到类似的node-gyp构建错误。

方案2:临时指定node-gyp使用Python 2.7(不推荐,仅临时救急)

如果你暂时不想替换node-sass,可以强制让node-gyp使用Python 2.7来构建:

  1. 确保你的系统已经安装了Python 2.7
  2. 执行以下命令指定Python版本后再安装依赖:
# 临时指定本次安装使用Python 2.7
npm install --python=python2.7

# 或者全局配置node-gyp默认使用Python 2.7
npm config set python python2.7

但注意这个方法只是临时解决,后续升级Node.js版本还是会出现兼容问题,而且node-sass本身已经停止维护,不建议长期使用。

方案3:降级Node.js到v16版本

如果一定要继续使用node-sass,可以把Node.js版本降级到v16.x(node-sass支持的最高Node版本),你可以使用nvm(Node版本管理工具)来快速切换版本:

# 安装nvm后,安装Node.js v16
nvm install 16

# 切换到v16版本
nvm use 16

# 重新执行依赖安装
npm install

附你的报错日志

"node_modules\node-sass
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node scripts/build.js
npm ERR! Building: \node_modules\node-gyp\bin\node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp verb cli [enter code here
npm ERR! gyp verb cli 'C:\Program Files\nodejs\node.exe',
npm ERR! gyp verb cli '\node_modules\node-gyp\bin\node-gyp.js',
npm ERR! gyp verb cli 'rebuild',
npm ERR! gyp verb cli '--verbose',
npm ERR! gyp verb cli '--libsass_ext=',
npm ERR! gyp verb cli '--libsass_cflags=',
npm ERR! gyp verb cli '--libsass_ldflags=',
npm ERR! gyp verb cli '--libsass_library='
npm ERR! gyp verb cli ]
npm ERR! gyp info using node-gyp@3.8.0
npm ERR! gyp ERR! stack File "", 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:514:28)
npm ERR! gyp ERR! stack at maybeClose (node:internal/child_process:1091:16)
npm ERR! gyp ERR! stack at Socket. (node:internal/child_process:449:11)
npm ERR! gyp ERR! stack at Socket.emit (node:events:514:28)
npm ERR! gyp ERR! stack at Pipe. (node:net:323:12)
npm ERR! gyp ERR! System Windows_NT 10.0.19044
npm ERR! gyp ERR! node -v v18.17.1
npm ERR! gyp ERR! node-gyp -v v3.8.0
npm ERR! gyp ERR! not ok
npm ERR! Build failed with error code: 1"

备注:内容来源于stack exchange,提问作者Java

火山引擎 最新活动