使用npm install时,Node-gyp版本不同

3

当我运行时

npm install -g ffi -verbose

出现错误,日志显示node-gyp版本为8.2.0:

...
npm ERR! D:\Programs\NodeJS\node_global_modules\node_modules\ffi\node_modules\ref\src\binding.cc(644,8): warning C4996: 'Nan::ForceSet': ������Ϊ�ѷ�� [D:\Programs\NodeJS\node_global_modules\node_modules\ffi\node_modules\ref\build\binding.vcxproj]
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@8.2.0
npm ERR! gyp info using node@16.13.0 | win32 | x64
npm ERR! gyp info find Python using Python version 3.8.8 found at "D:\Programs\Anaconda3\python.exe"
npm ERR! gyp info find VS using VS2019 (16.10.31515.178) found at:
npm ERR! gyp info find VS "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community"
npm ERR! gyp info find VS run with --verbose for detailed information
npm ERR! gyp info spawn D:\Programs\Anaconda3\python.exe
...

这与运行node-gyp -v时我看到的不同:

C:\Users\Administrator>node-gyp -v
v8.4.1

这可能解释了为什么它使用Python 3.8和MSVC 2019,即使我已经运行:

npm config set python python2.7
npm config set msvs_version 2015

我不知道为什么我的电脑上有另一个node-gyp,也不知道如何使用正确的版本。

3个回答

2

非常有趣的发现,我想在这里分享一下。 我也遇到了类似的情况,我的全局node-gyp是9.0.0,我也像这里提到的那样更新了npm node-gyp。 我正在使用

  • node 16.2.0
  • npm 7.15.1
  • node-gyp 9.0.0

当我运行npm install @abandonware/bleno时,我收到了以下错误,并认为是npm使用了错误的node-gyp版本。

416 error code 1
417 error path D:\VSCode Projects\react-native-ble-read-peripheral\food-bank-peripheral\node_modules\@abandonware\bleno
418 error command failed
419 error command C:\WINDOWS\system32\cmd.exe /d /s /c node-gyp rebuild
420 error gyp info it worked if it ends with ok
420 error gyp info using node-gyp@3.8.0
420 error gyp info using node@16.2.0 | win32 | x64
420 error gyp ERR! configure error
420 error gyp ERR! stack Error: Command failed: C:\Python39\python.EXE -c import sys; print "%s.%s.%s" % sys.version_info[:3];
420 error gyp ERR! stack   File "<string>", line 1
420 error gyp ERR! stack     import sys; print "%s.%s.%s" % sys.version_info[:3];
420 error gyp ERR! stack                       ^
420 error gyp ERR! stack SyntaxError: invalid syntax
420 error gyp ERR! stack
420 error gyp ERR! stack     at ChildProcess.exithandler (node:child_process:326:12)
420 error gyp ERR! stack     at ChildProcess.emit (node:events:365:28)
420 error gyp ERR! stack     at maybeClose (node:internal/child_process:1067:16)
420 error gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5)
420 error gyp ERR! System Windows_NT 10.0.19044
420 error gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "D:\\VSCode Projects\\react-native-ble-read-peripheral\\food-bank-peripheral\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
420 error gyp ERR! cwd D:\VSCode Projects\react-native-ble-read-peripheral\food-bank-peripheral\node_modules\@abandonware\bleno
420 error gyp ERR! node -v v16.2.0
420 error gyp ERR! node-gyp -v v3.8.0
420 error gyp ERR! not ok
421 verbose exit 1

但是后来我看到了这个评论这里这里,于是我回去查看日志文件,发现实际错误是从这里(第409行)开始的。
409 verbose stack Error: command failed
409 verbose stack     at ChildProcess.<anonymous> (C:\Users\Ramlah\AppData\Roaming\nvm\v16.2.0\node_modules\npm\node_modules\@npmcli\run-script\node_modules\@npmcli\promise-spawn\index.js:64:27)
409 verbose stack     at ChildProcess.emit (node:events:365:28)
409 verbose stack     at maybeClose (node:internal/child_process:1067:16)
409 verbose stack     at Socket.<anonymous> (node:internal/child_process:453:11)
409 verbose stack     at Socket.emit (node:events:365:28)
409 verbose stack     at Pipe.<anonymous> (node:net:661:12)
410 verbose pkgid @abandonware/bleno@0.5.1-3
411 verbose cwd D:\VSCode Projects\react-native-ble-read-peripheral\food-bank-peripheral
412 verbose Windows_NT 10.0.19044
413 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install"
414 verbose node v16.2.0
415 verbose npm  v7.15.1
416 error code 1
417 error path D:\VSCode Projects\react-native-ble-read-peripheral\food-bank-peripheral\node_modules\@abandonware\bleno
418 error command failed
419 error command C:\WINDOWS\system32\cmd.exe /d /s /c node-gyp rebuild

所以我使用 nvm 将我的 node 版本从 16.2.0 降级到了 14.17.1(这也使我的 npm 版本降级到了 6.14.13),然后哇!我成功安装了 npm。


2
这是因为 npm 捆绑了自己的内部副本 node-gyp。这个内部副本独立于通过 npm install -g node-gyp 安装的任何全局安装的 node-gyp 副本。
这意味着,虽然 node-gyp 默认情况下不会安装到您的 $PATH 中,但 npm 仍然保留自己的副本,在您尝试 npm install 本地插件时调用它。
有时候,您可能需要将 npm 的内部 node-gyp 更新到比已安装的版本更新的版本。简单的 npm install -g node-gyp 不起作用,因为 npm 仍然会使用其内部副本而不是全局副本。
所以,我们需要这样做:

npm 版本

首先,我们需要知道您的 npm 版本:
npm --version

Linux、macOS、Solaris 等

Unix 很容易。只需运行以下命令。

如果您的 npm 版本为 78,请执行以下操作:

$ npm explore npm/node_modules/@npmcli/run-script -g -- npm_config_global=false npm install node-gyp@latest

如果你的npm版本小于7,则执行以下操作:

$ npm explore npm/node_modules/npm-lifecycle -g -- npm install node-gyp@latest

如果命令执行时出现权限错误,请尝试使用sudo,然后执行该命令。
Windows操作系统稍微复杂一些,因为npm可能安装在“Program Files”目录中,在当前的Windows系统下需要管理员权限才能进行修改。因此,请在“以管理员身份运行”的cmd.exe中执行以下命令:
首先,我们需要找到node的位置。如果你还不知道node.exe的安装位置,那么请执行以下命令:
$ where node

现在 cd 到包含 node.exe 的目录中,例如:
$ cd "C:\Program Files\nodejs"

如果你的 npm 版本是 78,请执行以下操作:

cd node_modules\npm\node_modules\@npmcli\run-script

如果你的 npm 版本小于 7,请执行以下操作:

cd node_modules\npm\node_modules\npm-lifecycle

运行以下命令以完成:

$ npm install node-gyp@latest

来源:https://github.com/nodejs/node-gyp/blob/main/docs/Force-npm-to-use-global-node-gyp.md


2
我已经尝试了所有方法,包括使用这个和node-gyp,但仍然回到3.8.0并失败,因为它有一个bug。我已经删除并替换了我能找到的所有更新的node-gyps,但它总是会再次出现。这似乎与丑陋的sass包有关。不知道该往哪里求助! - Brian Reinhold
这解决了我所有的问题。非常感谢你! - Eyal Gerber

1

输入where node-gyp

你可能已经全局安装了node-gyp,也可能每个包都带有自己的版本,这可能是ffi的情况。 PATH决定哪个版本会运行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接