个人项目安装node-sass报错求助:yarn安装node-sass失败问题(已解决)
求助后解决:yarn add node-sass 安装触发node-gyp SyntaxError
我今天尝试给个人项目执行yarn add node-sass来安装node-sass,结果在构建依赖的阶段直接报错了,折腾了一阵终于解决,把问题和方案分享出来:
完整报错信息
[2/4] 🚚 Fetching packages... [3/4] 🔗 Linking dependencies... [4/4] 🔨 Building fresh packages... [-/7] ⠄ waiting... [-/7] ⠄ waiting... [7/7] ⠄ node-sass [6/7] ⠄ pngcrush-bin error /Users/user/Desktop/project/node_modules/gulp-sass/node_modules/node-sass: Command failed. Exit code: 1 Command: node scripts/build.js Arguments: Directory: /Users/user/Desktop/project/node_modules/gulp-sass/node_modules/node-sass Output: Building: /usr/local/Cellar/node@16/16.13.0/libexec/node /Users/user/Desktop/project/node_modules/gulp-sass/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 '/usr/local/Cellar/node@16/16.13.0/libexec/node', gyp verb cli '/Users/user/Desktop/project/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 using node-gyp@3.8.0 gyp info using node@16.13.0 | darwin | x64 gyp verb command rebuild [] gyp verb command clean [] gyp verb clean removing "build" directory gyp verb command configure [] gyp verb check python checking for Python executable "/usr/local/opt/python@3.9/bin/python3" in the PATH gyp verb `which` succeeded /usr/local/opt/python@3.9/bin/python3 /usr/local/opt/python@3.9/bin/python3 gyp ERR! configure error gyp ERR! stack Error: Command failed: /usr/local/opt/python@3.9/bin/python3 -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:397:12) gyp ERR! stack at ChildProcess.emit (node:events:390:28) gyp ERR! stack at maybeClose (node:internal/child_process:1064:16) gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5) gyp ERR! System Darwin 21.1.0 gyp ERR! command "/usr/local/Cellar/node@16/16.13.0/libexec/node" "/Users/user/Desktop/project/node_modules/gulp-sass/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library=" gyp ERR! cwd /Users/user/Desktop/project/node_modules/gulp-sass/node_modules/node-sass gyp ERR! node -v v16.13.0
问题根因
仔细看报错日志,能定位到两个核心问题:
- Python版本不兼容:项目依赖的
node-gyp@3.8.0是比较老的版本,它用的是Python2的print语法(没有括号),但我本地环境是Python3.9,直接触发了语法错误。 - Node.js与node-sass版本不匹配:我用的是Node.js 16.13.0,而通过gulp-sass间接依赖的node-sass版本太旧,不支持Node.js 16的API,这也是构建失败的重要原因。
解决方案(亲测有效)
我最终用了替换为dart-sass的方案,彻底解决了依赖构建的问题,推荐大家优先尝试:
方案1:替换node-sass为dart-sass(官方推荐)
node-sass已经停止维护了,官方现在推荐使用dart-sass(npm包名为sass),它不需要node-gyp编译,兼容性拉满:
# 先卸载旧的相关依赖 yarn remove node-sass gulp-sass # 安装最新版gulp-sass(默认依赖dart-sass) yarn add gulp-sass --dev
方案2:强制匹配Node.js与node-sass版本(适合必须用node-sass的场景)
如果一定要保留node-sass,得确保版本和Node.js 16兼容:
- Node.js 16对应的node-sass最低版本是
6.0.0
执行以下命令安装指定版本:
yarn add node-sass@^6.0.0 --dev
同时解决Python问题:
可以安装Python2.7并配置node-gyp使用它:
npm config set python /usr/bin/python2.7
或者升级全局node-gyp到支持Python3的版本:
npm install -g node-gyp@latest
方案3:强制升级间接依赖的node-gyp
如果是gulp-sass间接依赖的node-gyp版本太低,可以在package.json里添加resolutions强制升级:
"resolutions": { "node-gyp": "^9.0.0" }
然后重新执行yarn install即可。
内容的提问来源于stack exchange,提问作者agoumi
相关产品推荐
相关产品推荐

