在mac下使用npm安装依赖包时,经常会报如下错误:

node-gyp rebuild

No receipt for 'com.apple.pkg.CLTools_Executables' found at '/'.

No receipt for 'com.apple.pkg.DeveloperToolsCLILeo' found at '/'.

No receipt for 'com.apple.pkg.DeveloperToolsCLI' found at '/'.

gyp: No Xcode or CLT version detected!
gyp ERR! configure error 
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/Users/sobird/.nvm/versions/node/v8.12.0/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:345:16)
gyp ERR! stack     at emitTwo (events.js:126:13)
gyp ERR! stack     at ChildProcess.emit (events.js:214:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)
gyp ERR! System Darwin 19.3.0
gyp ERR! command "/Users/sobird/.nvm/versions/node/v8.12.0/bin/node" "/Users/sobird/.nvm/versions/node/v8.12.0/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/sobird/Meituan/Oasis_FE/node_modules/fsevents
gyp ERR! node -v v8.12.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok

个人经验,发现是每次升级Xcode后,都会报这个错误,为避免再次出现该错误,本文记录下执行操作

终端执行下面命令

xcode-select --install
xcode-select: error: command line tools are already installed, use "Software Update" to install updates

如果出现以上结果,说明xcode-select --install命令不好使,放大招,执行下面的命令:

sudo rm -rf $(xcode-select -print-path)
xcode-select --install
等待完成

可能执行的时间比较长,耐心等待即可。

另外,如果mac卸载了Xcode,在使用git、make等命令时则会报如下错误:

xcrun: error: active developer path ("/Applications/Xcode.app/Contents/Developer") does not exist
Use `sudo xcode-select --switch path/to/Xcode.app` to specify the Xcode that you wish to use for command line developer tools, or use `xcode-select --install` to install the standalone command line developer tools.
See `man xcode-select` for more details.

这种情况可以通过sudo xcode-select --switch path/to/Xcode.app命令指定一个Xcode安装路径,如果没有安装Xcode,那么可以通过重置系统默认开发者工具路径。

执行如下命令查看xcode-select命令用法:

xcode-select -h
Usage: xcode-select [options]

Print or change the path to the active developer directory. This directory
controls which tools are used for the Xcode command line tools (for example, 
xcodebuild) as well as the BSD development commands (such as cc and make).

Options:
  -h, --help                  print this help message and exit
  -p, --print-path            print the path of the active developer directory
  -s <path>, --switch <path>  set the path for the active developer directory
  --install                   open a dialog for installation of the command line developer tools
  -v, --version               print the xcode-select version
  -r, --reset                 reset to the default command line tools path
// 切换 CLT 目录到 /Library/Developer/CommandLineTools
sudo xcode-select --switch /Library/Developer/CommandLineTools