"Error: Reanimated 2 failed to create a worklet, maybe you forgot to add Reanimated's babel plugin?"
Asked Answered
C

8

27

I am trying to use createDrawerNavigator via
import { createDrawerNavigator } from '@react-navigation/drawer';
in React Native.
However, I am getting the error below, which I don't know how to solve.

Error: Requiring module "node_modules\react-native-reanimated\src\Animated.js", which threw an exception: Error: Reanimated 2 failed to create a worklet, maybe you forgot to add Reanimated's babel plugin?

In babel.config.js, I tried the code below, but it's not working :

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

Here is my component :

import * as React from 'react';
import { Button, View } from 'react-native';
import { createDrawerNavigator } from '@react-navigation/drawer';
import { NavigationContainer } from '@react-navigation/native';

function HomeScreen({ navigation }) {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Button
        onPress={() => navigation.navigate('Notifications')}
        title="Go to notifications"
      />
    </View>
  );
}

function NotificationsScreen({ navigation }) {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Button onPress={() => navigation.goBack()} title="Go back home" />
    </View>
  );
}

const Drawer = createDrawerNavigator();

export default function MyDrawer() {
  return (
    <NavigationContainer>
      <Drawer.Navigator initialRouteName="Home">
        <Drawer.Screen name="Home" component={HomeScreen} />
        <Drawer.Screen name="Notifications" component={NotificationsScreen} />
      </Drawer.Navigator>
    </NavigationContainer>
  );
}
Childish answered 29/12, 2021 at 7:39 Comment(2)
Very similar: Reanimated 2 failed to create a worklet, maybe you forgot to add Reanimated's babel plugin?.Herrod
Here is my solution for Reanimated 3, which is the current version: Answer to "Did you forget to add Reanimated Babel plugin in babel.config.js?"Herrod
I
35

Please complete the setup for react-native-reanimated. You have to add 'react-native-reanimated/plugin', in the babel.config.js file so the final code in babel.config.js will look like

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

As state in the setup docs for react-native-reanimatedHere

Also you need to complete setup for android as well (if not done yet) as stated Here

If you are using expo then follow these steps

Finally, run expo r -c to clear the cache.

Interfile answered 29/12, 2021 at 7:50 Comment(2)
@lewismachilika After adding this you have to restart the packager. Run expo start againInterfile
This didn't help me, RN - 0.68.2, @react-navigation/drawer@^6.4.2 react-native-reanimated@~2.8.0, are these version compatible each other , should I different version. Can anyone help plsPhosphorous
S
25

If you are using expo. Set this in babel.config.js:

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

Note: If you load other Babel plugins, the Reanimated plugin has to be the last item in the plugins array

After you add the Babel plugin, restart your development server and clear the bundler cache: expo start --clear.

Sheepshearing answered 8/1, 2022 at 19:22 Comment(0)
T
2

You must install according to these instructions:

[https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/installation/][1]

Also, make no mistake Be careful where you write the following code

@Override
protected JSIModulePackage getJSIModulePackage() {
return new ReanimatedJSIModulePackage();  }
Trachoma answered 9/2, 2022 at 6:51 Comment(0)
G
2

[Solution][1]

  yarn add react-native-reanimated
  cd ios
  pod install

And Import on either Index or App .js file import Animated from 'react-native-reanimated'; [1]: https://docs.swmansion.com/react-native-reanimated/docs/1.x.x/getting_started/

Gannes answered 26/4, 2022 at 10:54 Comment(0)
S
2

I am using react-native 0.69.3 and also had an issue with compiling [email protected]. I updated to @2.11.0 and it fixed my compile error.

Soche answered 14/10, 2022 at 14:34 Comment(0)
V
1

Make sure react-native-reanimated is up to date and the other packages are the same then try running npx react-native link react-native-reanimated.

If that didn't work you need to set up react-native-reanimated properly. Check out the documentation on how to set it up.

Vigilance answered 29/12, 2021 at 9:16 Comment(1)
This is not solving the issueChildish
B
1

According to react navigations drawer documentation.

  1. After installing all the packages

  2. To finalize installation of react-native-gesture-handler, add the following at the top (make sure it's at the top and there's nothing else before it) of your entry file, such as index.js or App.js:

    import 'react-native-gesture-handler';

Now this might now work for you. To complete the installation you have to change your babel.config.js file add the following:

plugins: [...,"react-native-reanimated/plugin"]

Note ... just means your other plugins if you have. Remove it if you don't have any.

You are almost there. The final thing you have to know and do is:

Note: If you load other Babel plugins, the Reanimated plugin has to be the last item in the plugins array.

After you add the Babel plugin, restart your development server and clear the bundler cache: expo start --clear

You can reference the expo reanimated docs for more details

Bacteroid answered 19/9, 2022 at 22:59 Comment(0)
U
1

Add this to babel.config.js file:

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

then run this to clear cache and restart development server:

npx expo start --clear

learn more on expo official doc.

Unformed answered 29/1, 2023 at 8:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.