npm install で node-gyp エラーが発生したときの解決方法

Tadashi Shigeoka ·  Sat, October 27, 2018

npm でインストールしていた iconv のバージョンを update しようとしたら node-gyp rebuild というエラーメッセージが表示されて、インストールできませんでした。

何度もこのエラーと戦ってきたので、解決方法をまとめてみました。(最終更新日:2020年4月24日)

npm

npm install でエラー

$ npm install
...
npm ERR! Darwin 17.7.0
npm ERR! argv "/Users/username/.nvm/versions/v6.11.0/bin/node" "/Users/username/works/myapp/node_modules/.bin/npm" "install"
npm ERR! node v6.11.0
npm ERR! npm  v3.9.2
npm ERR! code ELIFECYCLE

npm ERR! [email protected] install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] install script 'node-gyp rebuild'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the iconv package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs iconv
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls iconv
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/username/works/myapp/npm-debug.log

node-gyp rebuild しても、これでは解決できませんでした。

(解決方法 1) npm cache clean

npm cache clean
npm install

npm install 後に iconv がインストールされたことを確認して作業完了です。

$ npm ls | grep iconv@        
├─┬ [email protected]

(解決方法 2) Command Line tools 再インストール

以下のような gyp: No Xcode or CLT version detected! エラーが発生したときの対処方法です。

$ npm install

> [email protected] install /Users/youruser/example/public/node_modules/fsevents
> 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/youruser/.nvm/versions/node/v12.16.2/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:351:16)
gyp ERR! stack     at ChildProcess.emit (events.js:310:20)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
gyp ERR! System Darwin 19.4.0
gyp ERR! command "/Users/youruser/.nvm/versions/node/v12.16.2/bin/node" "/Users/youruser/.nvm/versions/node/v12.16.2/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/youruser/example/public/node_modules/fsevents
gyp ERR! node -v v12.16.2
gyp ERR! node-gyp -v v5.1.0
gyp ERR! not ok 
npm WARN [email protected] No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] install: `node-gyp rebuild`
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Exit status 1

audited 10199 packages in 3.362s

37 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

以下のように Command Line tools を再インストールして npm install し直せば、解決しました。

sudo rm -rf $(xcode-select -print-path)
xcode-select --install

npm cache clean --force
npm install

(解決方法 3) common.gypi の MACOSX_DEPLOYMENT_TARGET 変更

(背景) macOS アップグレード

今回は macOS Mojave へアップグレード後に発生しました。

common.gypi ファイルの場所を確認

まず、エラーが出ていたときに利用していた Node.js バージョンの common.gypi ファイルの場所を探します。

$ find ~/.node-gyp -name common.gypi

/Users/yourname/.node-gyp/6.11.1/include/node/common.gypi

注意点

/Users/yourname/.nvm/versions/v6.11.0/include/node/common.gypi とありますが、こちらではないです。お間違えなく。

MACOSX_DEPLOYMENT_TARGET の置換

COMMON_GYPI_PATH=/Users/yourname/.node-gyp/6.11.1/include/node/common.gypi
$ grep MACOSX_DEPLOYMENT_TARGET $COMMON_GYPI_PATH

          'MACOSX_DEPLOYMENT_TARGET': '10.7',       # -mmacosx-version-min=10.7
          
$ grep 10.7 $COMMON_GYPI_PATH

          'MACOSX_DEPLOYMENT_TARGET': '10.7',       # -mmacosx-version-min=10.7
TMPFILE="$(mktemp /tmp/XXXXXX)"

# replace 10.7 with 10.9
sed -e "s/10\\.7/10\\.9/g" $COMMON_GYPI_PATH > $TMPFILE

mv $TMPFILE $COMMON_GYPI_PATH

(解決方法 4) PKG_CONFIG_PATH

エラーメッセージ

> [email protected] install /Users/yourname/works/yourapp/node_modules/canvas
> node-gyp rebuild

(node:68901) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGINT listeners added. Use emitter.setMaxListeners() to increase limit
Package libffi was not found in the pkg-config search path.
Perhaps you should add the directory containing `libffi.pc'
to the PKG_CONFIG_PATH environment variable
Package 'libffi', required by 'gobject-2.0', not found
gyp: Call to './util/has_lib.sh freetype' returned exit status 0 while in binding.gyp. while trying to load binding.gyp
gyp ERR! configure error 
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/Users/yourname/.nvm/versions/node/v10.15.3/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:345:16)
gyp ERR! stack     at ChildProcess.emit (events.js:189:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
gyp ERR! System Darwin 18.5.0
gyp ERR! command "/Users/yourname/.nvm/versions/node/v10.15.3/bin/node" "/Users/yourname/.nvm/versions/node/v10.15.3/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/yourname/works/yourapp/node_modules/canvas
gyp ERR! node -v v10.15.3
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok 
npm WARN The package babel-core is included as both a dev and production dependency.
npm WARN The package node-mocks-http is included as both a dev and production dependency.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] 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/yourname/.npm/_logs/2019-04-02T09_05_59_468Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install_all: `npm install`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] install_all 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/yourname/.npm/_logs/2019-04-02T09_05_59_540Z-debug.log

PKG_CONFIG_PATH の確認

$ echo $PKG_CONFIG_PATH
/Users/yourname/.gvm/pkgsets/go1.11/global/overlay/lib/pkgconfig:

echo $PKG_CONFIG_PATH で確認したところ gvm で使ってるなにかに PKG_CONFIG_PATH が設定されており、これが原因のようでした。

PKG_CONFIG_PATH の設定

PKG_CONFIG_PATH に libffi への PATH も設定後 npm install することで解決しました。

# http://opamp.hatenablog.jp/entry/20120316/1331908752
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH/usr/local/opt/libffi/lib/pkgconfig

以上、node-gyp エラーを定期的に解決してる現場からお送りしました。

参考情報