42

Getting the following error when running yarn install

gyp/bin/node-gyp.js clean configure
gyp info it worked if it ends with ok
gyp info using node-gyp@5.1.1
gyp info using node@16.18.1 | darwin | arm64
gyp info find Python using Python version 3.11.0 found at \"/Library/Frameworks/Python.framework/Versions/3.11/bin/python3\"
(node:28367) [DEP0150] DeprecationWarning: Setting process.config is deprecated. In the future the property will be read-only.
(Use `node --trace-deprecation ...` to show where the warning was created)
gyp info spawn /Library/Frameworks/Python.framework/Versions/3.11/bin/python3
gyp info spawn args [
gyp info spawn args   '/Users/username/chegg-web/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'make',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/username/chegg-web/node_modules/@chegg/logger/node_modules/@newrelic/native-metrics/build/config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/username/chegg-web/node_modules/node-gyp/addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/username/Library/Caches/node-gyp/16.18.1/include/node/common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=/Users/username/Library/Caches/node-gyp/16.18.1',
gyp info spawn args   '-Dnode_gyp_dir=/Users/username/chegg-web/node_modules/node-gyp',
gyp info spawn args   '-Dnode_lib_file=/Users/username/Library/Caches/node-gyp/16.18.1/<(target_arch)/node.lib',
gyp info spawn args   '-Dmodule_root_dir=/Users/username/chegg-web/node_modules/@chegg/logger/node_modules/@newrelic/native-metrics',
gyp info spawn args   '-Dnode_engine=v8',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'build',
gyp info spawn args   '-Goutput_dir=.'
gyp info spawn args ]
Traceback (most recent call last):
  File \"/Users/username/chegg-web/node_modules/node-gyp/gyp/gyp_main.py\", line 50, in <module>
    sys.exit(gyp.script_main())
             ^^^^^^^^^^^^^^^^^
  File \"/Users/username/chegg-web/node_modules/node-gyp/gyp/pylib/gyp/__init__.py\", line 554, in script_main
    return main(sys.argv[1:])
           ^^^^^^^^^^^^^^^^^^
  File \"/Users/username/chegg-web/node_modules/node-gyp/gyp/pylib/gyp/__init__.py\", line 547, in main
    return gyp_main(args)
           ^^^^^^^^^^^^^^
  File \"/Users/username/chegg-web/node_modules/node-gyp/gyp/pylib/gyp/__init__.py\", line 520, in gyp_main
    [generator, flat_list, targets, data] = Load(
                                            ^^^^^
  File \"/Users/username/chegg-web/node_modules/node-gyp/gyp/pylib/gyp/__init__.py\", line 136, in Load
    result = gyp.input.Load(build_files, default_variables, includes[:],
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File \"/Users/username/chegg-web/node_modules/node-gyp/gyp/pylib/gyp/input.py\", line 2782, in Load
    LoadTargetBuildFile(build_file, data, aux_data,
  File \"/Users/username/chegg-web/node_modules/node-gyp/gyp/pylib/gyp/input.py\", line 391, in LoadTargetBuildFile
    build_file_data = LoadOneBuildFile(build_file_path, data, aux_data,
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File \"/Users/username/chegg-web/node_modules/node-gyp/gyp/pylib/gyp/input.py\", line 234, in LoadOneBuildFile
    build_file_contents = open(build_file_path, 'rU').read()
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid mode: 'rU' while trying to load binding.gyp
gyp ERR! configure error 
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/Users/username/chegg-web/node_modules/node-gyp/lib/configure.js:351:16)
gyp ERR! stack     at ChildProcess.emit (node:events:513:28)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:293:12)
gyp ERR! System Darwin 22.1.0
gyp ERR! command \"/Users/username/.nvm/versions/node/v16.18.1/bin/node\" \"/Users/username/chegg-web/node_modules/node-gyp/bin/node-gyp.js\" \"clean\" \"configure\"
gyp ERR! cwd /Users/username/chegg-web/node_modules/@chegg/logger/node_modules/@newrelic/native-metrics
gyp ERR! node -v v16.18.1
gyp ERR! node-gyp -v v5.1.1
gyp ERR! not ok 
Failed to execute native-metrics install: No pre-built artifacts for your OS/architecture."
info This module is OPTIONAL, you can safely ignore this error

Please advise what might be the cause? Running on Mac OS, M1, Ventura

David Faizulaev
  • 4,651
  • 21
  • 74
  • 124

9 Answers9

46

I was facing this same error. I had python v3.11 and canvas v2.7.0 in my package.json.

Solution: All I did was downgrade my python version to v3.10 and the error disappeared. You can install it from here

23

This issue has been reported to node-gyp and was introduced on Python3.11. To solve it and keep using Python3.11 you can update your input.py file as the file 'rU' has been renamed to 'r' and that is why we see this error.

Location for the file: /Users/username/npm-versions/6.13.7/node_modules/node-gyp/gyp/pylib/gyp/input.py

Method to be updated: LoadOneBuildFile()

Method updated:

def LoadOneBuildFile(build_file_path, data, aux_data, includes,
                     is_target, check):
  if build_file_path in data:
    return data[build_file_path]

  if os.path.exists(build_file_path):
    # Open the build file for read ('r') with universal-newlines mode ('U')
    # to make sure platform specific newlines ('\r\n' or '\r') are converted to '\n'
    # which otherwise will fail eval()
    if sys.platform == 'zos':
      # On z/OS, universal-newlines mode treats the file as an ascii file. But since
      # node-gyp produces ebcdic files, do not use that mode.
      build_file_contents = open(build_file_path, 'r').read()
    else:
      build_file_contents = open(build_file_path, 'r').read() # Here is the line you need to update. Instead of 'rU', update to only 'r'
  else:
    raise GypError("%s not found (cwd: %s)" % (build_file_path, os.getcwd()))
9

First Solution: If the Python version is needed for NPM's sake then try,..

npm config set python /path/to/executable/python

i.e. npm config set python /opt/homebrew/bin/python3.10

If the PYTHON environment variable is set to the path of a Python executable, then that version will be used, if it is a compatible version.

Second Solution: Set NODE_GYP_FORCE_PYTHON environment variable.

# file: $HOME/.bash_profile
NODE_GYP_FORCE_PYTHON=/path/to/executable/python

# Example: 
#   export NODE_GYP_FORCE_PYTHON=/opt/homebrew/bin/python3.10
Jones Agyemang
  • 1,230
  • 16
  • 15
  • `brew install python@3.10` and `export NODE_GYP_FORCE_PYTHON=/opt/homebrew/bin/python3.10` solved it for me – Sebi Aug 01 '23 at 15:43
4

If you don't mind removing python. I deleted Python from my system (Mac M1) and it fixed it. It is better to install python in virtual environments because one global installation can always have side effects on other projects. Refer to this article on how to delete python on a MacBook

If you want to keep your global python3 installation I recommend this answer above.

In summary, in your node_modules/node-gyp/gyp/pylib/gyp/input.py file there is such a line of code as below. (You can find this file by clicking on the final path printed in the error-trace in your terminal.)

Old code

 else:
      build_file_contents = open(build_file_path, 'rU').read() 

Since the 'rU' file read-mode is deprecated in python3 so you have to update that code to this. (Change rU to r)

New code

 else:
      build_file_contents = open(build_file_path, 'r').read() 
Hamza Kyamanywa
  • 417
  • 3
  • 8
3

I got this error because I was using something that depends on an old version of node-gyp. In my case it was the prebuild library, which hasn't updated node-gyp since Jan 11, 2020. You can tell npm to use a more recent version of node-gyp by adding it to "dependencies": or "devDependencies": and then adding it to "overrides": in your package.json file:

  "devDependencies": {
    "node-gyp": "9.4.0"
  },
  "overrides": {
    "prebuild": {
      "node-gyp": "$node-gyp"
    }
  },

Replace "prebuild" above with whatever library depends on node-gyp.

The overrides attribute was added in npm 8.3, so this requires Node 16.

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
3

This solution completely updates the node-gyp version and works regardless of your node version:

Linux Alpine (perhaps other distros):

find /usr/lib -type d -name "node-gyp" -exec sh -c 'cd "$(dirname "{}")" && npm i node-gyp@latest' \;

Mac using nvm:

find ~/.nvm -type d -name "node-gyp" -exec sh -c 'cd "$(dirname "{}")" && npm i node-gyp@latest' \;

basically find all the directories that have node-gyp for your current node installation and manually update the node-gyp version and all the nested dependencies.

On alpine linux I also got an error fatal error: vips/vips8: No such file or directory

So I had to install vips-dev

but when I rand apk add vips-dev I got an error:

ERROR: unable to select packages:
  libmount-2.38.1-r6:
    breaks: util-linux-dev-2.38.1-r7[libmount=2.38.1-r7]
    satisfies: glib-2.76.1-r1[so:libmount.so.1]

to resolve this I ran

apk update
apk add vips-dev

And the vips-dev package installed this time

Now I can run yarn install with no errors!

Josh Woodcock
  • 2,683
  • 1
  • 22
  • 29
2

I had the same issue when I installed python3.11. The solution is exporting the Python to be < 3.10.

Here is the command for Mac M1:

export PYTHON=/opt/homebrew/bin/python3.10
IslamTaha
  • 1,056
  • 1
  • 10
  • 17
1

I didn't want to deal with all the complexities nor downgrade default Python, the following workaround panned out:

  1. Install Miniconda
  2. conda create -n py310 python=3.10
  3. conda activate py310
  4. yarn
j4hangir
  • 2,532
  • 1
  • 13
  • 14
0

I fixed that via the following steps:

  1. brew install python@3.10
  2. In the .zshrc I set export PYTHON=/opt/homebrew/bin/python3.10
  3. Don't forget to run source ~/.zshrc after you change your .zshrc