Npm 安装错误。 'npm ERR! gyp ERR! find Python, stack Error'

Npm install error. 'npm ERR! gyp ERR! find Python, stack Error'

每当我在我的 nuxt.js(vue.js) 项目中尝试 运行 npm installnpm update 时,就会出现以下错误。

npm ERR! code 1
npm ERR! path /Users/kyeolhan/ForWork/BackOffceFront/node_modules/deasync
npm ERR! command failed
npm ERR! command sh -c node ./build.js
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@7.1.2
npm ERR! gyp info using node@16.14.2 | darwin | arm64
npm ERR! gyp ERR! find Python
npm ERR! gyp ERR! find Python Python is not set from command line or npm configuration
npm ERR! gyp ERR! find Python Python is not set from environment variable PYTHON
npm ERR! gyp ERR! find Python checking if "python3" can be used
npm ERR! gyp ERR! find Python - "python3" is not in PATH or produced an error
npm ERR! gyp ERR! find Python checking if "python" can be used
npm ERR! gyp ERR! find Python - "python" is not in PATH or produced an error
npm ERR! gyp ERR! find Python checking if "python2" can be used
npm ERR! gyp ERR! find Python - "python2" is not in PATH or produced an error
npm ERR! gyp ERR! find Python
npm ERR! gyp ERR! find Python **********************************************************
npm ERR! gyp ERR! find Python You need to install the latest version of Python.
npm ERR! gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
npm ERR! gyp ERR! find Python you can try one of the following options:
npm ERR! gyp ERR! find Python - Use the switch --python="/path/to/pythonexecutable"
npm ERR! gyp ERR! find Python   (accepted by both node-gyp and npm)
npm ERR! gyp ERR! find Python - Set the environment variable PYTHON
npm ERR! gyp ERR! find Python - Set the npm configuration variable python:
npm ERR! gyp ERR! find Python   npm config set python "/path/to/pythonexecutable"
npm ERR! gyp ERR! find Python For more information consult the documentation at:
npm ERR! gyp ERR! find Python https://github.com/nodejs/node-gyp#installation
npm ERR! gyp ERR! find Python **********************************************************
npm ERR! gyp ERR! find Python
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Could not find any Python installation to use
npm ERR! gyp ERR! stack     at PythonFinder.fail (/Users/kyeolhan/ForWork/BackOffceFront/node_modules/node-gyp/lib/find-python.js:302:47)
npm ERR! gyp ERR! stack     at PythonFinder.runChecks (/Users/kyeolhan/ForWork/BackOffceFront/node_modules/node-gyp/lib/find-python.js:136:21)
npm ERR! gyp ERR! stack     at PythonFinder.<anonymous> (/Users/kyeolhan/ForWork/BackOffceFront/node_modules/node-gyp/lib/find-python.js:179:16)
npm ERR! gyp ERR! stack     at PythonFinder.execFileCallback (/Users/kyeolhan/ForWork/BackOffceFront/node_modules/node-gyp/lib/find-python.js:266:16)
npm ERR! gyp ERR! stack     at exithandler (node:child_process:406:5)
npm ERR! gyp ERR! stack     at ChildProcess.errorhandler (node:child_process:418:5)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:526:28)
npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:289:12)
npm ERR! gyp ERR! stack     at onErrorNT (node:internal/child_process:478:16)
npm ERR! gyp ERR! stack     at processTicksAndRejections (node:internal/process/task_queues:83:21)
npm ERR! gyp ERR! System Darwin 21.4.0
npm ERR! gyp ERR! command "/Users/kyeolhan/.nvm/versions/node/v16.14.2/bin/node" "/Users/kyeolhan/ForWork/BackOffceFront/node_modules/.bin/node-gyp" "rebuild"
npm ERR! gyp ERR! cwd /Users/kyeolhan/ForWork/BackOffceFront/node_modules/deasync
npm ERR! gyp ERR! node -v v16.14.2
npm ERR! gyp ERR! node-gyp -v v7.1.2
npm ERR! gyp ERR! not ok
npm ERR! Build failed

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/kyeolhan/.npm/_logs/2022-03-28T12_56_28_864Z-debug-0.log

python3 安装在我的 mac (Apple M1 Pro, macOS Monterey 12.3).

$ python3 --version
Python 3.9.12

我还尝试了 --python 选项,路径如下。

$ which -a python3
/opt/homebrew/bin/python3
/usr/bin/python3
/opt/homebrew/bin/python3
npm i --python="/usr/bin/python3"
npm i --python="/opt/homebrew/bin/python3"

但是不行

npm ERR! code 1
npm ERR! path /Users/kyeolhan/ForWork/BackOffceFront/node_modules/deasync
npm ERR! command failed
npm ERR! command sh -c node ./build.js
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@9.0.0
npm ERR! gyp info using node@16.14.2 | darwin | arm64
npm ERR! gyp ERR! find Python
npm ERR! gyp ERR! find Python checking Python explicitly set from command line or npm configuration
npm ERR! gyp ERR! find Python - "--python=" or "npm config get python" is "/usr/bin/python3"
npm ERR! gyp ERR! find Python - "/usr/bin/python3" is not in PATH or produced an error
npm ERR! gyp ERR! find Python Python is not set from environment variable PYTHON
npm ERR! gyp ERR! find Python checking if "python3" can be used
npm ERR! gyp ERR! find Python - "python3" is not in PATH or produced an error
npm ERR! gyp ERR! find Python checking if "python" can be used
npm ERR! gyp ERR! find Python - "python" is not in PATH or produced an error
npm ERR! gyp ERR! find Python
npm ERR! gyp ERR! find Python **********************************************************
npm ERR! gyp ERR! find Python You need to install the latest version of Python.
npm ERR! gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
npm ERR! gyp ERR! find Python you can try one of the following options:
npm ERR! gyp ERR! find Python - Use the switch --python="/path/to/pythonexecutable"
npm ERR! gyp ERR! find Python   (accepted by both node-gyp and npm)
npm ERR! gyp ERR! find Python - Set the environment variable PYTHON
npm ERR! gyp ERR! find Python - Set the npm configuration variable python:
npm ERR! gyp ERR! find Python   npm config set python "/path/to/pythonexecutable"
npm ERR! gyp ERR! find Python For more information consult the documentation at:
npm ERR! gyp ERR! find Python https://github.com/nodejs/node-gyp#installation
npm ERR! gyp ERR! find Python **********************************************************
npm ERR! gyp ERR! find Python
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Could not find any Python installation to use
npm ERR! gyp ERR! stack     at PythonFinder.fail (/Users/kyeolhan/.nvm/versions/node/v16.14.2/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:330:47)
npm ERR! gyp ERR! stack     at PythonFinder.runChecks (/Users/kyeolhan/.nvm/versions/node/v16.14.2/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:159:21)
npm ERR! gyp ERR! stack     at PythonFinder.<anonymous> (/Users/kyeolhan/.nvm/versions/node/v16.14.2/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:202:16)
npm ERR! gyp ERR! stack     at PythonFinder.execFileCallback (/Users/kyeolhan/.nvm/versions/node/v16.14.2/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:294:16)
npm ERR! gyp ERR! stack     at exithandler (node:child_process:406:5)
npm ERR! gyp ERR! stack     at ChildProcess.errorhandler (node:child_process:418:5)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:526:28)
npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:289:12)
npm ERR! gyp ERR! stack     at onErrorNT (node:internal/child_process:478:16)
npm ERR! gyp ERR! stack     at processTicksAndRejections (node:internal/process/task_queues:83:21)
npm ERR! gyp ERR! System Darwin 21.4.0
npm ERR! gyp ERR! command "/Users/kyeolhan/.nvm/versions/node/v16.14.2/bin/node" "/Users/kyeolhan/.nvm/versions/node/v16.14.2/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd /Users/kyeolhan/ForWork/BackOffceFront/node_modules/deasync
npm ERR! gyp ERR! node -v v16.14.2
npm ERR! gyp ERR! node-gyp -v v9.0.0
npm ERR! gyp ERR! not ok
npm ERR! Build failed

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/kyeolhan/.npm/_logs/2022-03-28T13_05_45_769Z-debug-0.log

我认为 'deasync' 包需要 python 因为只有在 package-lock.json 中包含该包的项目才会出现此错误。

我该如何解决?

我通过将节点版本降级(到 v14.19.1)解决了这个问题。

#reference!