node-libcurl installation fails on MacOS Catalina: clang: error: no such file or directory: '/usr/include'

node-libcurl installation fails on MacOS Catalina: clang: error: no such file or directory: '/usr/include'

我只是想安装 insomnia-inso on a Mac (Catalina) which has a dependency to node-libcurl 并收到以下错误:

npm install -g node-libcurl

> node-libcurl@2.2.0 install /Users/jonashecht/dev/spring-boot/spring-boot-openapi-kong/node_modules/node-libcurl
> node-pre-gyp install --fallback-to-build

node-pre-gyp WARN Using request for node-pre-gyp https download
  CXX(target) Release/obj.target/node_libcurl/src/node_libcurl.o
clang: error: no such file or directory: '/usr/include'
make: *** [Release/obj.target/node_libcurl/src/node_libcurl.o] Error 1
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:194:23)
gyp ERR! stack     at ChildProcess.emit (node:events:327:20)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:277:12)
gyp ERR! System Darwin 19.6.0
gyp ERR! command "/usr/local/Cellar/node/15.1.0/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "build" "--fallback-to-build" "--module=/Users/jonashecht/dev/spring-boot/spring-boot-openapi-kong/node_modules/node-libcurl/lib/binding/node_libcurl.node" "--module_name=node_libcurl" "--module_path=/Users/jonashecht/dev/spring-boot/spring-boot-openapi-kong/node_modules/node-libcurl/lib/binding" "--napi_version=7" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v88"
gyp ERR! cwd /Users/jonashecht/dev/spring-boot/spring-boot-openapi-kong/node_modules/node-libcurl
gyp ERR! node -v v15.1.0
gyp ERR! node-gyp -v v5.1.0
gyp ERR! not ok
node-pre-gyp ERR! build error
node-pre-gyp ERR! stack Error: Failed to execute '/usr/local/Cellar/node/15.1.0/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js build --fallback-to-build --module=/Users/jonashecht/dev/spring-boot/spring-boot-openapi-kong/node_modules/node-libcurl/lib/binding/node_libcurl.node --module_name=node_libcurl --module_path=/Users/jonashecht/dev/spring-boot/spring-boot-openapi-kong/node_modules/node-libcurl/lib/binding --napi_version=7 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v88' (1)
node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (/Users/jonashecht/dev/spring-boot/spring-boot-openapi-kong/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
node-pre-gyp ERR! stack     at ChildProcess.emit (node:events:327:20)
node-pre-gyp ERR! stack     at maybeClose (node:internal/child_process:1055:16)
node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:288:5)
node-pre-gyp ERR! System Darwin 19.6.0
node-pre-gyp ERR! command "/usr/local/Cellar/node/15.1.0/bin/node" "/Users/jonashecht/dev/spring-boot/spring-boot-openapi-kong/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! cwd /Users/jonashecht/dev/spring-boot/spring-boot-openapi-kong/node_modules/node-libcurl
node-pre-gyp ERR! node -v v15.1.0
node-pre-gyp ERR! node-pre-gyp -v v0.15.0
node-pre-gyp ERR! not ok
Failed to execute '/usr/local/Cellar/node/15.1.0/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js build --fallback-to-build --module=/Users/jonashecht/dev/spring-boot/spring-boot-openapi-kong/node_modules/node-libcurl/lib/binding/node_libcurl.node --module_name=node_libcurl --module_path=/Users/jonashecht/dev/spring-boot/spring-boot-openapi-kong/node_modules/node-libcurl/lib/binding --napi_version=7 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v88' (1)

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-libcurl@2.2.0 install: `node-pre-gyp install --fallback-to-build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-libcurl@2.2.0 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/jonashecht/.npm/_logs/2020-11-05T08_43_46_342Z-debug.log

我使用 brew install node 通过自制程序安装了 node。版本是:

node --version
v15.1.0
npm --version
6.14.8

问题出在最新版本的 MacOS 上 Xcode 命令行工具的较新版本。

日志中的关键部分是:

  CXX(target) Release/obj.target/node_libcurl/src/node_libcurl.o
clang: error: no such file or directory: '/usr/include'
make: *** [Release/obj.target/node_libcurl/src/node_libcurl.o] Error 1

作为 node-libcurl docs state:

newer versions of the Command Line Tools does not add the /usr/include folder by default. Check Xcode 10 release notes for details.

要解决此问题,我们需要通过设置 npm 环境变量 npm_config_curl_include_dirs 来告知 npm install /usr/include 的新位置 $(xcrun --show-sdk-path)/usr/include

npm_config_curl_include_dirs="$(xcrun --show-sdk-path)/usr/include" npm install -g node-libcurl

在我的例子中,我想首先安装另一个 npm 库,例如 insomnia-inso,您需要将两个 npm 库添加到命令中才能成功安装:

npm_config_curl_include_dirs="$(xcrun --show-sdk-path)/usr/include" npm install -g node-libcurl insomnia-inso

现在 insomnia-inso 的安装非常有效。