npm ERR! code 1 npm ERR! path /Users/yinyuehe/yinyihao/MasterDataWorkSpace/chinatower-mdm-fe/node_modules/node-sass npm ERR! command failed npm ERR! command sh -c -- node scripts/build.js npm ERR! Building: /usr/local/bin/node /Users/yinyuehe/yinyihao/MasterDataWorkSpace/chinatower-mdm-fe/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 [ npm ERR! gyp verb cli '/usr/local/bin/node', npm ERR! gyp verb cli '/Users/yinyuehe/yinyihao/MasterDataWorkSpace/chinatower-mdm-fe/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 [email protected] npm ERR! gyp info using [email protected] | darwin | arm64 npm ERR! gyp verb command rebuild [] npm ERR! gyp verb command clean [] npm ERR! gyp verb clean removing "build" directory npm ERR! gyp verb command configure [] npm ERR! gyp verb check python checking for Python executable "/usr/bin/python3" in the PATH npm ERR! gyp verb `which` succeeded /usr/bin/python3 /usr/bin/python3 npm ERR! gyp ERR! configure error npm ERR! gyp ERR! stack Error: Command failed: /usr/bin/python3 -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: invalid syntax npm ERR! gyp ERR! stack npm ERR! gyp ERR! stack at ChildProcess.exithandler (node:child_process:402:12) npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:513:28) npm ERR! gyp ERR! stack at maybeClose (node:internal/child_process:1100:16)
时间: 2025-05-14 15:57:32 浏览: 46
### 解决方案
在 macOS ARM64 架构上安装 `node-sass` 并遇到 `node-gyp` 的 Python 版本冲突问题时,可以采取以下方法来解决问题。
#### 1. 设置正确的 Python 路径
如果系统中有多个 Python 版本,则可以通过设置 `npm` 配置中的 `python` 键来指定特定的 Python 版本。例如,假设已安装 Python 2.7 或兼容版本,可运行以下命令[^1]:
```bash
npm config set python /usr/bin/python2.7
```
#### 2. 更新 Node.js 和 npm 到最新稳定版
确保使用的 Node.js 和 npm 是最新的稳定版本,因为旧版本可能无法支持某些依赖项或存在兼容性问题。更新命令如下:
```bash
nvm install --lts
nvm use --lts
npm install -g npm@latest
```
#### 3. 使用 Rosetta 运行 x86_64 环境下的工具链
由于部分原生模块尚未完全适配 Apple Silicon (ARM64),可以在终端中启用 Rosetta 来模拟 Intel 架构环境。具体操作包括重新安装 Homebrew 和相关依赖项。以下是步骤概述:
- 卸载现有 Homebrew(如果有)并重新通过 Rosetta 安装。
- 执行以下命令以安装适用于 Intel 架构的依赖项:
```bash
arch -x86_64 brew install python@2
```
随后,在安装过程中强制使用该路径作为 Python 可执行文件的位置:
```bash
arch -x86_64 npx node-gyp rebuild --python=$(which python2)
```
#### 4. 替代方式:切换到 Dart-SASS
考虑到长期维护性和跨平台一致性,推荐考虑迁移到纯 JavaScript 实现的替代品——Dart-SASS。它无需编译过程即可正常工作,从而规避了与本地构建工具相关的复杂配置需求。安装指令如下所示:
```bash
npm uninstall node-sass && npm install sass
```
#### 5. 全局安装 Windows Build Tools (仅限于类比参考)
尽管当前问题是针对 macOS ARM64 设备提出的,但如果是在 Windows 上遇到了类似的错误消息,也可以尝试全局安装用于处理 C++ 插件绑定所需的开发套件[^3]。不过此选项并不适合 Mac 用户直接应用。
---
### 总结
综上所述,当面对由不同 Python 版本引发的 `node-gyp` 编译失败情况时,应当优先调整所选用的具体解释器实例;与此同时也要注意基础软件栈的整体升级状态以及目标硬件架构特性带来的额外考量因素。
阅读全文