I recently returned to an old project built on Ionic v4 from four years ago. However, when attempting to install all modules using the command npm install
, the process failed with an error related to node-gyp
. I distinctly remember the project running and building successfully without any issues in the past.
Let’s dive into what went wrong this time!
Identifying the Problem
While investigating the issue using the error details from the log:
npm ERR! command failed
npm ERR! command sh -c node-pre-gyp install --fallback-to-build
A quick Google search provided numerous results:
After trying many solutions without success, I revisited the full log details to pinpoint the core issue. I found a critical line about the Python version:
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using [email protected]
npm ERR! gyp info using [email protected] | darwin | arm64
npm ERR! gyp info find Python using Python version 3.13.0 found at "/opt/homebrew/opt/[email protected]/bin/python3.13"
npm ERR! gyp info spawn /opt/homebrew/opt/[email protected]/bin/python3.13
The node-gyp README includes an important note:
Important Python >= v3.12 requires node-gyp >= v10
Aha! The issue became clear.
Solution
In my development environment:
- node-gyp: 10.1.0
- Node.js: v18.20.6
- Python: 3.13.0
- OS: macOS Sonoma 14.6.1 arm64
While my environment matched the requirement Python >= v3.12 requires node-gyp >= v10
, the issue persisted. I decided to install Python 3.12 exactly.
To install and manage multiple versions of Python, I recommend using pyenv:
$ brew update && brew install pyenv
$ pyenv install 3.12
$ pyenv local 3.12
After installation, verify the Python version:
$ python -V
Python 3.12.8
With Python 3.12 installed, I was able to run npm install
successfully without any issues.
Additional Notes
-
If
npm install
still fails due to an incorrect Python binary version, try setting the Python binary path using the environment variable:NODE_GYP_FORCE_PYTHON=python npm install
-
If you continue to face issues, consider downgrading the Node.js version to v14 or v12 and retry.
-
Another solution found online is to install the alternative package
@mapbox/node-pre-gyp
to resolve the issue:npm install @mapbox/node-pre-gyp@^1 --save
Although this did not work in my environment, it might be worth trying in yours.