Expo构建Android应用Metro报错:Cannot read properties of undefined (reading 'match')
Expo + React Native 构建报错:TypeError: Cannot read properties of undefined (reading 'match')
环境信息
- Expo SDK: 44.0.0
- React Native: 0.64.3
- React: 17.0.1
问题现象
应用此前运行正常,构建时先触发Metro多项配置验证警告,随后抛出核心错误,最终导致Gradle构建失败。核心错误为TypeError: Cannot read properties of undefined (reading 'match'),报错位置在@react-native-community/cli依赖的metro包的countLines方法中。尝试调整Metro配置未解决问题,怀疑@react-native-community相关依赖存在版本兼容问题。
完整报错日志
Task :app:bundleReleaseJsAndAssets warning: the transform cache was reset. [stderr] ● Validation Warning: [stderr] Unknown option "server.experimentalImportBundleSupport" with value false was found. [stderr] This is probably a typing mistake. Fixing it will remove this message. [stderr] ● Validation Warning: [stderr] Unknown option "server.unstable_serverRoot" with value null was found. [stderr] This is probably a typing mistake. Fixing it will remove this message. [stderr] ● Validation Warning: [stderr] Unknown option "watcher" with value {"additionalExts": ["cjs", "mjs"], "healthCheck": {"enabled": false, "filePrefix": ".metro-health-check", "interval": 30000, "timeout": 5000}, "unstable_preferTreeFS": false, "watchman": {"deferStates": ["hg.update"]}} was found. [stderr] This is probably a typing mistake. Fixing it will remove this message. [stderr] ● Validation Warning: [stderr] Unknown option "unstable_perfLoggerFactory" with value () => { [stderr] class Logger { [stderr] start() {} [stderr] end() {} [stderr] annotate() {} [stderr] point() {} [stderr] subSpan() { [stderr] return this; [stderr] } [stderr] } [stderr] return new Logger(); [stderr] } was found. [stderr] This is probably a typing mistake. Fixing it will remove this message. -------------------------------------- > Task :app:bundleReleaseJsAndAssets Welcome to Metro! Fast - Scalable - Integrated [stderr] error index.js: Cannot read properties of undefined (reading 'match'). TypeError: Cannot read properties of undefined (reading 'match') at countLines (/home/expo/workingdir/build/node_modules/@react-native- community/cli/node_modules/metro/src/lib/countLines.js:17:23) at Object.<anonymous> (/home/expo/workingdir/build/node_modules/@react-native- community/cli/node_modules/metro-transform-worker/src/index.js:587:24) at Generator.next (<anonymous>) at asyncGeneratorStep (/home/expo/workingdir/build/node_modules/@react-native- community/cli/node_modules/metro-transform-worker/src/index.js:146:24) at _next (/home/expo/workingdir/build/node_modules/@react-native- community/cli/node_modules/metro-transform-worker/src/index.js:168:9) info Run CLI with --verbose flag for more details. > Task :app:bundleReleaseJsAndAssets FAILED > Task :react-native-async-storage_async-storage:generateReleaseRFile > Task :react-native-community_masked-view:generateReleaseRFile > Task :react-native-community_checkbox:generateReleaseRFile [stderr] FAILURE: Build failed with an exception. [stderr] * What went wrong: [stderr] Execution failed for task ':app:bundleReleaseJsAndAssets'. [stderr] > Process 'command 'node'' finished with non-zero exit value 1 [stderr] * Try: [stderr] Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. [stderr] * Get more help at https://help.gradle.org [stderr] BUILD FAILED in 1m 39s Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0. Use '--warning-mode all' to show the individual deprecation warnings. See https://docs.gradle.org/6.9 /userguide/command_line_interface.html#sec:command_line_warnings 238 actionable tasks: 238 executed Error: Gradle build failed with unknown error. See logs for the "Run gradlew" phase for more information.
解决方案
1. 锁定Metro兼容版本
React Native 0.64.3对应的Metro稳定版本为^0.66.2,当前@react-native-community/cli依赖的Metro版本可能存在适配问题。在package.json中添加resolutions字段强制锁定版本:
"resolutions": { "metro": "^0.66.2", "metro-core": "^0.66.2", "metro-transform-worker": "^0.66.2" }
执行npm install或yarn install重新安装依赖。
2. 清理缓存与构建产物
- 清理Metro缓存:
npx react-native start --reset-cache - 清理Android构建产物:进入
android目录执行./gradlew clean - 重新执行构建命令
3. 移除Metro无效配置项
编辑metro.config.js,删除以下不兼容的配置:
server.experimentalImportBundleSupportserver.unstable_serverRootwatcher配置块unstable_perfLoggerFactory配置
4. 修复@react-native-community/cli依赖冲突
执行npm ls @react-native-community/cli或yarn list @react-native-community/cli查看依赖树,若存在多版本共存,通过resolutions强制锁定与React Native 0.64.3兼容的版本(应为^6.4.0):
"resolutions": { "@react-native-community/cli": "^6.4.0" }
内容的提问来源于stack exchange,提问作者Pruteanu Alexandru
相关产品推荐
相关产品推荐

