react-native搭建开发环境

520 阅读1分钟

官方文档搭建步骤:reactnative.cn/docs/enviro…

遇到的问题

1、执行npx react-native init AwesomeProject报如下错误:

## npm ERR! Could not install from "Files\nodejs\node_cache_npx\13392" 
as it does not contain a package.json file.

原因分析:node路径带有空格

执行如下命令打开.npmrc文件,把文件名称的空格用~代替

npm config edit

比如/Program Files/xxx 更改为/Program~Files/xxx

以~代替空格是window支持的一种写法。更改之后就可以正常创建项目了。

2、执行npx react-native init AwesomeProject 安装依赖的过程中发生错误:

原因分析:node版本问题

解决方法:把node更改为这几个版本"^12.22.0 || ^14.17.0 || >=16.0.0"

可到官网下载符合要求的node稳定版:nodejs.org/zh-cn/

error eslint-plugin-jest@26.9.0: The engine "node" is incompatible with this module. 
Expected version "^12.22.0 || ^14.17.0 || >=16.0.0". Got "14.16.0" error Found incompatible module

3、安装的版本react-native@0.71.4,jdk1.8时会出现以下错误

并且低版本Android Studio不支持jdk11,所以Android Studio版本太低的需要先升级版本 不然想要切换jdk11时会弹出:please choose a valid  jdk 8 directory

ERROR: Cause: com/facebook/react/ReactPlugin 
has been compiled by a more recent version of the Java Runtime (class file version 55.0),
this version of the Java Runtime only recognizes class file versions up to 52.0

低于 0.67 版本的 React Native 需要 JDK 1.8 版本(官方也称 8 版本)。

其他 React Native 需要安装 Java Development Kit [JDK] 11

最终版本:react-native@0.71.4、node@18、Java Development Kit [JDK] 11 可以去TemurinOracle JDK上下载(后者下载需注册登录)。**