Skip to content

Instantly share code, notes, and snippets.

@natterstefan
Last active December 13, 2023 08:06
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save natterstefan/ad1c98193b41a75a275d5e17d4f79859 to your computer and use it in GitHub Desktop.
Save natterstefan/ad1c98193b41a75a275d5e17d4f79859 to your computer and use it in GitHub Desktop.
NODE | macOS fix node-gyp

Research | macOS fix node-gyp

visitors

Node

Update asdf

asdf plugin-update --all
asdf update

(source)

node installed with homebrew and asdf

I had node installed with brew previously, but I use asdf now. During my research I found answers indicating I must completely deinstall node.

# I did not remove ~/.nvm and ~/.npmrc though
sudo rm -rf ~/.npm ~/node_modules ~/.node-gyp
sudo rm -rf /usr/local/bin/npm /usr/local/bin/node-debug /usr/local/bin/node /usr/local/bin/node-gyp

# In retrospect I’d not do this, or you delete the man page for node and npm (https://askubuntu.com/a/244810).
sudo rm -rf /usr/local/share/man/man1/node* /usr/local/share/man/man1/npm*

# I did not remove this, as `/usr/local/include/node/openssl` was still there and I
# had issues with `openssl` in the past.
sudo rm -rf /usr/local/include/node /usr/local/include/node_modules

sudo rm -rf /usr/local/lib/node /usr/local/lib/node_modules /usr/local/lib/dtrace/node.d
sudo rm -rf /opt/local/include/node /opt/local/bin/node /opt/local/lib/node
sudo rm -rf /usr/local/share/doc/node
sudo rm -rf /usr/local/share/systemtap/tapset/node.stp

brew uninstall node

# I also removed yarn at the same time
brew uninstall yarn

brew doctor

# I did not run this though.
brew cleanup --prune-prefix

Verify if npx is only installed by asdf:

type -a npx
# npx is /Users/<username>/.asdf/shims/npx

Check global installed node packages

npm list -g --depth 0

When switching the global node version, deinstall packages and install them again in the new system version. In general removing global installations from older node versions is a good thing to do (source).

node-gyp and Python

node-gyp says it works with Python > 3.x, but only when used with a proper npm version (which comes with its own node-gyp version). You can read more about this in the sections below.

I did not test setting the Configuring Python Dependency though.

# run "which python" first to get the path
npm config set python /path/to/executable/python

node-gyp and python@2.7.x

I had to install and use python@2.7.13 with node@12.16.0 after I read this:

It might not be node-gyp. Apparently it uses python 3 since version 5.5, although some windows users still report issues. It likely stems from node-sass using an older node-gyp dependency as indicated by a comment in the link below.

nodejs/node-gyp#1977

(source)

It still looks like some dependencies use Python@2.7.x (proofed here as well as mentioned also here, here and here), which causes node-gyp errors.

# e.g. with asdf
asdf install python 2.7.13
asdf install nodejs 12.14.1
# node@v12.14.1
# npm@6.13.4

Only then CRA (and other commands) worked again.

At this point my global .tool-versions looks like this:

nodejs 12.16.0
python 2.7.13
yarn 1.22.4

Upgrade npm to update npm-internal node-gyp with Python@3.x support

npm comes with an internal version of node-gyp (source). This can also cause issues (when old versions are used). You can update npm (and consequently node-gyp)

# https://github.com/asdf-vm/asdf/issues/162#issuecomment-383286403
cd ~/.asdf/installs/nodejs/[VERSION]/lib && npm i npm

# https://github.com/asdf-vm/asdf/issues/162#issuecomment-321791251
asdf reshim nodejs

(source)

It is also documented here:

You do not need Python 2.7 in order to run node-gyp. Python 3 has been supported since version 5.0.5. If you have verified that your issue is python related, as in that node-gyp is trying to run a script which contains errors according to the python 3 interpreter, then try the following:

  • Update your global node-gyp install. npm install -g node-gyp
  • Update your npm install. npm install -g npm

Both modules are separate, npm will have its own copy of node-gyp. If you are trying to link a local package, for instance, npm will use its own copy of node-gyp. However, if you run node-gyp from the command line, it will use the other.

CRA

npx create-react-app

npx searches for globally-accessible bins, but also the bin folder of every node_modules at or above your current working directory. You probably have create-react-app installed higher in your directory tree. You can test this by seeing if adding the ignore existing flag makes it work: npx --ignore-existing create-react-app newapp.

(source, but also mentioned here)

Although npx --ignore-existing create-react-app newapp worked (when npx create-react-app newapp did not), I figured that removing old node files (see other chapter), removing the global CRA installation and using an python@2.7 again helped.

I did not test npx create-react-app@latest myapp though (was also mentioned).

Remove global CRA installation

When you got the following error, you have to remove the old global CRA installation:

A template was not provided. This is likely because you're using an outdated version of create-react-app.

type -a create-react-app
create-react-app is /usr/local/bin/create-react-app

If this still exists, run this to uninstall it (according to this issue).

rm -rf /usr/local/bin/create-react-app

Xcode

node-gyp requires XCode's Command Line Tools. You have to verify they are installed and work.

node-gyp, XCode and Command Line Tools

Test if the Command Line Tools are properly installed with this tutorial.

Uninstall XCode and Command Line tools

killall Xcode
xcrun -k
xcodebuild -alltargets clean
rm -rf "$(getconf DARWIN_USER_CACHE_DIR)/org.llvm.clang/ModuleCache"
rm -rf "$(getconf DARWIN_USER_CACHE_DIR)/org.llvm.clang.$(whoami)/ModuleCache"
rm -rf /Applications/Xcode.app
rm -rf ~/Library/Caches/com.apple.dt.Xcode
rm -rf ~/Library/Developer
rm -rf ~/Library/MobileDevice
rm -rf ~/Library/Preferences/com.apple.dt.Xcode.plist
rm -rf ~/Library/Preferences/com.apple.dt.xcodebuild.plist
sudo rm -rf /Library/Preferences/com.apple.dt.Xcode.plist
sudo rm -rf /System/Library/Receipts/com.apple.pkg.XcodeExtensionSupport.bom
sudo rm -rf /System/Library/Receipts/com.apple.pkg.XcodeExtensionSupport.plist
sudo rm -rf /System/Library/Receipts/com.apple.pkg.XcodeSystemResources.bom
sudo rm -rf /System/Library/Receipts/com.apple.pkg.XcodeSystemResources.plist
sudo rm -rf /private/var/db/receipts/com.apple.pkg.Xcode.bom

(https://medium.com/@onexlab.io/uninstall-xcode-from-macos-eca1b69dc836)

Install Command Line Tools

Install XCode from the App Store and then you can install the Command Line Tools afterward. Just download and install them manually.

Troubleshooting

TypeError: '>=' not supported between instances of 'tuple' and 'str'

This error happens, when an old version of node-gyp is used with python@3.x. Ensure you install the latest node-gyp with npm to overwrite their internal node-gyp version (see Python section above for more details).

npm explore npm -g -- npm install node-gyp@latest
npm explore npm -g -- npm explore npm-lifecycle -- npm install node-gyp@latest

make failed with exit code 2

@guychouk
Copy link

This is amazing, thank you so much!

@natterstefan
Copy link
Author

This is amazing, thank you so much!

You're welcome. I am happy this was of help for you @guychouk.

@janpauldahlke
Copy link

been here, done that. thanks for the gist

@natterstefan
Copy link
Author

Hi @janpauldahlke,

I'm glad it helped you!

@pdemagny
Copy link

pdemagny commented Mar 2, 2023

Hi !
In our case, all we had to do was to follow this doc and run:

npm install -g node-gyp
npm config set node_gyp <path to node-gyp>

Hope this helps !

@rubenbelem
Copy link

Thank you very much for this gist!! It helped me solve an issue with heroku cli that isn't even commented here haha.

I tried to run a heroku run from the terminal and got the error:

Running pg_dump -s -x -O $DATABASE_URL on test-le-svc-order-syd... done
    TypeError: Cannot read properties of undefined (reading 'statusCode')

The things I did that helped solving this issue was:

  1. Changed my 20.0.0 global version in ~/.tools-version to latest Node v18 LTS (18.18.0)
  2. Ran the steps from this section: https://gist.github.com/natterstefan/ad1c98193b41a75a275d5e17d4f79859#upgrade-npm-to-update-npm-internal-node-gyp-with-python3x-support
  3. Ran npm explore npm -g -- npm install node-gyp@latest

@natterstefan
Copy link
Author

Hi @rubenbelem,

I'm happy that the gist was of help to you and thanks for extending the range of solutions for this problem by providing further instructions.

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