升级到 macos v10.15 之后,nodejs无法正常编译 c++ 库,node-gyp 文档给了解决方案,本篇是其的简短翻译。
当升级到 macOS Catalina 或者使用 Software Update 会造成 node-gyp 安装失败。一般在 npm install 的时候会遇到下面这个错误:
gyp: No Xcode or CLT version detected!
使用 node-gyp v7
最新版本的 node-gyp 或许可以解决这个错误。如果你直接使用使用 node-gyp 你应该安装使用 node-gyp v7。
如果你在 npm install 中使用 node-gyp(注:npm install 一些 C++ 扩展),你应该使用全局安装的 node-gyp :
npm config set node_gyp <path to node-gyp>或者- 使用环境变量配置:
npm_config_node_gyp=<path to node-gyp> npm install
注意,如果使用了这种方式,那么必须要注意更新 node-gyp 的版本。另外可以使用 npm config delete node_gyp 删除之前的配置。
使用老版本 node-gyp
当使用 v7 之前的老版本 node-gyp 可以按照下面流程进行。
确认是否在使用 macOS Catalina
$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.15
BuildVersion: 19A602
如果 ProductVersion 低于 10.15 那么不要继续进行下面的操作,而是使用 REAMDME.md 这里的指南进行。
确定是否安装 CLT (acid test)
可以使用下面脚本确定是否安装了 XCode Command Line Tools
curl -sL https://github.com/nodejs/node-gyp/raw/master/macOS_Catalina_acid_test.sh | bash
如果测试成功,那么可以在项目里里面重新尝试 npm install 。
如果测试失败,那么需要先进行安装 XCode Command Line Tools。
有三种方式进行安装:
- 从 App Store 安装 XCode(~7.6 GB)
- 安装成功后
xcodebuild应该打印XCode 11.1或更新- 如果不是,需要从下载最新的版本
- 打开 Xcode
- 选择 Preferences > Locations 如果
Command Line Tools选项为空,那么选择一
- 如果提示需要安装更多编译工具,那么允许其执行
- 选择 Preferences > Locations 如果
- 当全部安装结束后,退出 xcode
- 运行
sudo xcodebuild -license accept同意软件许可证协议(license term) - 运行
softwareupdate -l没有需要更新的软件- 如果存在更新的软件,那么使用 系统偏好设置 > 软件更新 进行更新
- 运行
xcode-select -version应该返回xcode-select version 2370或更新 - 运行
xcode-select -print-path应该返回/Applications/Xcode.app/Contents/Developer - 然后再次运行 acid test 是否安装
CLT的命令- 如果仍旧测试失败,使用
sudo xcode-select --reset没有任何输出是正常的,否则从第 7 步重新开始
- 如果仍旧测试失败,使用
- 安装成功后
- 使用
xcode-select --install命令最小安装- 如果 acid test 失败,那么重新尝试
xcode-select --install - 如果安装命令返回
xcode-select: error: command line tools are already installed, use "Software Update" to install updates需要尝试删除 XCL 后重装。 - 等待安装成功…
- 运行
softwareupdate -l没有需要更新的软件- 如果存在更新的软件,那么使用 系统偏好设置 > 软件更新 进行更新
- 运行
xcode-select -version应该返回xcode-select version 2370或更新 - 运行
xcode-select -print-path应该返回/Applications/Xcode.app/Contents/Developer - 重新进行 acid test
- 如果还没有成功,那么运行
sudo xcode-select --reset接着从第 1 步开始
- 如果 acid test 失败,那么重新尝试
- 手动从苹果开发者中心下载
- 登录 https://developer.apple.com/download/more 选择下载最新的 Command_Line_Tools 下载
- 安装,然后进行 acid test
卸载重装 XCL
- 运行
sudo rm -rf $(xcode-select -print-path)没有输出为正常 - 运行
sudo rm -rf /Library/Developer/CommandLineTools - 运行
sudo xcode-select --reset - 运行
xcode-select --install - 进行 acid test
- 如果仍旧没有测试成功
npm explore npm -g -- npm install node-gyp@latestnpm explore npm -g -- npm explore npm-lifecycle -- npm install node-gyp@latest
- 如果还没有测试成功,可以在 node-gyp github issue 中反馈
- 如果仍旧没有测试成功