如何强制 npm 在安装软件包时使用不同的 node-gyp 版本?

wak*_*aka 12 node.js npm node-gyp

我正在使用 Visual Studio 2022 运行 Node 16.13.2 LTS。在安装包期间,会触发 node-gyp 并失败。

npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /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@8.3.0
npm ERR! gyp info using node@16.13.2 | win32 | x64
npm ERR! gyp info find Python using Python version 3.9.4 found at "C:\Users\ahboy\AppData\Local\Programs\Python\Python39\python.exe"
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS msvs_version not set from command line or npm config
npm ERR! gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
npm ERR! gyp ERR! find VS unknown version "undefined" found at "C:\Program Files\Microsoft Visual Studio\2022\Professional"
npm ERR! gyp ERR! find VS could not find a version of Visual Studio 2017 or newer to use
npm ERR! gyp ERR! find VS looking for Visual Studio 2015
npm ERR! gyp ERR! find VS - not found
Run Code Online (Sandbox Code Playgroud)

错误

我去做了一些研究,发现node-gyp@8.3.0不支持VS 2022。只有node-gyp@8.4版本支持。所以我开始尝试让 npm 使用 node-gyp@8.4.0 但没有任何效果,包括来自 node-gyp github 页面的建议来全局安装 node-gyp 并指向 npmrc 文件中的路径。

那么如何才能强制 npm 在安装软件包时使用不同的 node-gyp 版本呢?

Yil*_*maz 0

如果您安装了最新的node-gyp.

在终端(Powershell)和 vscode 终端中检查 node-gyp 的版本。其中一个应该指向 8.4,另一个应该指向 8.3,因此您运行应用程序的终端看起来无法识别 8.4 版本的路径。