Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddoomm/ed835aff9d796e57e25b3d9808bcea5c to your computer and use it in GitHub Desktop.
Save ddoomm/ed835aff9d796e57e25b3d9808bcea5c to your computer and use it in GitHub Desktop.
Node-gyp compiling with xcode 10+ and osx 10.7+

Node-gyp compiling with xcode 10+ and osx 10.7+

The problem

On OSX 10.7+ and xcode 10+ I'm not able to install some project dependencies using npm.

NODE and NODE-GYP versions:

dom:~workspace/my_project$ nvm use 0.10.36
Now using node v0.10.36 (npm v2.15.1)
dom:~workspace/my_project$ node-gyp -v
v3.3.1

Running the npm install command, it throws an error compiling the C++ dependecy:

dom:~workspace/my_project$ npm i node-expat
-
> node-expat@2.3.17 install /Users/dom/workspace/my_project/node_modules/node-expat
> node-gyp rebuild

  CC(target) Release/obj.target/expat/deps/libexpat/lib/xmlparse.o
  CC(target) Release/obj.target/expat/deps/libexpat/lib/xmltok.o
  CC(target) Release/obj.target/expat/deps/libexpat/lib/xmlrole.o
  LIBTOOL-STATIC Release/libexpat.a
  CXX(target) Release/obj.target/node_expat/node-expat.o
warning: include path for stdlibc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead [-Wstdlibcxx-not-found]
In file included from ../node-expat.cc:1:
In file included from ../../nan/nan.h:54:
In file included from /Users/dom/.node-gyp/6.11.4/include/node/node.h:42:
/Users/dom/.node-gyp/6.11.4/include/node/v8.h:21:10: fatal error: 'utility' file not found
#include <utility>
         ^~~~~~~~~
1 warning and 1 error generated.
make: *** [Release/obj.target/node_expat/node-expat.o] Error 1
...

The cause

Apple has deprecated the stdlibc++ lib in favor of libc++ and the old\not updated NPM packages that have C++ dependencies to compile throw an error similar to the above one.

How to quickly solve using a little workaround (E.g. NODE 0.10.36):

Edit the file /Users/dom/.node-gyp/0.10.36/common.gypi changing the MACOSX_DEPLOYMENT_TARGET from 10.5 to 10.7 (or higher) because the libc++ requires OSX 10.7+. Also add 'CLANG_CXX_LIBRARY': 'libc++', on row below, in order to tell to OSX to use the newer lib on all packeges compiling.

How it will appear:

...
'PREBINDING': 'NO',                       # No -Wl,-prebind
'MACOSX_DEPLOYMENT_TARGET': '10.7',       # -mmacosx-version-min=10.7
'CLANG_CXX_LIBRARY': 'libc++',
# 'CLANG_CXX_LANGUAGE_STANDARD':'c++11',              # OPTIONAL. this may be not necessary
# 'GCC_VERSION': 'com.apple.compilers.llvm.clang.1_0' # OPTIONAL. this may be not necessary
'USE_HEADERMAP': 'NO',
...

Apply the same change for all installed node verions (I use NVM in order to use multiple node instances on same machine).

E.g. for NODE 4.4.7:

dom:~workspace/my_project$ nvm use 4.4.7
Now using node v4.4.7 (npm v2.15.8)
dom:~workspace/my_project$ node-gyp -v
v3.8.0

Edit the file /Users/dom/.node-gyp/4.4.7/include/node/common.gypi changing the MACOSX_DEPLOYMENT_TARGET from 10.5 to 10.7. Also add 'CLANG_CXX_LIBRARY': 'libc++', on row below.

Result:

dom:~workspace/my_project$ npm i node-expat
-
> node-expat@2.3.17 install /Users/dom/workspace/my_project/node_modules/node-expat
> node-gyp rebuild

  CC(target) Release/obj.target/expat/deps/libexpat/lib/xmlparse.o
  CC(target) Release/obj.target/expat/deps/libexpat/lib/xmltok.o
  CC(target) Release/obj.target/expat/deps/libexpat/lib/xmlrole.o
  LIBTOOL-STATIC Release/libexpat.a
  CXX(target) Release/obj.target/node_expat/node-expat.o
  SOLINK_MODULE(target) Release/node_expat.node
node-expat@2.3.17 node_modules/node-expat

Enjoy it! :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment