You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native创建抽屉导航时react-native-reanimated初始化失败求助

React Native抽屉导航创建时react-native-reanimated初始化失败问题

我在VSCode中开发React Native项目,尝试创建抽屉导航,代码如下:

import { createDrawerNavigator } from '@react-navigation/drawer';

const Drawer = createDrawerNavigator();

但出现以下错误:

Error: Failed to initialize react-native-reanimated library, make sure you followed installation steps here:

  1. Make sure reanimated's babel plugin is installed in your babel.config.js (you should have 'react-native-reanimated/plugin' listed there - also see the above link for details)
  2. Make sure you reset build cache after updating the config, run: yarn start --reset-cache, js engine: hermes

我已确认babel.config.js配置正确:

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: ['react-native-reanimated/plugin'],
};

也已在根文件App.js顶部添加了如下导入语句:

import 'react-native-gesture-handler';

并且执行了命令重置缓存:

npm cache clean --force

项目依赖包如下:

...
"dependencies": {
    "@react-navigation/drawer": "^6.5.5",
    "@react-navigation/native": "^6.1.1",
    "@react-navigation/native-stack": "^6.9.6",
    "react": "18.1.0",
    "react-native": "0.70.6",
    "react-native-gesture-handler": "^2.8.0",
    "react-native-reanimated": "^2.13.0",
    "react-native-safe-area-context": "^4.4.1",
    "react-native-screens": "^3.18.2"
  },
...

请问该如何解决这个初始化失败的问题?


解决步骤
  • 使用正确的缓存重置命令:npm cache clean --force仅清理npm缓存,React Native需要重置Metro缓存,执行:

    npx react-native start --reset-cache
    

    若用yarn则执行yarn start --reset-cache,确保Metro缓存完全清除。

  • 重装react-native-reanimated并补全原生依赖:

    1. 卸载并重新安装依赖:
      npm uninstall react-native-reanimated
      npm install react-native-reanimated@2.13.0
      
    2. iOS项目进入ios目录执行:
      cd ios && pod install && cd ..
      
    3. Android项目删除构建缓存后重新构建:
      rm -rf android/app/build
      npx react-native run-android
      
  • 调整babel插件顺序:确保react-native-reanimated/plugin放在所有其他插件之前,修改babel.config.js:

    module.exports = {
      presets: ['module:metro-react-native-babel-preset'],
      plugins: ['react-native-reanimated/plugin', /* 其他插件后置 */],
    };
    
  • 确认Hermes引擎配置:

    1. Android项目打开android/app/build.gradle,确保:
      project.ext.react = [
        enableHermes: true,
      ]
      
      然后执行cd android && ./gradlew clean && cd ..清理构建缓存。
    2. iOS项目检查ios/Podfile中Hermes启用状态,重新执行pod install。
  • 完全重启开发环境:关闭VSCode、Metro服务器、模拟器/真机,重新打开项目并启动服务,避免环境残留问题。

内容的提问来源于stack exchange,提问作者Andrew Kerious

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 20:10:48