npm ERR!: code 127. gyp ERR! stack Error: Command failed

npm ERR!: code 127. gyp ERR! stack Error: Command failed

我在执行 npm install 时遇到以下错误:

$ npm install
npm ERR! code 127
npm ERR! path /home/jesusjimenez/projects/project/node_modules/fibers
npm ERR! command failed
npm ERR! command sh -c node build.js || nodejs build.js
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@3.8.0
npm ERR! gyp info using node@15.12.0 | linux | x64
npm ERR! gyp ERR! configure error 
npm ERR! gyp ERR! stack Error: Command failed: /usr/bin/python3 -c import sys; print "%s.%s.%s" % sys.version_info[:3];
npm ERR! gyp ERR! stack   File "<string>", line 1
npm ERR! gyp ERR! stack     import sys; print "%s.%s.%s" % sys.version_info[:3];
npm ERR! gyp ERR! stack                       ^
npm ERR! gyp ERR! stack SyntaxError: invalid syntax
npm ERR! gyp ERR! stack 
npm ERR! gyp ERR! stack     at ChildProcess.exithandler (node:child_process:326:12)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:369:20)
npm ERR! gyp ERR! stack     at maybeClose (node:internal/child_process:1067:16)
npm ERR! gyp ERR! stack     at Socket.<anonymous> (node:internal/child_process:453:11)
npm ERR! gyp ERR! stack     at Socket.emit (node:events:369:20)
npm ERR! gyp ERR! stack     at Pipe.<anonymous> (node:net:665:12)
npm ERR! gyp ERR! System Linux 5.8.0-48-generic
npm ERR! gyp ERR! command "/usr/bin/node" "/home/jesusjimenez/projects/project/node_modules/.bin/node-gyp" "rebuild" "--release"
npm ERR! gyp ERR! cwd /home/jesusjimenez/projects/project/node_modules/fibers
npm ERR! gyp ERR! node -v v15.12.0
npm ERR! gyp ERR! node-gyp -v v3.8.0
npm ERR! gyp ERR! not ok 
npm ERR! node-gyp exited with code: 1
npm ERR! Please make sure you are using a supported platform and node version. If you
npm ERR! would like to compile fibers on this machine please make sure you have setup your
npm ERR! build environment--
npm ERR! Windows + OS X instructions here: https://github.com/nodejs/node-gyp
npm ERR! Ubuntu users please run: `sudo apt-get install g++ build-essential`
npm ERR! RHEL users please run: `yum install gcc-c++` and `yum groupinstall 'Development Tools'` 
npm ERR! Alpine users please run: `sudo apk add python make g++`
npm ERR! sh: 1: nodejs: not found

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/jesusjimenez/.npm/_logs/2021-03-30T21_32_02_097Z-debug.log

我不完全确定是什么破坏了安装,如果纤维或某种方式 python SyntaxError: invalid syntax 但是,尝试单独安装光纤会引发相同的错误。 npm i -g fibers 没有修复,也没有改变 npm configure set python /path/to/python。暂时删除纤维会继续抛出相同的 Python SyntaxError: invalid syntax 错误,这次是另一个 node_module.

这是此问题的解决方案

解释:

注意 NPM 错误日志的这一部分:

npm ERR! gyp ERR! node-gyp -v v3.8.0

这意味着您的代码正在尝试安装 3.8.0 版的 node-gyp

可能发生了什么(在我的情况下)你有一个错误的 NodeJS 版本,用于node-gyp。

我搜索了适合node-gyp 3.8.0的NodeJS版本,答案如下。

解法:

  1. 卸载当前的 NodeJS 版本
  2. 安装 NodeJS 版本 6.17.1(如果你需要多个 NodeJS 版本使用 NVM 工具(Instructions For Windows/Linux/MacOS)https://www.nubo.eu/Install-Multiple-Node-Versions-On-Windows/
  3. 运行 npm install 再次进入您的项目

我在没有对节点版本进行任何更改的情况下遇到了 same/similar 问题。我讨厌成为那个人,但经过一天的挣扎,我决定重新启动我的机器。那修好了...

问题是 node-gyp 尝试使用 Python 2 语法构建:print "%s.%s.%s" % sys.version_info[:3]; 这可能会引发错误,因为您的计算机上有 Python 3

这通常发生在安装与系统上当前 Node 版本不兼容的过时 npm 模块时。尝试安装 Python 2 并将其设置为路径,或者在 package.json 中找到有问题的模块,然后将版本号升级到与您当前的 Node 版本兼容的版本。