Fix broken ‘node-gyp’ issue for Mac OS
This issue is usually caused because a node-gyp config file called common.gypi is not set up for your current Mac OS version.
It often times comes accompanied with an error message like:
Another usual fix is to reinstall XCode’s command-line tools with:
Just for context, this issue usually happens after upgrading your OS of if you use nvm and are using a newly installed NodeJs version.
If your project has any complex dependencies that rely heavily on native libraries this solution might not be for you.
If the most complex node-gyp -related dependency you have in your project is something common like node-sass then this is usually the fix you need.
You’ll need to find and edit the common.gypi file for the NodeJs version you’re using. You’ll usually find it in a path like:
Example for NodeJs 6.5.0:
means your user’s $HOME directory)
Open the file, find the MACOSX_DEPLOYMENT_TARGET key value and replace it with your current Mac OS version ( 10.15.3 for Catalina for example).
Examples with different editors:
Using nano
The most underrated editor ever:
Using VS Code
Yes you can also use it to edit shady config files, just Save the file as usual when you’re done.
(Remember this is under $HOME so the command doesn’t require any special permissions like sudo)
Using vim
(But why? Odds are you’re already having a shitty day with node-gyp):
Pro-tip: To exit hit Esc and then :wq! and then Enter .
w is for write, q for quit and ! to express you’re happy to be free again!
Just kidding, it’s to force the operation.
If this doesn’t work
Here are some related Github issues and Stack Overflow posts that might help:
Источник
Node gyp mac os
Copy raw contents
Installation notes for macOS Catalina (v10.15)
This document specifically refers to upgrades from previous versions of macOS to Catalina (10.15). It should be removed from the source repository when Catalina ceases to be the latest macOS version or when future Catalina versions no longer raise these issues.
Both upgrading to macOS Catalina and running a Software Update in Catalina may cause normal node-gyp installations to fail. This might manifest as the following error during npm install :
The newest release of node-gyp should solve this problem. If you are using node-gyp directly then you should be able to install v7 and use it as-is.
If you need to use node-gyp from within npm (e.g. through npm install ), you will have to install node-gyp (either globally with -g or to a predictable location) and tell npm where the new version is. Either use:
- npm config set node_gyp
; or
run npm with an environment variable prefix: npm_config_node_gyp=
Where «path to node-gyp» is to the node-gyp executable which may be a symlink in your global bin directory (e.g. /usr/local/bin/node-gyp ), or a path to the node-gyp installation directory and the bin/node-gyp.js file within it (e.g. /usr/local/lib/node_modules/node-gyp/bin/node-gyp.js ).
If you use npm config set to change your global node_gyp you are responsible for keeping it up to date and can’t rely on npm to give you a newer version when available. Use npm config delete node_gyp to unset this configuration option.
Fixing Catalina for older versions of node-gyp
Is my Mac running macOS Catalina?
Let’s first make sure that your Mac is running Catalina:
If ProductVersion is less then 10.15 then this document is not for you. Normal install docs for node-gyp on macOS can be found at https://github.com/nodejs/node-gyp#on-macos
To see if Xcode Command Line Tools is installed in a way that will work with node-gyp , run:
If test succeeded, you are done! You should be ready to install node-gyp .
If test failed, there is a problem with your Xcode Command Line Tools installation. Continue to Solutions.
There are three ways to install the Xcode libraries node-gyp needs on macOS. People running Catalina have had success with some but not others in a way that has been unpredictable.
- With the full Xcode (
7.6 GB download) from the App Store app.
Installing node-gyp using the full Xcode
- xcodebuild -version should show Xcode 11.1 or later.
- If not, then install/upgrade Xcode from the App Store app.
- Open the Xcode app and.
- Under Preferences > Locations select the tools if their location is empty.
- Allow Xcode app to do an essential install of the most recent compiler tools.
- Once all installations are complete, quit out of Xcode.
- sudo xcodebuild -license accept # If you agree with the licensing terms.
- softwareupdate -l # No listing is a good sign.
- If Xcode or Tools upgrades are listed, use «Software Upgrade» to install them.
- xcode-select -version # Should return xcode-select version 2370 or later.
- xcode-select -print-path # Should return /Applications/Xcode.app/Contents/Developer
- Try the acid test steps above to see if your Mac is ready.
- If the acid test does not pass then.
- sudo xcode-select —reset # Enter root password. No output is normal.
- Repeat step 7 above. Is the path different this time? Repeat the acid test.
Installing node-gyp using the Xcode Command Line Tools via xcode-select —install
- If the acid test has not succeeded, then try xcode-select —install
- If the installation command returns xcode-select: error: command line tools are already installed, use «Software Update» to install updates , continue to remove and reinstall
- Wait until the install process is complete.
- softwareupdate -l # No listing is a good sign.
- If Xcode or Tools upgrades are listed, use «Software Update» to install them.
- xcode-select -version # Should return xcode-select version 2370 or later.
- xcode-select -print-path # Should return /Library/Developer/CommandLineTools
- Try the acid test steps above to see if your Mac is ready.
- If the acid test does not pass then.
- sudo xcode-select —reset # Enter root password. No output is normal.
- Repeat step 5 above. Is the path different this time? Repeat the acid test.
Installing node-gyp using the Xcode Command Line Tools via manual download
- Download the appropriate version of the «Command Line Tools for Xcode» for your version of Catalina from https://developer.apple.com/download/more/. As of MacOS 10.15.5, that’s Command_Line_Tools_for_Xcode_11.5.dmg
- Install the package.
- Run the acid test steps above.
I did all that and the acid test still does not pass 🙁
Источник
Node gyp mac os
node-gyp — Node.js native addon build tool
node-gyp is a cross-platform command-line tool written in Node.js for compiling native addon modules for Node.js. It contains a vendored copy of the gyp-next project that was previously used by the Chromium team, extended to support the development of Node.js native addons.
Note that node-gyp is not used to build Node.js itself.
Multiple target versions of Node.js are supported (i.e. 0.8 , . 4 , 5 , 6 , etc.), regardless of what version of Node.js is actually installed on your system ( node-gyp downloads the necessary development files or headers for the target version).
- The same build commands work on any of the supported platforms
- Supports the targeting of different versions of Node.js
You can install node-gyp using npm :
Depending on your operating system, you will need to install:
- Python v3.6, v3.7, v3.8, or v3.9
- make
- A proper C/C++ compiler toolchain, like GCC
ATTENTION: If your Mac has been upgraded to macOS Catalina (10.15), please read macOS_Catalina.md.
- Python v3.6, v3.7, v3.8, or v3.9
- Xcode
- You also need to install the XCode Command Line Tools by running xcode-select —install . Alternatively, if you already have the full Xcode installed, you can find them under the menu Xcode -> Open Developer Tool -> More Developer Tools. . This step will install clang , clang++ , and make .
Install the current version of Python from the Microsoft Store package.
Install tools and configuration manually:
- Install Visual C++ Build Environment: Visual Studio Build Tools (using «Visual C++ build tools» workload) or Visual Studio Community (using the «Desktop development with C++» workload)
- Launch cmd, npm config set msvs_version 2017
If the above steps didn’t work for you, please visit Microsoft’s Node.js Guidelines for Windows for additional tips.
To target native ARM64 Node.js on Windows 10 on ARM, add the components «Visual C++ compilers and libraries for ARM64» and «Visual C++ ATL for ARM64».
Configuring Python Dependency
node-gyp requires that you have installed a compatible version of Python, one of: v3.6, v3.7, v3.8, or v3.9. If you have multiple Python versions installed, you can identify which Python version node-gyp should use in one of the following ways:
- by setting the —python command-line option, e.g.:
- If node-gyp is called by way of npm , and you have multiple versions of Python installed, then you can set npm ‘s ‘python’ config key to the appropriate value:
If the PYTHON environment variable is set to the path of a Python executable, then that version will be used, if it is a compatible version.
If the NODE_GYP_FORCE_PYTHON environment variable is set to the path of a Python executable, it will be used instead of any of the other configured or builtin Python search paths. If it’s not a compatible version, no further searching will be done.
To compile your native addon, first go to its root directory:
The next step is to generate the appropriate project build files for the current platform. Use configure for that:
Auto-detection fails for Visual C++ Build Tools 2015, so —msvs_version=2015 needs to be added (not needed when run by npm as configured above):
Note: The configure step looks for a binding.gyp file in the current directory to process. See below for instructions on creating a binding.gyp file.
Now you will have either a Makefile (on Unix platforms) or a vcxproj file (on Windows) in the build/ directory. Next, invoke the build command:
Now you have your compiled .node bindings file! The compiled bindings end up in build/Debug/ or build/Release/ , depending on the build mode. At this point, you can require the .node file with Node.js and run your tests!
Note: To create a Debug build of the bindings file, pass the —debug (or -d ) switch when running either the configure , build or rebuild commands.
The binding.gyp file
A binding.gyp file describes the configuration to build your module, in a JSON-like format. This file gets placed in the root of your package, alongside package.json .
A barebones gyp file appropriate for building a Node.js addon could look like:
The docs directory contains additional documentation on specific node-gyp topics that may be useful if you are experiencing problems installing or building addons using node-gyp.
Some additional resources for Node.js native addons and writing gyp configuration files:
node-gyp responds to the following commands:
Command | Description |
---|---|
help | Shows the help dialog |
build | Invokes make / msbuild.exe and builds the native addon |
clean | Removes the build directory if it exists |
configure | Generates project build files for the current platform |
rebuild | Runs clean , configure and build all in a row |
install | Installs Node.js header files for the given version |
list | Lists the currently installed Node.js header versions |
remove | Removes the Node.js header files for the given version |
node-gyp accepts the following command options:
Command | Description |
---|---|
-j n , —jobs n | Run make in parallel. The value max will use all available CPU cores |
—target=v6.2.1 | Node.js version to build for (default is process.version ) |
—silly , —loglevel=silly | Log all progress to console |
—verbose , —loglevel=verbose | Log most progress to console |
—silent , —loglevel=silent | Don’t log anything to console |
debug , —debug | Make Debug build (default is Release ) |
—release , —no-debug | Make Release build |
-C $dir , —directory=$dir | Run command in different directory |
—make=$make | Override make command (e.g. gmake ) |
—thin=yes | Enable thin static libraries |
—arch=$arch | Set target architecture (e.g. ia32) |
—tarball=$path | Get headers from a local tarball |
—devdir=$path | SDK download directory (default is OS cache directory) |
—ensure | Don’t reinstall headers if already present |
—dist-url=$url | Download header tarball from custom URL |
—proxy=$url | Set HTTP(S) proxy for downloading header tarball |
—noproxy=$urls | Set urls to ignore proxies when downloading header tarball |
—cafile=$cafile | Override default CA chain (to download tarball) |
—nodedir=$path | Set the path to the node source code |
—python=$path | Set path to the Python binary |
—msvs_version=$version | Set Visual Studio version (Windows only) |
—solution=$solution | Set Visual Studio Solution version (Windows only) |
Use the form npm_config_OPTION_NAME for any of the command options listed above (dashes in option names should be replaced by underscores).
For example, to set devdir equal to /tmp/.gyp , you would:
Run this on Unix:
Or this on Windows:
Use the form OPTION_NAME for any of the command options listed above.
For example, to set devdir equal to /tmp/.gyp , you would run:
Note: Configuration set via npm will only be used when node-gyp is run via npm , not when node-gyp is run directly.
node-gyp is available under the MIT license. See the LICENSE file for details.
Источник