noge-gyp构建项目踩坑记录

开发环境
系统: win11
node: 19.7.0
npm: 8.3.2
node-gyp: 10.0.2

可以不使用windows-build-tools来安装构建工具,手动进行安装

我这边用windows-build-tools安装时候会提示

'process.env' only accepts a configurable, writable, and enumerable data descriptor.

查了资料后加上dd_clint.log这个文件也没有用

再加上这个构建工具中用的Python还是2.7版本的,太旧了,所以选择手动安装构建工具

1、安装Visual Studio 2022

需要勾选"使用C++的桌面开发",“MSVC v143 - VS 2022 C++ x64/x86 生成工具(最新)”,“Windows 11 SDK(10.0.22000.0)”,这个SDK后面的数字怎么选下面会介绍
image-20240829101649626
image-20240829101743932
image-20240829102020482

查看Windows版本

在搜索栏输入"系统信息"

image-20240829102242367
image-20240829102232006

我的系统是Win11,版本是10.0.22000,对应的SDK就是Windows 11 SDK(10.0.22000.0)

设置环境变量

下载完后添加环境变量VCINSTALLDIR,路径设置图中这个

image-20240829102523310

2、安装node-gyp

安装命令:npm install -g node-gyp

因为我用的是nvm来管理多个nodejs,安装完后找不到node-gyp,还需要加上环境变量(我的电脑>右键>属性>高级系统设置>环境变量>系统变量>Path>编辑>新建)

E:\nvm\v19.7.0\node_global

3、node-gyp构建模块

默认node-gyp下载编译需要的头文件是从nodejs官网下载的经常会因为网络问题下载失败。

FetchError: request to https://nodejs.org/download/release/v19.7.0.....

可以在npmrc中设置

disturl = "https://npmmirror.com/mirrors/node"

或者在执行node-gyp命令的时候加上以下参数

--disturl=https://npmmirror.com/mirrors/node

通过淘宝的镜像网站下载头文件,可能某些版本node的头文件淘宝镜像没有,可以从其他网站镜像寻找配置

开始构建模块

node-gyp configure --disturl=https://npmmirror.com/mirrors/node

问题1:msvs_version not set,VCINSTALLDIR not set,not found Visual Studio

执行指令

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

执行完一定要检查一下

$ npm config get msvs_version
2022
$ echo %VCINSTALLDIR%
E:\Microsoft Visual Studio\2022\Community\VC
问题2:msvs_version not set,VCINSTALLDIR not set,not found Visual Studio

大意是msvs_version变量取到了,但是VCINSTALLDIR 还是没有set。

解决办法:

  1. 手动设置系统的环境变量(我的电脑>右键>属性>高级系统设置>环境变量>新建系统变量)
  2. vscode所属的terminal,echo %VCINSTALLDIR%返回的值都不符合预期。
    但windows自带的cmd.exe是可以取到VCINSTALLDIR的值。
问题3:unknown version “undefined” found

node-gyp寻找Visual Studio的文件是下面这个

E:\nvm\v19.7.0\node_global\node_modules\node-gyp\lib\find-visualstudio.js

用vscode打开,加入以下代码后调试运行(勾选上捕获异常)

debugger;
// 第一个参数是node的版本
// 第二个参数是Visual Studio的版本
findVisualStudio('19.7.0', '2022', (result) => { 
   console.log(result); 
});

image-20240829105440267

查找Visual Studio命令的构建在findNewVS这个函数下,最后发现是执行命令报错,导致JSON.parse(stdout),这一行命令在解析的时候出现问题

手动把构建的命令复制出来,在PowerShell执行发现是从环境变量中的用户变量lib读取到了不存在的路径

E:\Microsoft Visual Studio\VC98\mfc\lib
E:\Microsoft Visual Studio\VC98\lib

这个是因为我之前安装过Visual C++,后续文件夹删除了,但是环境变量没有删除,导致node-gyp寻找Visual Studio时出错,所以将环境变量删除即可

问题4:does not match this Visual Studio Command Prompt

解决办法1: 在 C:\Users\Admin\.npmrc (找不到这个文件的用Everything搜一下)文件中添加上msvs_version=2022

我的.npmrc文件设置如下

cache=E:\nodejs\node_cache
msvs_version=2022
node_gyp=E:\nodejs\node_global\node_modules\node-gyp\bin\node-gyp.js
prefix=E:\nodejs\node_global
registry=https://registry.npm.taobao.org/
sass_binary_site=https://npmmirror.com/mirrors/node-sass
strict-ssl=false

解决办法2: 在构建的时候加上 --msvs_version=2022,比如:node-gyp configure --msvs_version=2022

问题5:Could not find any Visual Studio installation to use

在安装了Windows11 SDK后出现这个问题是因为node-gyp没有找到Windows11 SDK

node-gyp寻找SDK的文件是下面这个

E:\nvm\v19.7.0\node_global\node_modules\node-gyp\lib\find-visualstudio.js

里面有一个函数getSDK,将其修改即可

// Helper - process Windows SDK information
  getSDK (info) {
    const win8SDK = 'Microsoft.VisualStudio.Component.Windows81SDK'
    const win10SDKPrefix = 'Microsoft.VisualStudio.Component.Windows10SDK.'
    const win11SDKPrefix = 'Microsoft.VisualStudio.Component.Windows11SDK.'

    let Win10or11SDKVer = 0
    info.packages.forEach((pkg) => {
      if (!pkg.startsWith(win10SDKPrefix) && !pkg.startsWith(win11SDKPrefix)) {
        return
      }
      const parts = pkg.split('.')
      if (parts.length > 5 && parts[5] !== 'Desktop') {
        this.log.silly('- ignoring non-Desktop Win10/11SDK:', pkg)
        return
      }
      const foundSdkVer = parseInt(parts[4], 10)
      if (isNaN(foundSdkVer)) {
        // Microsoft.VisualStudio.Component.Windows10SDK.IpOverUsb
        this.log.silly('- failed to parse Win10/11SDK number:', pkg)
        return
      }
      this.log.silly('- found Win10/11SDK:', foundSdkVer)
      Win10or11SDKVer = Math.max(Win10or11SDKVer, foundSdkVer)
    })

    if (Win10or11SDKVer !== 0) {
      return `10.0.${Win10or11SDKVer}.0`
    } else if (info.packages.indexOf(win8SDK) !== -1) {
      this.log.silly('- found Win8SDK')
      return '8.1'
    }
    return 

参考资料:

  1. https://juejin.cn/post/7132125416034140173
  2. https://stackoverflow.com/questions/70315519/node-gyp-error-could-not-find-any-visual-studio-installation-to-use
  3. https://blog.csdn.net/qq_36888550/article/details/133287592
  4. https://blog.csdn.net/jhlovett/article/details/3969791
  5. https://github.com/nodejs/node-gyp/pull/2565
  6. https://juejin.cn/post/7042123168722452516
  • 8
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值