Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

@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, 2021 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, 2021 at 2:19 @TimRoberts I see. I ran that in my terminal and yarn install produces the same error unfortunately. – J. Jackson Nov 29, 2021 at 16:01 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, 2021 at 20:23

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
                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, 2022 at 19:00
                @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, 2022 at 20:05
                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 7, 2022 at 8:53

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

  • 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.

  • override PYTHON env. export PYTHON=python2.

  • Rerun the install.

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

    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 7, 2021 at 16:58 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 7, 2021 at 23:02

    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"
                    Increasing the node-sass version in my package.json made it work. Theres a compatibility table on the repos readme (github.com/sass/node-sass).   I guess overwriting the resolutions is only necessary if node-sass is a transient dependency?
    – EimerReis
                    Jan 25 at 10:44
    

    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
    

    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
    

    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');
    sass.compiler = require('sass');
    
    const sass = require('gulp-sass')
    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.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •