Mac M1环境下执行NPM命令报错gyp退出码1,无法卸载node-sass
解决Mac M1上NPM命令因gyp报错
Undefined variable standalone_static_library的问题 问题背景
- 执行任何NPM命令均失败,报错核心信息:
加载binding.gyp时提示Undefined variable standalone_static_library,gyp配置失败,
gypfailed with exit code: 1 - 此前为解决
ng build的Python2缺失问题,通过pyenv安装了Python2.7.18并配置了PATH:pyenv install 2.7.18 echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile export PATH="${HOME}/.pyenv/shims:${PATH}"
解决步骤
1. 临时绕过gyp直接卸载node-sass
所有NPM命令触发gyp编译导致失败,先通过--ignore-scripts跳过脚本执行,强制卸载node-sass:
npm uninstall node-sass --ignore-scripts
2. 修复Python环境与node-gyp的兼容性
- 确认当前Python版本指向pyenv安装的2.7.18:
若版本不符,切换全局Python版本并重新加载环境:python --versionpyenv global 2.7.18 source ~/.bash_profile - 升级node-gyp到兼容M1架构的最新版本:
npm install -g node-gyp@latest - 配置NPM使用正确的Python路径:
npm config set python $(pyenv which python)
3. 替换node-sass为官方sass(推荐长期方案)
node-sass已停止维护,对M1架构兼容性差,直接替换为官方维护的sass包:
# 确保卸载node-sass npm uninstall node-sass --ignore-scripts # 安装sass npm install sass
之后修改项目配置(如angular.json的styles编译选项),将node-sass替换为sass。
4. 清理缓存并重新安装依赖
# 清理NPM缓存 npm cache clean --force # 删除旧依赖文件 rm -rf node_modules package-lock.json # 重新安装依赖 npm install
内容的提问来源于stack exchange,提问作者kajal ojha
相关产品推荐
相关产品推荐

