React Native 开发 Windows 客户端

更新日期: 2025-02-02 阅读次数: 22 字数: 638 分类: ReactJS

在春节假期,使用 React Native 成功开发了两款 Android App 之后。我突然想试试是否可以用 RN 开发 Windows 客户端试试,毕竟是微软官方支持的,而且 github 上 star 数也很多,应该是靠谱的。不妨动手一试。

虽然这次尝试以失败告终,但是也算是证明了直接使用 Expo 的脚手架开发 Windows 客户端并不合适。

微软官方文档

https://microsoft.github.io/react-native-windows/docs/getting-started

Could not find MSBuild with VCTools for Visual Studio 17.11.0 or later

执行报错:

> npx react-native run-windows

‼ No public VS release found
 i Trying pre-release VS
 × Could not find MSBuild with VCTools for Visual Studio 17.11.0 or later. Make sure all required components have been installed
 × It is possible your installation is missing required software dependencies. Dependencies can be automatically installed by running node_modules\react-native-windows\scripts\rnw-dependencies.ps1 from an elevated PowerShell prompt.
For more information, go to http://aka.ms/rnw-deps
Command failed. Re-run the command with --logging for more information.

本机 VS 版本

Microsoft Visual Studio Community 2022 (64 位) - Current 版本 17.10.5

升级完,再次执行

> npx react-native run-windows

elevated PowerShell prompt

即使用管理员身份打开 PowerShell

Running PowerShell with elevated privileges (as an administrator) is often necessary for executing certain commands and scripts that require higher permissions.

node_modules\react-native-windows\scripts\rnw-dependencies.ps1

Checking Free space on current drive > 15 GB                        OK
Checking Installed memory >= 16 GB                       Failed (warn)
Checking Windows version >= 10.0.17763.0                            OK
Checking Developer mode is on                                       OK
Checking Long path support is enabled                           Failed

升级 VS 版本

对于 VS 版本低的问题,可以打开 VS,然后升级最新版本即可。

Error: Cannot find module '@react-native/metro-config'

yarn add --dev @react-native/js-polyfills metro-config @react-native/metro-babel-transformer metro-runtime @react-native/metro-config

然后,

yarn start

就能正常启动了。

index.js was not found

最后卡在了这一步,无论是在 VS 中,还是在命令行中,编译都会报这个错。

对 Android 开发环境的影响

在已有的 expo 项目中,增加了 Windows 目录及依赖之后,发现 Android Studio 编译会报错。

去掉相关的 windows 依赖之后,就能正常编译 Android APK 了。。。

想法

一晚上,还是没搞定 react native Windows 的打包,还是等 expo 的官方支持吧。

想了想,react native 项目中使用的大量组件只宣称支持了 Android ios 和 web 端,很少有提及 Windows 端的,所以所以真正要做到支持 Windows,还是有点麻烦。

wails 来开发 Windows 客户端,是个不错的选择。基础逻辑先用 go 命令行写好,然后做界面,沿用 react 组件。毕竟,我发现用 golang 来开发小工具还挺方便的,比 .net 方便多了。

阅读更多 👀

React Native 跨平台应用开发教程

微信关注我哦 👍

大象工具微信公众号

我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式