简体   繁体   English

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

[英]How to force npm to use different node-gyp version when installing packages?

I am running Node 16.13.2 LTS with Visual Studio 2022. During the installation of a package, node-gyp is triggered and fails.我正在使用 Visual Studio 2022 运行 Node 16.13.2 LTS。在安装 package 期间,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

错误

I went to do some research and found out that node-gyp@8.3.0 does not support VS 2022. Only version node-gyp@8.4 does.我去做了一些研究,发现 node-gyp@8.3.0 不支持 VS 2022。只有版本 node-gyp@8.4 支持。 So I started to try to get npm to use node-gyp@8.4.0 but nothing works including this advice from node-gyp github page to install node-gyp globally and point the path in npmrc file.所以我开始尝试让 npm 使用 node-gyp@8.4.0 但没有任何效果,包括来自 node- gyp github 页面的此建议,以全局安装 node-gyp 并指向 npmrc 文件中的路径。

So what can be done to force npm to use different node-gyp versions when installing packages?那么有什么办法可以强制npm在安装包的时候使用不同的node-gyp版本呢?

I had a similar issue, what solved it for me was using nvm: Node Version Manager to install Node version 14, alongside the latest one.我有一个类似的问题,为我解决的问题是使用 nvm: Node Version Manager 来安装 Node 版本 14,以及最新的版本。 The repository I was cloning had a lot of dependencies.我正在克隆的存储库有很多依赖项。 Also, I did not need to be on the bleeding edge of progress with the latest Node version.此外,我不需要在最新的 Node 版本上处于进展的最前沿。

Here is further resource on how to install and use nvm: Installing nvm on Windows .以下是有关如何安装和使用 nvm 的更多资源: 在 Windows 上安装 nvm

If you installed latest node-gyp .如果您安装了最新node-gyp

Check the version of node-gyp both in terminal (Powershell) and vscode terminal.在终端(Powershell)和 vscode 终端中检查 node-gyp 的版本。 One of them should be pointing to the 8.4 and another one to 8.3 So the terminal that you are running the app, looks like is not recognizing the path of 8.4 version.其中一个应该指向 8.4,另一个指向 8.3 因此,您运行应用程序的终端看起来无法识别 8.4 版本的路径。

What does the package.json in the package your installing show for the node-gyp dependency version? package 中的 package 中的 package.json 对 node-gyp 依赖版本有什么影响? Also look at all subdependencies of those dependencies to see if any of those reference version 8.3.还要查看这些依赖项的所有子依赖项,看看是否有任何参考版本 8.3。 I've ran into this same issue as well on windows.我在 windows 上也遇到了同样的问题。 If you can't get your version to update, you can also install visual studio build tools 2017 and set your msvs_version to 2017 and it should pick that up for you.如果您无法更新您的版本,您还可以安装 Visual Studio 构建工具 2017 并将您的 msvs_version 设置为 2017,它应该会为您选择。 No need to install the full vs2017, just the build tools will do fine.无需安装完整的 vs2017,只需构建工具即可。

Declare your dependency on the newer version of node-gyp in your package.json file.在 package.json 文件中声明您对新版本 node-gyp 的依赖。 Add this to your dependency block:将此添加到您的依赖项块中:

 "node-gyp"              : "^8.4.1",

I did exactly this about an hour ago to get our github actions CI tests working since the switch of 'windows-latest' became windows server 2022.大约一个小时前,我正是这样做的,以使我们的 github 动作 CI 测试正常工作,因为“windows-latest”的开关变成了 windows 服务器 2022。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM