Last updated: Apr 13, 2024
Reading time·3 min

The node-gyp "ValueError: invalid mode: 'rU' while trying to load
binding.gyp" was introduced in Python version 3.11.
There are 2 main ways to solve the error:
gyp/input.py file.gyp/input.py fileYour input.py file should be located at:
on Windows -
C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\gyp\pylib\gyp\input.py.
on macOS -
/Users/YOUR_USER/npm-versions/YOUR_NPM_VERSION/node_modules/node-gyp/gyp/pylib/gyp/input.py.
on Ubuntu -
/home/YOUR_USER/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/input.py
You can also use the locate command to locate the directory on Linux.
locate "pylib/gyp" | grep /pylib/gyp$
Once you find the input.py file, open it in your preferred text editor.
Ctrl + F to look for the definition of the LoadOneBuildFile
function.Ctrl + F to search for build_file_contents = open.The following line:
# ⛔️ Incorrect - has rU build_file_contents = open(build_file_path, 'rU').read()
Should become the following line:
# ✅ Correct - has only r build_file_contents = open(build_file_path, 'r').read()
Notice that I removed the U after the r.
Once you make the change, save the file and the issue should be resolved.
3.10The error was introduced in Python 3.11.
You can check your Python version by issuing the following command.
python --version # Or python3 python3 --version # Or py py --version

You can downgrade your Python version to 3.10 by downloading it from
the official page.
Once you scroll down to the Files section, you will see the download links.

If you use Anaconda, you can install a specific Python version using the following command:
conda install python=3.10.6

And you can create a virtual environment scoped to version 3.10 with the
following command.
conda create --name my_env -c anaconda python=3.10.6
Make sure to replace my_env with the name of your virtual environment.
If you are on macOS, you can also try to install python3.10 using brew.
brew install python@3.10 export NODE_GYP_FORCE_PYTHON=/opt/homebrew/bin/python3.10
If the error persists, you can try to run the npm config set python command.
First, run the following command.
npm install --python=python3.10
Now, issue the npm config set python command with the path to your python
executable.
npm config set python /path/to/python/executable
For example, on macOS, your path might look like:
/opt/homebrew/bin/python3.10.
npm config set python /opt/homebrew/bin/python3.10
On Linux, your path might look something like /usr/bin/python3.10.
npm config set python /usr/bin/python3.10
On Windows, your path might look something like C:\Python3.10\python.exe.
npm config set python C:\Python3.10\python.exe
node-gyp versionYou can also try to update your node-gyp version.
If you are on macOS or Linux, try issuing the following command.
find ~/.nvm -type d -name "node-gyp" -exec sh -c 'cd "$(dirname "{}")" && npm i node-gyp@latest' \;

You can learn more about the related topics by checking out the following tutorials: