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

news/2024/7/10 0:47:53 标签: vue, npm, node-gyp, Visual Studio

省流阅读

遇到问题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
  • 尝试解决一
  • 新问题:VCINSTALLDIR not set, not running in VS Command
  • 尝试解决二
  • 新问题:unknown version "undefined" found
  • 解决方法三


遇到问题: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

成功解决


http://www.niftyadmin.cn/n/5050749.html

相关文章

python设置全局代理

代理的种类&#xff1a; 代理分为http代理和socks代理&#xff01;&#xff01;&#xff01; python在设置代理的时候分两种情况&#xff0c; 第一种是只支持http代理、https代理的&#xff0c;那么就要写如下的代码在文件最前面&#xff1a; import osos.environ["ht…

51单片机自行车码表 速度里程计霍尔测速模拟电机设计

一、系统方案 本设计采用51单片机作为主控器&#xff0c;霍尔测速&#xff0c;数码管显示速度及里程数。 二、硬件设计 原理图如下&#xff1a; 三、单片机软件设计 1、首先是系统初始化 void init() { IT01; //INT0负跳变触发 TMOD0x01;//定时器工作于方式1 TH00x3c; //5…

设计模式——2. 工厂模式

1. 说明 工厂模式(Factory Pattern)是一种创建型设计模式,它提供了一种创建对象的方式,而无需直接暴露对象的创建逻辑。工厂模式将对象的实例化过程封装在一个工厂类中,使客户端代码与具体对象的创建解耦,从而提高了代码的可维护性和灵活性。 工厂模式通常有以下几种变…

【Python】自动完成手写字体图片贴入以及盖章工具

简介 该工具完成了如下功能&#xff1a; 1.将文字转换为手写体填入到模板文件中 2.自动将文字转换为盖章格式填入到模板文件中 3.字体格式可以替换 4.有配置文件进行扩展功能 功能模块 1.界面模块 import sys from PyQt5.QtWidgets import QApplication, QMessageBox, QWid…

Linux内核启动流程-第二阶段start_kernel 函数

一. Linux内核启动 上一篇文章简单介绍了 Linux内核启动的第一阶段&#xff0c;即执行汇编流程。 本文简单了解一下&#xff0c;Linux内核启动的第二阶段&#xff1a;start_kernel函数&#xff0c;这是一个 C 函数。 本文续上一篇文章的学习&#xff0c;地址如下&#xff1a;…

springboot 捕获数据库唯一索引导致的异常

在一些业务场景中,需要保证数据的唯一性,一般情况下,我们会先到数据库中去查询是否存在,再去判断是否可以插入新的数据.如果是在高并发的情况下,可能还是会出现重复的情况.这时候可能就需要用到锁.也可以在数据库中设置唯一索引. 如果使用唯一索引,在插入相同数据的情况下会抛出…

【设计模式】四、工厂模式

文章目录 概述工厂模式简单工厂模式&#xff1a;工厂方法模式抽象工厂模式小结 概述工厂模式 传统方式&#xff1a; 简单工厂模式&#xff1a; 简单工厂模式的设计方案: 定义一个可以实例化 Pizaa 对象的类&#xff0c;封装创建对象的代码。 存在的问题&#xff1a; 简单工厂…

18661 2020 金属卤化物灯 钪钠系列 性能要求 学习记录

声明 本文是学习GB-T 18661-2020 金属卤化物灯 钪钠系列 性能要求. 而整理的学习笔记,分享出来希望更多人受益,如果存在侵权请及时联系我们 1 范围 本标准规定了钪钠系列金属卤化物灯的分类与命名、主要尺寸、基本参数、技术要求、试验方法、检 验规则、标志、包装、运输和…