React Native升级至0.61.5后构建报错:Cannot read properties of undefined (reading 'sync')
问题
将React Native项目从0.59.9版本升级至0.61.5版本,已更新package.json中的react-native和react依赖包。执行构建命令react-native run-android --variant=alphaRelease时,出现以下错误:
Cannot read properties of undefined (reading 'sync') TypeError: Cannot read properties of undefined (reading 'sync') at findProject (/Users/widhi.nugraha/projects/kbru/v1/fo-apps-v1/node_modules/@react-native-community/cli-platform-ios/build/config/findProject.js:67:35)
此前按照React Native CLI提示解除了所有项目包的链接操作,完成后就出现了该错误。项目的android/build.gradle文件内容如下:
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext { buildToolsVersion = "29.0.1" minSdkVersion = 16 compileSdkVersion = 29 targetSdkVersion = 30 supportLibVersion = "28.0.0" kotlinVersion = "1.9.10" kotlin_version = "1.9.10" } ext.kotlin_version = "1.9.1i0" repositories { google() jcenter() maven { url 'https://maven.fabric.io/public' } // maven { // url "https://repo.spring.io/plugins-release/" // } mavenCentral() } dependencies { classpath("com.android.tools.build:gradle:7.0.4") // classpath("com.android.tools.build:gradle:3.4.0") classpath('com.google.gms:google-services:4.3.8') // classpath('com.google.gms:google-services:4.2.0') // classpath 'com.google.firebase:firebase-plugins:1.1.1' classpath 'com.google.firebase:perf-plugin:1.1.5' // classpath 'io.fabric.tools:gradle:1.+' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { mavenLocal() google() jcenter() maven { // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm url "$rootDir/../node_modules/react-native/android" } // ADD THIS // maven { url 'https://maven.google.com' } // ADD THIS maven { url "https://jitpack.io" } // maven { // url "https://repo.spring.io/plugins-release" // } maven { url "https://maven.google.com" } mavenCentral() // Add the midtrans repository into the list of repositories // maven { url "http://dl.bintray.com/pt-midtrans/maven" } } } }
解决方案
1. 修复CLI依赖版本不兼容问题
- 删除
node_modules和锁文件:rm -rf node_modules package-lock.json yarn.lock - 安装RN 0.61.5适配的CLI版本:
npm install --save-dev @react-native-community/cli@4.14.0
2. 修正android/build.gradle配置错误
- 删除重复且拼写错误的
ext.kotlin_version = "1.9.1i0"行,保留ext块内的kotlinVersion = "1.9.10"配置即可。 - 替换gradle构建工具版本:RN 0.61.5适配
3.5.3版本,而非7.0.4,修改buildscript依赖:classpath("com.android.tools.build:gradle:3.5.3") - 删除文件末尾多余的闭合括号
},修复语法错误。
3. 处理unlink后的项目遗留问题
- 进入Android目录执行清理:
cd android && ./gradlew clean - 重新安装依赖并同步项目:
(RN 0.60+支持自动链接,若CLI提示无需手动链接则跳过npm installreact-native link命令)
4. 重新构建项目
react-native run-android --variant=alphaRelease
内容的提问来源于stack exchange,提问作者dytra
相关产品推荐
相关产品推荐

