Yarn构建node-sass失败求助,含错误日志及相关安装异常
Foundation-Sites Yarn安装失败问题解决
问题背景
按照foundation-cli推荐使用Yarn修复foundation-sites安装,两次尝试均失败——即使删除yarn.lock并执行npm install -g node-sass后,仍遇到几乎相同的错误。此外,执行npm install --global windows-build-tools时,安装到末尾出现冻结现象。
核心错误分析
从Yarn错误日志来看,问题出在node-sass构建阶段的node-gyp报错:
gyp ERR! stack Error: Command failed: C:\Users\epicd\AppData\Local\Programs\Python\Python38-32\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
原因是当前项目依赖的node-gyp@3.8.0仅支持Python2语法,而系统中安装的是Python3.8,Python3的print是函数,需要加括号,导致语法错误。
解决方案
方案1:配置node-gyp使用Python2.7
- 下载并安装Python2.7(选择与系统匹配的位数,如x64)
- 告诉node-gyp使用Python2.7:
npm config set python python2.7 - 清理现有依赖后重新安装:
rm -rf node_modules yarn.lock yarn install
方案2:升级node-gyp到支持Python3的版本
- 全局升级node-gyp:
npm install -g node-gyp@latest - 在项目的
package.json中添加Yarn resolutions,强制依赖最新版node-gyp:"resolutions": { "node-gyp": "^9.0.0" } - 清理依赖重新安装:
rm -rf node_modules yarn.lock yarn install
方案3:替换node-sass为dart-sass(推荐)
node-sass已停止维护,改用dart-sass更稳定且无需复杂编译:
- 卸载旧依赖:
yarn remove gulp-sass node-sass - 安装支持dart-sass的最新版gulp-sass:
yarn add gulp-sass@latest --dev - 重新执行安装:
yarn install
完整错误日志(中文翻译)
D:\Documents\field-benders-webpage>yarn yarn install v1.22.19 info 未找到lockfile。 [1/4] 解析包... 警告 gulp > glob-watcher > chokidar@2.1.8: Chokidar 2自2019年起不再接收安全更新。升级到chokidar 3,依赖减少15倍 警告 gulp > glob-watcher > chokidar > fsevents@1.2.13: fsevents 1在node v14+环境下会失效,且可能使用不安全的二进制文件。升级到fsevents 2。 警告 gulp > glob-watcher > anymatch > micromatch > snapdragon > source-map-resolve@0.5.3: 该包已废弃 警告 gulp > glob-watcher > anymatch > micromatch > snapdragon > source-map-resolve > source-map-url@0.4.1: 该包已废弃 警告 gulp > glob-watcher > anymatch > micromatch > snapdragon > source-map-resolve > resolve-url@0.2.1: 该包已废弃 警告 gulp > glob-watcher > anymatch > micromatch > snapdragon > source-map-resolve > urix@0.1.0: 该包已废弃 警告 gulp-imagemin > imagemin-svgo > svgo@1.3.2: 此SVGO版本不再受支持。升级到v2.x.x。 警告 gulp-imagemin > imagemin-svgo > svgo > stable@0.1.8: 现代JS已保证Array#sort()是稳定排序,因此该库已废弃。查看MDN兼容性表了解详情。 警告 gulp-imagemin > imagemin-gifsicle > gifsicle > bin-build > tempfile > uuid@3.4.0: 请升级到版本7或更高。旧版本在某些情况下可能使用Math.random(),这存在已知问题。 警告 gulp-sass > node-sass > request@2.88.2: request已废弃 警告 gulp-sass > node-sass > node-gyp > request@2.88.2: request已废弃 警告 gulp-sass > node-sass > request > uuid@3.4.0: 请升级到版本7或更高。旧版本在某些情况下可能使用Math.random(),这存在已知问题。 警告 gulp-sass > node-sass > node-gyp > tar@2.2.2: 此版本tar不再受支持,也不会接收安全更新。请尽快升级。 警告 gulp-sass > node-sass > request > har-validator@5.1.5: 该库不再受支持 警告 gulp-sourcemaps > css > source-map-resolve@0.6.0: 该包已废弃 警告 node-sass > request@2.88.2: request已废弃 警告 panini > highlight.js@8.9.1: 版本不再受支持。升级到最新版 警告 style-sherpa > highlight.js@8.9.1: 版本不再受支持。升级到最新版 警告 uncss > request@2.88.2: request已废弃 警告 uncss > jsdom > request@2.88.2: request已废弃 警告 uncss > jsdom > request-promise-native@1.0.9: request-promise-native已废弃,因为它基于已废弃的request包 [2/4] 获取包... [3/4] 链接依赖... 警告 " > foundation-sites@6.7.5" 存在不正确的对等依赖 "motion-ui@latest"。 [4/4] 构建新包... [6/6] ⠂ node-sass [-/6] ⠂ 等待... [-/6] ⠂ 等待... [-/6] ⠂ 等待... 错误 D:\Documents\field-benders-webpage\node_modules\gulp-sass\node_modules\node-sass: 命令失败。 退出码: 1 命令: node scripts/build.js 参数: 目录: D:\Documents\field-benders-webpage\node_modules\gulp-sass\node_modules\node-sass 输出: 构建中: C:\Program Files\nodejs\node.exe D:\Documents\field-benders-webpage\node_modules\gulp-sass\node_modules\node-gyp\bin\node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library= gyp info 执行成功会以ok结尾 gyp verb cli [ gyp verb cli 'C:\\Program Files\\nodejs\\node.exe', gyp verb cli 'D:\\Documents\\field-benders-webpage\\node_modules\\gulp-sass\\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 使用 node-gyp@3.8.0 gyp info 使用 node@16.14.2 | win32 | x64 gyp verb 命令 rebuild [] gyp verb 命令 clean [] gyp verb clean 移除 "build" 目录 gyp verb 命令 configure [] gyp verb 检查python 在PATH中查找Python可执行文件 "python2" gyp verb `which` 失败 Error: 未找到: python2 gyp verb `which` 失败 在 getNotFoundError (D:\Documents\field-benders-webpage\node_modules\which\which.js:13:12) gyp verb `which` 失败 在 F (D:\Documents\field-benders-webpage\node_modules\which\which.js:68:19) gyp verb `which` 失败 在 E (D:\Documents\field-benders-webpage\node_modules\which\which.js:80:29) gyp verb `which` 失败 在 D:\Documents\field-benders-webpage\node_modules\which\which.js:89:16 gyp verb `which` 失败 在 D:\Documents\field-benders-webpage\node_modules\isexe\index.js:42:5 gyp verb `which` 失败 在 D:\Documents\field-benders-webpage\node_modules\isexe\windows.js:36:5 gyp verb `which` 失败 在 FSReqCallback.oncomplete (node:fs:198:21) gyp verb `which` 失败 python2 Error: 未找到: python2 gyp verb `which` 失败 在 getNotFoundError (D:\Documents\field-benders-webpage\node_modules\which\which.js:13:12) gyp verb `which` 失败 在 F (D:\Documents\field-benders-webpage\node_modules\which\which.js:68:19) gyp verb `which` 失败 在 E (D:\Documents\field-benders-webpage\node_modules\which\which.js:80:29) gyp verb `which` 失败 在 D:\Documents\field-benders-webpage\node_modules\which\which.js:89:16 gyp verb `which` 失败 在 D:\Documents\field-benders-webpage\node_modules\isexe\index.js:42:5 gyp verb `which` 失败 在 D:\Documents\field-benders-webpage\node_modules\isexe\windows.js:36:5 gyp verb `which` 失败 在 FSReqCallback.oncomplete (node:fs:198:21) { gyp verb `which` 失败 code: 'ENOENT' gyp verb `which` 失败 } gyp verb 检查python 在PATH中查找Python可执行文件 "python" gyp verb `which` 成功找到 python C:\Users\epicd\AppData\Local\Programs\Python\Python38-32\python.EXE gyp ERR! configure 错误 gyp ERR! stack Error: 命令失败: C:\Users\epicd\AppData\Local\Programs\Python\Python38-32\python.EXE -c import sys; print "%s.%s.%s" % sys.version_info[:3]; gyp ERR! stack 文件 "<string>", 第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 在 ChildProcess.exithandler (node:child_process:399:12) gyp ERR! stack 在 ChildProcess.emit (node:events:526:28) gyp ERR! stack 在 maybeClose (node:internal/child_process:1092:16) gyp ERR! stack 在 Process.ChildProcess._handle.onexit (node:internal/child_process:302:5) gyp ERR! 系统 Windows_NT 10.0.19044 gyp ERR! 命令 "C:\\Program Files\\nodejs\\node.exe" "D:\\Documents\\field-benders-webpage\\node_modules\\gulp-sass\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library=" gyp ERR! 工作目录 D:\Documents\field-benders-webpage\node_modules\gulp-sass\node_modules\node-sass gyp ERR! node -v v16.14.2 gyp ERR! node-gyp -v v3.8.0
内容的提问来源于stack exchange,提问作者Zachery Uporsky
相关产品推荐
相关产品推荐

