How to resolve node-gyp errors during npm install

Tadashi Shigeoka ·  Sat, October 27, 2018

When I tried to update the version of iconv that was installed via npm, I got a node-gyp rebuild error message and couldn’t install it.

I’ve fought this error many times, so I’ve compiled the solutions. (Last updated: April 24, 2020)

npm

npm install error

$ 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

Running node-gyp rebuild didn’t resolve this issue.

(Solution 1) npm cache clean

npm cache clean
npm install

After npm install, confirm that iconv has been installed and the work is complete.

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

(Solution 2) Reinstalling Command Line tools

This is how to deal with the gyp: No Xcode or CLT version detected! error like the following:

$ 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

Reinstalling Command Line tools and running npm install again resolved the issue:

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

npm cache clean --force
npm install

(Solution 3) Changing MACOSX_DEPLOYMENT_TARGET in common.gypi

(Background) macOS upgrade

This time it occurred after upgrading to macOS Mojave.

Confirming the location of common.gypi file

First, find the location of the common.gypi file for the Node.js version that was being used when the error occurred.

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

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

Note

There is /Users/yourname/.nvm/versions/v6.11.0/include/node/common.gypi, but this is not the right one. Don’t make a mistake.

Replacing 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

(Solution 4) PKG_CONFIG_PATH

Error message

> [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

Checking PKG_CONFIG_PATH

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

When I checked with echo $PKG_CONFIG_PATH, I found that PKG_CONFIG_PATH was set to something used by gvm, which seemed to be the cause.

Setting PKG_CONFIG_PATH

The issue was resolved by setting the PATH to libffi in PKG_CONFIG_PATH and then running npm install.

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

That’s all from the Gemba where I regularly resolve node-gyp errors.

References