节点 gyp 错误 - 无效模式:尝试加载 binding.gyp 时“rU”

Dav*_*aiz 69 node-gyp

运行时出现如下错误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
Run Code Online (Sandbox Code Playgroud)

请告知可能是什么原因?在 Mac OS、M1、Ventura 上运行

Val*_*ira 72

我面临着同样的错误。我有蟒蛇v3.11和画布v2.7.0

解决方案: 我所做的就是将我的 python 版本降级v3.10,然后错误就消失了。您可以从这里安装它

  • 降级并不是一个好的解决方案 (3认同)
  • 如果使用“pyenv”,您可以简单地使用“pyenv isntall 3.10.0”和“pyenv global 3.10.0” (3认同)

小智 34

该问题已报告给node-gyp,并在Python3.11上引入。要解决此问题并继续使用 Python3.11,您可以更新 input.py 文件,因为文件“rU”已重命名为“r”,这就是我们看到此错误的原因。

文件位置: /Users/username/npm-versions/6.13.7/node_modules/node-gyp/gyp/pylib/gyp/input.py

更新方法: LoadOneBuildFile()

方法更新:

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()))
Run Code Online (Sandbox Code Playgroud)


小智 22

第一个解决方案:如果 NPM 需要 Python 版本,请尝试,..

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

IEnpm config set python /opt/homebrew/bin/python3.10

如果 PYTHON 环境变量设置为 Python 可执行文件的路径,则将使用该版本(如果它是兼容版本)。

解决方案二:设置NODE_GYP_FORCE_PYTHON环境变量。

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

# Example: 
#   export NODE_GYP_FORCE_PYTHON=/opt/homebrew/bin/python3.10
Run Code Online (Sandbox Code Playgroud)

  • `brew install python@3.10` 和 `export NODE_GYP_FORCE_PYTHON=/opt/homebrew/bin/python3.10` 为我解决了这个问题 (3认同)

小智 8

如果您使用的是prebuild,请将其更新到 v12.0.0。

早于 v8.0.0 的node-gyp 版本不适用于 Python 3.11。就我而言,我使用的是旧版本的预构建库,该库依赖于 node-gyp v6.0.1。

如果您使用的库没有依赖于 node-gyp 8+ 的较新版本,那么您可以告诉 npm 通过将其添加到"dependencies":"devDependencies":(以依赖于哪个库)来使用更新版本的 node-gyp node-gyp 是),然后将其添加到"overrides":package.json 文件中:

  "devDependencies": {
    "node-gyp": "9.4.0"
  },
  "overrides": {
    "prebuild": {
      "node-gyp": "$node-gyp"
    }
  },
Run Code Online (Sandbox Code Playgroud)

将上面的“ prebuild”替换为依赖于node-gyp的任何库。

overrides属性是在 npm 8.3 中添加的,因此需要Node 16


Ham*_*ywa 5

如果你不介意删除 python。我从我的系统 (Mac M1) 中删除了 Python 并且修复了它。最好在虚拟环境中安装 python,因为全局安装总是会对其他项目产生副作用。参考这篇文章如何在MacBook上删除python

如果你想保留你的全局 python3 安装,我推荐上面的这个答案

总之,在你的node_modules/node-gyp/gyp/pylib/gyp/input.py文件中有这样一行代码,如下所示。(您可以通过单击终端中错误跟踪中打印的最终路径来找到该文件。)

旧代码

 else:
      build_file_contents = open(build_file_path, 'rU').read() 
Run Code Online (Sandbox Code Playgroud)

由于“rU”文件读取模式在 python3 中已被弃用,因此您必须将该代码更新为此。(rU改成r

新代码

 else:
      build_file_contents = open(build_file_path, 'r').read() 
Run Code Online (Sandbox Code Playgroud)

  • 谁决定在 Python 的次要版本中删除“open”函数上支持的文件模式? (7认同)

Ole*_*zov 5

我通过以下步骤解决了这个问题:

  1. brew install python@3.10
  2. 在 .zshrc 中我设置export PYTHON=/opt/homebrew/bin/python3.10
  3. source ~/.zshrc更改 .zshrc 后不要忘记运行