NPM Error "Can't find Python executable" in MacOS Big Sur

Question:

I’ve been looking for the answer to this for a good solid week now, with no success. I’ve looked at every StackOverflow post, every article from Google and every related Github issue I could find. Most related errors seem to be older, so I’m wondering if my issue is slightly different due to me being on macOS Big Sur.

The issue:
When I try to run yarn install in my local repo, I receive an error related to node-gyp and a python executable that is unable to be found. Here is what my terminal shows:

yarn install v1.22.17

...other stuff

[4/4]    Building fresh packages...
[6/13] ⠐ node-sass
[2/13] ⠐ node-sass
[10/13] ⠐ metrohash
[4/13] ⠐ fsevents
error /Users/jimmiejackson/Documents/repositories/repo-name/node_modules/metrohash: Command failed.
Exit code: 1
Command: node-gyp rebuild
Arguments:
Directory: /Users/jimmiejackson/Documents/repositories/repo-name/node_modules/metrohash
Output:
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | darwin | x64
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "/usr/local/opt/[email protected]/bin/python3", you can set the PYTHON env variable.
gyp ERR! stack     at PythonFinder.failNoPython (/Users/jimmiejackson/Documents/repositories/repo-name/node_modules/node-gyp/lib/configure.js:484:19)
gyp ERR! stack     at PythonFinder.<anonymous> (/Users/jimmiejackson/Documents/repositories/repo-name/node_modules/node-gyp/lib/configure.js:406:16)
gyp ERR! stack     at F (/Users/jimmiejackson/Documents/repositories/repo-name/node_modules/which/which.js:68:16)
gyp ERR! stack     at E (/Users/jimmiejackson/Documents/repositories/repo-name/node_modules/which/which.js:80:29)
gyp ERR! stack     at /Users/jimmiejackson/Documents/repositories/repo-name/node_modules/which/which.js:89:16
gyp ERR! stack     at /Users/jimmiejackson/Documents/repositories/repo-name/node_modules/isexe/index.js:42:5
gyp ERR! stack     at /Users/jimmiejackson/Documents/repositories/repo-name/node_modules/isexe/mode.js:8:5
gyp ERR! stack     at FSReqCallback.oncomplete (fs.js:167:21)
gyp ERR! System Darwin 20.6.0
gyp ERR! command "/Users/jimmiejackson/.nvm/versions/node/v12.18.0/bin/node" "/Users/jimmiejackson/Documents/repositories/repo-name/node_modules/metrohash/node_modules/.bin/node-gyp" "rebuild"
gyp ERR! cwd /Users/jimmiejackson/Documents/repositories/repo-name/node_modules/metrohash

I’m not entirely sure what this error means or why this node module is searching for python3. I’ve tried running npm set config /path/to/python, downloading python3, setting the PYTHON path in my .zshrc profile, but nothing seems to be working. It’s entirely possible that my lack of understanding of the issue means that I’m on the right path but didn’t quite get something right. Any ideas?

Asked By: J. Jackson

||

Answers:

From the terminal messages, you are installing an old version of node-gyp ([email protected]). From a quick search, it seams that this version requires python 2. Python 2 should be present in Big Sur. Properly setting the path, should work:

export PATH=/usr/bin/python:$PATH

Also, try:

export PYTHON=/usr/bin/python
Answered By: kgiannakakis

I believe you can explicitly define env var by prefixing it with npm_config:

$ export npm_config_python=/path/to/python

check if that is configured by listing the config:

$ npm config list
...

; environment configs
python = "/path/to/python"

this should be picked up by node-gyp.

Another approach would be to define it in .npmrc

python = "/path/to/python"

A third approach would be to set it globaly:

npm config --global set python /path/to/python
Answered By: Pedreiro

Reading the gyp-node source might helps. Here are some steps you can try.

  1. Install python2. You should make sure that in the terminal, which -a python2 only returns one python2 and python2 -V returns the correct 2.x version.

  2. override PYTHON env. export PYTHON=python2.

  3. Rerun the install.

If there’s still an error, probably the error message is different.

Answered By: willnode

This one also plagued me for a week because node-gyp could actually find my Python instance just fine, but then a later build step was not properly configured and all of the popular answers weren’t able to pick up Python. My steps to resolve on macOS Monterey (12.3.1)…

$ brew install pyenv

# Any modern version python should do. I don't think Python 2 is required any more.
$ pyenv install 3.10.3
$ pyenv global 3.10.3

# Add pyenv to your PATH so that you can reference python (not python3)
$ echo "export PATH="${HOME}/.pyenv/shims:${PATH}"" >> ~/.zshrc

# open a new terminal window and confirm your pyenv version is mapped to python
$ which python
$ python --version

# Now try to re-run yarn install
$ yarn
Answered By: PaulMest

You might be seeing this issue if you upgrade from Node 14 to Node 16, like I did. In that case, a simple workaround for it might be making sure yarn resolves node-sass to version 6.

Set this in your package.json:

 "resolutions": {
    "node-sass": "^6.0.1"
  }
Answered By: Jan Klimo

I was also having the same issue. Tried almost everything. Switching the node version using nvm. Restarting. Trying to install a different node-sass version. Nothing worked for me.

Instead of trying to get node-sass work. You can just migrate to dart-sass(sass). There is practically no code change except for the import lines.

In my file. The changes were.

sass.compiler = require('node-sass');

to

sass.compiler = require('sass');

and

const sass = require('gulp-sass')

to

const sass = require('gulp-sass')(require('sass'))

Check the following article as well.

https://sass-lang.com/blog/libsass-is-deprecated#how-do-i-migrate

Hope this helps someone.

Answered By: 2xSamurai
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.