66

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 node-gyp@3.8.0
gyp info using node@12.18.0 | darwin | x64
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "/usr/local/opt/python@3.9/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?

J. Jackson
  • 3,326
  • 8
  • 34
  • 74
  • 2
    You're saying that `export PYTHON=/usr/bin/python3` did not fix this? – Tim Roberts Nov 24 '21 at 21:00
  • @TimRoberts I'm assuming you mean by adding that to my .zshrc? I do have that in my zsh and it's still not working. – J. Jackson Nov 27 '21 at 17:29
  • You should be able to type that at a command line before you run the `yarn` command. If that flies, then that gives you a clue. – Tim Roberts Nov 28 '21 at 02:19
  • @TimRoberts I see. I ran that in my terminal and `yarn install` produces the same error unfortunately. – J. Jackson Nov 29 '21 at 16:01
  • 1
    The python in `/usr/local/opt` is managed by homebrew. Can you try removing `/usr/local/opt` from your `PATH`, at least temporarily? Alternatively, you can prepend `/usr/bin` to your path like this: `PATH=/usr/bin:$PATH yarn install ...` – jkr Nov 30 '21 at 20:23
  • Hi @jakub, that appears to be giving the same error still. – J. Jackson Dec 01 '21 at 15:13
  • Does `/usr/local/opt/python@3.9/bin/python3` exist ? – Philippe Dec 05 '21 at 23:08

6 Answers6

127

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
$ source ~/.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
Nikita Fuchs
  • 229
  • 5
  • 13
PaulMest
  • 12,925
  • 7
  • 53
  • 50
  • 1
    thank you for the response. I am also using macOS Monterey (`12.3.1`) and one of the packages I am installing is looking exclusively for `python2`. Can you help me understand, how I can address this? `node --version: v16.14.2 and npm --version: 8.5.0` – Deepak Thota May 14 '22 at 19:00
  • 9
    @DeepakThota try using the the answer above, but replace `3.10.3` with `2.7.18`. If `python2` is needed... try to also point `python2` to the newly installed version of python. `export python2=python` – PaulMest May 14 '22 at 20:05
  • 2
    Yes using `pyenv install 2.7.18` and exporting it worked. Thank you! – Deepak Thota May 14 '22 at 20:57
  • 4
    If you want you can add python2 to be set localy for specific folder. Go inside that folder and run `pyenv local 2.7.18` – emir Jun 07 '22 at 08:53
  • open a new terminal window/tab so you see `which python` command is working – Matteus Barbosa Jul 14 '22 at 13:58
  • 3
    exec `source ~/. zshrc` after editing it – unloco Aug 09 '22 at 13:37
  • Thanks a lot. Sometimes we should have both versions of Python: 2.x & 3.x. – nlavr Nov 07 '22 at 20:41
29

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.

willnode
  • 1,377
  • 13
  • 17
  • This appears to have fixed it! I knew I'd been close this whole time, but your step 2 seems to have been the one that I haven't tried. Thanks! – J. Jackson Dec 07 '21 at 16:58
  • 3
    Yeah, I was kinda bit surprised when reading the code, the `PYTHON` env will get appended with `PATH` no matter what, so putting the full path of python there is just wrong. That's a bit weird and easily explains your problem, but sure it may be just because one of the dependencies in your project uses old `node-gyp`. – willnode Dec 07 '21 at 23:02
26

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"
  }
Jan Klimo
  • 4,643
  • 2
  • 36
  • 42
  • This is the only thing that worked for me. – Scott Romack Nov 02 '22 at 13:34
  • 1
    Increasing the node-sass version in my package.json made it work. Theres a compatibility table on the repos readme (https://github.com/sass/node-sass). I guess overwriting the resolutions is only necessary if node-sass is a transient dependency? – EimerReis Jan 25 '23 at 10:44
7

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
Pedreiro
  • 1,641
  • 2
  • 18
  • 28
0

From the terminal messages, you are installing an old version of node-gyp (node-gyp@3.8.0). 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
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
0

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.

2xSamurai
  • 1,221
  • 12
  • 18