【已解决】node-gyp 安装报错

省流阅读

遇到问题node-gyp 安装报错,提示要安装vs,并开启Desktop development with C++,但总是提示vs版本不对
最终解决方法如下:

# 0 分析问题:当前npm版本为v14.16.0,适合python v2.7和VS2017,不能太高也不能太低
# 1 取巧安装vs2017
npm install --global --production windows-build-tools
# 2 锁定vs版本2017
npm config set msvs_version 2017
# 3 解决,运行程序
npm ci

九转十八弯的解决过程如下,希望给同样困扰的小伙伴一点思路
PS 这个问题困扰我大半年了,最终重装了居然还在,一怒之下终于解决了



遇到问题:msvs_version not set,VCINSTALLDIR not set,not found Visual Studio

系统:window11
npm ci的时候遇到的问题,mac电脑无问题

gyp ERR! find VS msvs_version not set from command line or npm config
gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
gyp ERR! find VS could not find a version of Visual Studio 2017 or newer to use
gyp ERR! find VS looking for Visual Studio 2015
gyp ERR! find VS - not found
gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
gyp ERR! find VS **************************************************************
gyp ERR! find VS You need to install the latest version of Visual Studio
gyp ERR! find VS including the "Desktop development with C++" workload.
gyp ERR! find VS For more information consult the documentation at:
gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows
gyp ERR! find VS **************************************************************

尝试解决一

搜索百科给了太多奇怪的答案,特地去搜了ai,给的方法如下:

这个错误是因为在运行Node.js项目时,没有设置正确的Visual Studio版本。解决方法如下:

  1. 确保已经安装了Visual Studio,并且安装了"Desktop development with C++" workload。如果没有安装,请根据错误提示中提供的链接下载并安装最新版本的Visual Studio,并确保选择了正确的工作负载。

  2. 确保已经设置了正确的环境变量。在命令行中运行以下命令来设置msvs_versionVCINSTALLDIR环境变量:

   npm config set msvs_version 2022
   set VCINSTALLDIR <Visual Studio安装路径>

其中<Visual Studio安装路径>是Visual Studio的安装路径,例如C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC

  1. 如果仍然遇到问题,可以尝试重新安装Node.js,并确保选择了正确的Visual Studio版本。

  2. 如果以上方法都无效,可以尝试使用其他版本的Node.js,或者使用其他构建工具代替Node.js的默认构建工具。

希望以上方法能够帮助您解决问题。如果问题仍然存在,请提供更多详细信息,以便我们能够更好地帮助您解决问题。


尝试上述解决方法

  1. 安装vs2022
    检查系统确实没有vs,于是下载了最新版的vs2022,安装的时候特地勾选了Desktop development with C++
  2. 执行指令
   npm config set msvs_version 2022
   set VCINSTALLDIR <Visual Studio安装路径>

执行完一定要检查一下

$ npm config get msvs_version
2022
$ echo %VCINSTALLDIR%
E:\app-code\Microsoft Visual Studio\2022\Community\VC

新问题:VCINSTALLDIR not set, not running in VS Command

新的报错如下:

gyp ERR! find VS msvs_version was set from command line or npm config
gyp ERR! find VS - looking for Visual Studio version 2022
gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
gyp ERR! find VS unknown version "undefined" found at "E:\app-code\Microsoft Visual Studio\2022\Community"

大意是msvs_version变量去到了,但是VCINSTALLDIR 还是没有set。
执行检查命令,确实拿不到VCINSTALLDIR 值:

$ npm config get msvs_version
2022
$ echo %VCINSTALLDIR%
%VCINSTALLDIR%

尝试解决二

  1. 手动设置系统的环境变量(我的电脑>右键>属性>高级系统设置>环境变量>新建系统变量)

  2. vscode所属的terminal,echo %VCINSTALLDIR%返回的值都不符合预期。
    但window自带的cmd.exe是可以取到VCINSTALLDIR的值。


新问题:unknown version “undefined” found

gyp ERR! find VS msvs_version was set from command line or npm config
gyp ERR! find VS - looking for Visual Studio version 2022
gyp ERR! find VS running in VS Command Prompt, installation path is:
gyp ERR! find VS "E:\app-code\Microsoft Visual Studio\2022\Community"
gyp ERR! find VS - will only use this version
gyp ERR! find VS unknown version "undefined" found at "E:\app-code\Microsoft Visual Studio\2022\Community"

问题翻译:

msvs_version 指定vs版本2022
VS Command Prompt 也找到了vs2022的安装目录
但无法识别版本

因为仓库指定npm版本是14.16.0,比较老的npm了。有可能和vs2022不太适配
强升到npm v18还需要安装python v3,于是我把vs版本降低到了2017

解决方法三

重新安装vs2017(因为实在找不到vs2017的安装包,想了个取巧的办法)

# 取巧安装vs2017
npm install --global --production windows-build-tools

还是有报错

gyp ERR! find VS msvs_version was set from command line or npm config:指定了 msvs_version 参数,可能是通过命令行或 npm 配置设置的。
gyp ERR! find VS - looking for Visual Studio version 2022:正在寻找 Visual Studio 2022 版本。
gyp ERR! find VS running in VS Command Prompt, installation path is::正在使用 VS 命令提示符运行,安装路径为指定的路径。
gyp ERR! find VS "E:\app-code\Microsoft Visual Studio\2022\Community":Visual Studio 2022 的安装路径。
gyp ERR! find VS - will only use this version:将只使用找到的 Visual Studio 2022 版本。
gyp ERR! find VS unknown version "undefined" found at "E:\app-code\Microsoft Visual Studio\2022\Community":在指定的路径下找到了未知版本的 Visual Studio 2022。
gyp ERR! find VS checking VS2017 (15.9.34031.82) found at::检查找到的 Visual Studio 2017 的版本信息。
gyp ERR! find VS "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools":Visual Studio 2017 的安装路径。
gyp ERR! find VS - found "Visual Studio C++ core features":找到了 Visual Studio C++ 核心功能。
gyp ERR! find VS - found VC++ toolset: v141:找到了 VC++ 工具集的版本。
gyp ERR! find VS - found Windows SDK: 10.0.17763.0:找到了 Windows SDK 的版本。
gyp ERR! find VS - msvs_version does not match this version:msvs_version 参数与找到的版本不匹配。
gyp ERR! find VS could not find a version of Visual Studio 2017 or newer to use:无法找到可用的 Visual Studio 2017 或更新的版本。

通过解析可以发现vs2017满足一切要求,只是版本锁定了2022,把之前设定的版本改成2017即可

# 修改指定vs版本
npm config set msvs_version 2017
# 手动删除环境变量 VCINSTALLDIR

成功解决

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值