解决Windows 11运行React Native报错node-gyp需要Visual Studio

Resolve the error of node-gyp requiring Visual Studio when running React Native on Windows 11.

Posted by byron han on August 6, 2024

背景

鸿蒙HarmonyOS时支持的React Native版本为0.72.5,所以项目整体做了升级,但在windows 11系统上遇到node-gyp相关报错,以下都是在无网络下安装

  1. windows 11
  2. node v18.20.1

node-lib和node-headers缺失

下载:

node-headers node-lib

配置变量

编辑~/.npmrc文件

nodedir=c:\node\node-v18.20.1-win-x64\node-v18.20.1

报错需要Visual Studio

1
2
3
4
5
6
7
8
npm ERR! gyp ERR! find VS msvs_version was set from command line or npm config
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS You need to install the latest version of Visual Studio
npm ERR! gyp ERR! find VS including the "Desktop development with C++" workload.
npm ERR! gyp ERR! find VS For more information consult the documentation at:
npm ERR! gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows
npm ERR! gyp ERR! find VS **************************************************************

离线安装Visual Studio 2022

1.脱机安装 Visual Studio 2.仅离线安装C++

1
vs_enterprise.exe --layout c:\localVSlayout --add Microsoft.VisualStudio.Workload.NativeDesktop --includeRecommended --includeOptional --lang en-US

3.打包下载好的zip包到电脑,执行vs_setup.exe 即可 4.配置环境变量 GYP_MSVS_VERSION=2022