How to resolve node-gyp + bcrypt errors on MacOS
The most common solution on the Interweb for resolving bcrypt errors is to upgrade the dependancy to the lastest version
npm uninstall --save bcrypt && npm install --save bcrypt@latest
or switch to bcryptjs.
npm install --save bcryptjs && npm uninstall --save bcrypt
If you have the liberty to do so , then you do not need to read this article any further, there is a good chance that changing the version or the dependancy itself will resolve your issue.
However, this may not be an option for the ones trying to run an older project ,or, are somehow bound to use the same version due to other dependancies relying on an older version of bcrypt.
Incase your project is using old libraries which are dependant on an older version of bcrypt (like in my case it was the parse-server dependancy which needed bcrypt version 1.0.2)
and you have node 10+ installed on your system, then it is likely that you are facing a ‘make’ error because a pre-existing binary was not found
node-pre-gyp ERR! Tried to download(404): https://github.com/kelektiv/node.bcrypt.js/releases/download/v1.0.2/bcrypt_lib-v1.0.2-node-v64-darwin-x64.tar.gz
All the available pre-built binaries can be found in the ‘release’ page of the bcrypt github — https://github.com/kelektiv/node.bcrypt.js/releases
and your compiler will try to build a binary for your system depending on the node version you have.
In that case you need to make sure that you have Xcode or command line tools properly installed .
xcode-select --install
Make sure your xcode-select command points to correct Developer folder.
If you have xcode installed —
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer/
If you have command line tools without Xcode —
sudo xcode-select --switch Library/Developer/CommandLineTools
After running the aforementioned command , you can test that the command line tools are successfully installed by running the well-known ‘acid-test’ as follows:
curl -sL https://github.com/nodejs/node-gyp/raw/master/macOS_Catalina_acid_test.sh | bash
If you are able to pass the acid-test and the error still persists then there is a high chance that the node-gyp module that is being used to generate the binary is incompatible and needs to be downgraded / upgraded accordingly.
As an example , lets take the following system specs:
- node — 14.17.0
- yarn -1.22.4
- node-gyp -8.1.0 (global)<- Incompatible
- node-gyp -3.5.0 (package.json)
In this case the global node-gyp module was being used to generate the bcrypt binary which was causing the error and removing it will allow you to use the local(compatible) version as the default.
In case your project does not have a node-gyp dependancy installed locally you can add one by using the following command (the compatible version needs to be specified after the ‘@’ character)
On doing so , your machine will use the locally installed (compatible) version to build bcrypt binary.
Hope you find this article helpful the next time you set up a bcrypt and node-gyp project.
I found the following links quite useful for tracking node-gyp dependancies.
Checkout the bcrypt github for latest updates (linked below).
Cheers.