首页指南参考教程

"React Native 版本不匹配" 错误

了解 React Native 版本不匹配的含义以及如何在 Expo 或 React Native 应用中解决该问题。


在开发 Expo 或 React Native 应用时,遇到如下错误的情况并不少见:

¥When developing an Expo or React Native app, it's not uncommon to run into an error that looks like:

Terminal
React Native version mismatch.
JavaScript version: X.XX.XNative version: X.XX.X
Make sure you have rebuilt the native code...

这个错误意味着什么

¥What this error means

你在终端中运行的打包程序(使用 npx expo start)使用的 react-native JavaScript 版本与设备或模拟器上的原生应用不同。升级 React Native 或 Expo SDK 版本后,或者连接到错误的本地开发服务器时,可能会发生这种情况。

¥The bundler that you're running in your terminal (using npx expo start) is using a different JavaScript version of react-native than the native app on your device or emulator. This can happen after upgrading your React Native or Expo SDK version, or when connecting to the wrong local development server.

如何修复它

¥How to fix it

  • 关闭你正在运行的所有开发服务器(你可以使用 ps 命令列出所有终端进程,并使用 ps -A | grep "expo\|react-native" 搜索 Expo CLI 或 React Native 社区 CLI 进程)。

    ¥Close out any development servers that you have running (you can list all terminal processes with the ps command, and search for Expo CLI or React Native community CLI processes with ps -A | grep "expo\|react-native").

  • 如果这是一个 Expo 项目,请从你的 app.json 文件中删除 sdkVersion 字段,或者确保它与你的 package.json 文件中的 expo 依赖的值匹配。

    ¥If this is a Expo project, either remove the sdkVersion field from your app.json file, or make sure it matches the value of the expo dependency in your package.json file.

  • 如果这是一个 Expo 项目,你应该确保你的 react-native 版本正确。运行 npx expo-doctor 将在你应安装 react-native 版本的位置显示警告。如果你确实升级到较新的 SDK,请确保运行 npx expo install --fix 并按照提示操作。Expo CLI 将确保 exporeact-native 等软件包的依赖版本保持一致。

    ¥If this is a Expo project, you should make sure your react-native version is correct. Run npx expo-doctor will show a warning where the react-native version you should install. If you did upgrade to a newer SDK, make sure to run npx expo install --fix and follow the prompts. Expo CLI will make sure that your dependency versions for packages like expo and react-native are aligned.

  • 如果这是一个裸 React Native 项目,并且在升级你的 React Native 版本后立即发生此错误,你应该仔细检查你是否已正确执行每个升级步骤。

    ¥If this is a bare React Native project, and this error is occurring right after upgrading your React Native version, you should double-check that you have performed each of the upgrade steps correctly.

  • 最后:

    ¥Finally:

    • 通过运行 rm -rf node_modules && npm cache clean --force && npm install && watchman watch-del-all && rm -rf $TMPDIR/haste-map-* && rm -rf $TMPDIR/metro-cache && npx expo start --clear 清除打包程序缓存

      ¥Clear your bundler caches by running rm -rf node_modules && npm cache clean --force && npm install && watchman watch-del-all && rm -rf $TMPDIR/haste-map-* && rm -rf $TMPDIR/metro-cache && npx expo start --clear

      • 如果你使用 npm,命令可以找到 这里。

        ¥Commands if you are using npm can be found here.

      • 如果你使用的是 Windows,则可以找到命令 这里。

        ¥Commands if you are using Windows can be found here.

    • 如果这是一个裸 React Native 项目,请运行 npx pod-install,然后重建你的原生项目(运行 yarn android 为 Android 重建,运行 yarn ios 为 iOS 重建)

      ¥If this is a bare React Native project, run npx pod-install, then rebuild your native projects (run yarn android to rebuild for Android, and yarn ios to rebuild iOS)

Expo 中文网 - 粤ICP备13048890号