错误:无法初始化react-native-reanimated库

Abh*_*ram 10 javascript reactjs react-native expo react-native-reanimated

我正在开发一个关于 React Native 的项目,试图创建抽屉导航

  • 我安装了导航抽屉、手势处理程序和重新启动的库

  • 当我运行时,我收到错误第一个错误:

    错误错误:无法初始化react-native-reanimated库,请确保您按照此处的安装步骤操作: https: //docs.swmansion.com/react-native-reanimated/docs/fundamentals/installation/

    1. 确保 reanimated 的 babel 插件安装在 babel.config.js 中(你应该在那里列出“react-native-reanimated/plugin” - 另请参阅上面的链接了解详细信息)
    2. 确保在更新配置后重置构建缓存,运行:yarn start --reset-cache,js引擎:hermes

所以我根据这个错误中的建议,在 babel.config.js 中添加了plugins:['react-native-reanimated/plugin',并从 npm start ----reset 缓存开始,这给了我另一个错误:

第二个错误

error: index.js: Unknown option: .Plugins. Check out https://babeljs.io/docs/en/babel-core/#options for more information about options.
Run Code Online (Sandbox Code Playgroud)

这是我的 package.json

{
  "name": "Train",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "@react-native-community/masked-view": "^0.1.11",
    "@react-navigation/drawer": "^6.5.0",
    "@react-navigation/native": "^6.0.13",
    "@react-navigation/stack": "^6.3.1",
    "react": "18.1.0",
    "react-native": "0.70.1",
    "react-native-gesture-handler": "^2.6.2",
    "react-native-reanimated": "^2.10.0",
    "react-native-safe-area-context": "^4.3.4",
    "react-native-screens": "^3.17.0",
    "react-navigation-stack": "^2.10.4"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "@babel/runtime": "^7.12.5",
    "@react-native-community/eslint-config": "^2.0.0",
    "babel-jest": "^26.6.3",
    "eslint": "^7.32.0",
    "jest": "^26.6.3",
    "metro-react-native-babel-preset": "^0.72.1",
    "react-test-renderer": "18.1.0"
  },
  "jest": {
    "preset": "react-native"
  }
}
Run Code Online (Sandbox Code Playgroud)

我的索引.js

import {AppRegistry} from 'react-native';
import App from './App';
import Login from './pages/Login';
import Home from './pages/Home';
import cart from './pages/Drawer/cart';
import items from './pages/Drawer/items';
import {name as appName} from './app.json';

AppRegistry.registerComponent(appName, () => App);
Run Code Online (Sandbox Code Playgroud)

我的 babel.config.js

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  Plugins:['react-native-reanimated/plugin'],//I added this line because of the 1st error 
};
Run Code Online (Sandbox Code Playgroud)

我的应用程序.js

import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator} from '@react-navigation/stack';
import {createDrawerNavigator} from '@react-navigation/drawer';

//for stack
import Login from './pages/Login';
import Home from './pages/Home';
//for drawer
import cart from './pages/Drawer/cart';
import items from './pages/Drawer/items';
import wallet from './pages/Drawer/wallet';
import orders from './pages/Drawer/orders';


const stack = createStackNavigator();
const Drawer = createDrawerNavigator();

function MystackNav(){
  return(
    <stack.Navigator>
      <stack.Screen name='Login' component={Login} options={{headerShown:false}}/>
      <stack.Screen name='Home' component={Home} options={{headerShown:false}}/>
      <stack.Screen name='Drawer' component={MyDrawer}/>
    </stack.Navigator>
  )
}

function MyDrawer(){
  return(
    <Drawer.Navigator>
      <Drawer.Screen name='cart' component={cart}/>
    </Drawer.Navigator>
  )
}

export default function App(){
  return(
    <NavigationContainer>
      <MystackNav/>
    </NavigationContainer>
  )
}
Run Code Online (Sandbox Code Playgroud)

小智 16

1.更新babel.config.js

  module.exports = {
    presets: [
      ...
    ],
    plugins: [
      ...
      'react-native-reanimated/plugin',
    ],
  };
Run Code Online (Sandbox Code Playgroud)

2.运行expo start -c

参考资料- https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/installation/


小智 10

我的正在工作,唯一的区别是:

babel.config.js

module.exports = function(api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: ['react-native-reanimated/plugin']
  };
};
Run Code Online (Sandbox Code Playgroud)

package.json

{
  "name": "awesomeproject2",
  "version": "1.0.0",
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web"
  },
  "dependencies": {
    "@react-navigation/bottom-tabs": "^6.4.0",
    "@react-navigation/drawer": "^6.5.0",
    "@react-navigation/native": "^6.0.13",
    "@react-navigation/stack": "^6.3.2",
    "babel-preset-expo": "^9.2.0",
    "expo": "^46.0.16",
    "expo-status-bar": "^1.4.0",
    "react": "~18.1.0",
    "react-native": "^0.70.3",
    "react-native-gesture-handler": "^2.7.1",
    "react-native-reanimated": "^2.11.0",
    "react-native-safe-area-context": "^4.4.1",
    "react-native-screens": "^3.18.2"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "@babel/plugin-proposal-export-namespace-from": "^7.18.9",
    "babel-preset-expo": "^9.2.0"
  },
  "private": true
}
Run Code Online (Sandbox Code Playgroud)


bar*_*rry 6

tl/dr:怀疑我到 babel 插件后npx expo start --clear而不是关闭npm start --reset-cache'react-native-reanimated/plugin'

当尝试在 Android 上使用 expo go 运行我的应用程序时,我遇到了同样的错误:

ERROR  Error: Failed to initialize react-native-reanimated library, make sure you followed installation steps here: https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/installation/       
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
ERROR  Invariant Violation: "main" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called.
Run Code Online (Sandbox Code Playgroud)

根据指南,我只需要更新上面多人提到的 babel.config.js,尽管这不起作用。

然而,当我按照指南上的网络支持更新部分进行操作时,它开始对我有用。https://docs.expo.dev/versions/latest/sdk/reanimated/#installation

plugins: [    
  '@babel/plugin-proposal-export-namespace-from',
  'react-native-reanimated/plugin',
],
Run Code Online (Sandbox Code Playgroud)

跟随但正在运行:

npx expo start --clear
Run Code Online (Sandbox Code Playgroud)

我怀疑它正在使用什么修复npx expo start --clear,因为之前我一直在使用npm start --reset-cache