You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用nvm升级至node@16后Yarn安装包报错求助

Node16升级后Yarn安装node-sass报错问题

当前环境信息

  • Node版本:v16.20.2
  • npm版本:8.19.4
  • Python版本:3.11.7
  • Yarn版本:1.22.11

问题背景

把原本运行在Node12环境的旧项目升级到Node16最新版,用nvm管理Node版本。已经试过删除所有依赖模块、清理Yarn缓存,在.bash_profile和.zshrc里配置Python路径并验证正确性,同时给npm和Yarn指定了Python路径,但安装依赖时还是报错。其他包的兼容问题已经解决,只剩node-sass相关的报错没搞定。

报错信息

error /Users/xxx/xxx/xxx/xxx/core/node_modules/node-sass: Command failed.
Exit code: 1
Command: node scripts/build.js
Arguments: 
Directory: /Users/xxx/xxx/xxx/xxx/core/node_modules/node-sass
Output:
Building: /Users/xxx/.nvm/versions/node/v16.20.2/bin/node /Users/xxx/xxx/xxx/xxx/core/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   '/Users/xxx/.nvm/versions/node/v16.20.2/bin/node',
gyp verb cli   '/Users/xxx/xxx/xxx/xxx/core/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.20.2 | darwin | arm64
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/bin/python" in the PATH
gyp verb `which` succeeded /usr/local/bin/python /usr/local/bin/python
gyp ERR! configure error 
gyp ERR! stack Error: Command failed: /usr/local/bin/python -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: Missing parentheses in call to 'print'. Did you mean print(...)?
gyp ERR! stack 
gyp ERR! stack     at ChildProcess.exithandler (node:child_process:402:12)
gyp ERR! stack     at ChildProcess.emit (node:events:513:28)
gyp ERR! stack     at maybeClose (node:internal/child_process:1100:16)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:304:5)
gyp ERR! System Darwin 21.6.0
gyp ERR! command "/Users/xxx/.nvm/versions/node/v16.20.2/bin/node" "/Users/xxx/xxx/xxx/xxx/core/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd /Users/xxx/xxx/xxx/xxx/core/node_modules/node-sass
gyp ERR! node -v v16.20.2

解决方案

1. 升级node-sass至兼容Node16的版本

node-sass和Node版本有严格对应关系,Node16需要用node-sass@6.x及以上版本。执行以下命令升级:

yarn upgrade node-sass@^6.0.0

如果package.json里锁定了版本,也可以直接修改版本号后重新执行yarn install。

2. 替换node-sass为dart-sass(推荐)

node-sass已经停止维护,dart-sass是官方推荐的替代方案,不需要依赖node-gyp编译,兼容性更强:

yarn remove node-sass
yarn add sass

之后要把项目里所有引用node-sass的配置(比如webpack配置、vue.config.js等)换成sass。

3. 临时指定Python2供node-gyp使用(不推荐)

如果暂时没法换依赖包,可以让旧版node-gyp调用Python2(macOS一般自带Python2,路径是/usr/bin/python2):

  • 给当前项目指定Python路径:
npm config set python /usr/bin/python2 --prefix ./
yarn config set python /usr/bin/python2
  • 或者临时设置环境变量后安装:
PYTHON=/usr/bin/python2 yarn install

内容的提问来源于stack exchange,提问作者Lervad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 09:53:22