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

29

83

How can I solve this error?

Reanimated 2 failed to create a worklet, maybe you forgot to add Reanimated's babel plugin?

enter image description here


This is my babel.config.js file :

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

My code:

import React, { useRef, useState } from 'react'
import { View, useWindowDimensions, Button } from 'react-native'
import Animated, { runOnUI } from 'react-native-reanimated';

export default function Login() {
  const { width, height } = useWindowDimensions();
  // const value = useSharedValue(0);
  function someWorklet(greeting: any) {
    'worklet';
    console.log("Hey I'm running on the UI thread");
  }

  return (
    <View style={{ flex: 1, justifyContent: 'flex-end', alignItems: 'center' }}>
      <Button title="click me" onPress={() => runOnUI(someWorklet)('Howdy')} />
    </View>
  );
}

The package I installed is "react-native-reanimated": "^2.1.0".

Nett answered 16/4, 2021 at 18:46 Comment(2)
Very similar: Reanimated 2 failed to create a worklet, maybe you forgot to add Reanimated's babel plugin?.Interspace
npm start -- --reset-cache solved this issue for me –Mystic
A
162

I have found this issue on this link. These are the steps that I have followed for having my project up and running without any errors:

  1. Run yarn add react-native-reanimated@next react-native-gesture-handler
  2. I have added import 'react-native-gesture-handler' to App.tsx file at the top of the file before importing any packages
  3. You should update the babel.config.js file and add react-native-reanimated/plugin to plugins
module.exports = {
  presets: ["module:metro-react-native-babel-preset"],
  plugins: [
    "react-native-reanimated/plugin",
  ],
};
  1. The last thing you should do is run your project by removing the cache yarn start --reset-cache
Aculeate answered 24/9, 2021 at 15:20 Comment(10)
For my folks using expo, on the last step, step 4, use expo r -c insteadTania
Feb 2022: cache cleaner is a killer thing that made everything works for me, thanks. Besides, everything works fine even without step 2, though it is recommended in docs during installationLattimer
Just this comment solved the issue. expo r -cOne
For others still having issues, don't foget about .babelrc I had both babel.config and .babelrc and wasn't adding it to .babelrcSeagrave
My config was already ok. Reset cache did the job. I guess configuring it is pretty easy and straight forward following the installation guide. That's why cache was the problem for most of us.Dor
I should mention that by running yarn add react-native-reanimated@next you are going to have reanimated@3^ installed in your projectAculeate
Step 4 is the key.Mckamey
After doing the above mentioned change, running the command expo r -c worked for me.Dandify
I only use expo r -c and it works.Frustrated
If using NPM, this command was the one that worked for me in step 4: npm start -- --reset-cacheChristology
C
114

Here is what worked for me in an Expo project.

This is my babel.config.js.

Note that react-native-reanimated/plugin must be at the last plugin in the plugins array of the babel.config.js.

module.exports = function (api) {
  api.cache(true);
  return {
    presets: ["babel-preset-expo"],
    plugins: [
      [
        "module-resolver",
        {
          extensions: [".tsx", ".ts", ".js", ".json"],
        },
      ],
      "react-native-reanimated/plugin",
    ],
  };
};

Make sure you add react-native-reanimated/plugin as the last element of the plugins

Then stop the expo-server and run the following command:

expo r -c

It's all done.

Calc answered 7/8, 2021 at 17:26 Comment(7)
I was updating my expo project......you may have just prevented me from having a stroke....🍻Pent
Logged in just to upvote this. Thanks!Marte
This works in for my expo project. THANKSMimicry
This worked for me today on expo 44 version.Welcher
just excellent. thanks dearSubmission
That work, It works, the documentation makes it very easy for us to misunderstand: module.exports = { presets: [ ... ], plugins: [ ... 'react-native-reanimated/plugin', ], };Brockway
Nice try, but it didn't work for me. Sorry!Interspace
G
63

Clearing the cache worked for me, run:

npx react-native start --reset-cache
Gipon answered 13/6, 2021 at 16:17 Comment(2)
In my case, It also required me to install react-native-gesture-handlerFaction
npm start -- --reset-cache solved this issue for meMystic
A
25

If you are using Expo try to start your app with expo r -c in order to clear caches associated with your project.

Azotize answered 30/5, 2021 at 11:10 Comment(0)
T
23

Just add the below code in babel.config.js

 module.exports = {
      presets: ['module:metro-react-native-babel-preset'],

      // add the below line 
      plugins: ['react-native-reanimated/plugin'], 
     // this should be always last line
    };

then if you need to clear the package manager cache and start it clean if you are using

yarn

yarn start --reset-cache

npx

npx react-native start --reset-cache

and it worked for me

Teofilateosinte answered 11/11, 2021 at 8:20 Comment(0)
L
16

I faced the same issue. The following command solved the issue:

npm install react-native-reanimated@~2.2.0
Letitialetizia answered 7/1, 2022 at 9:26 Comment(3)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Moffat
i was stuck for 5 hour then i found this and solved my problem thank youBullfight
worked for me from 2.3 to 2.2Bounder
B
6

Just adding "plugins: ['react-native-reanimated/plugin']", in my babel.config did the trick for me.

My babel.config.js file look like this now.

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

then I fully cleaned my RN project by running these:

watchman watch-del-all
rm -rf yarn.lock package-lock.json node_modules
rm -rf android/app/build
rm ios/Pods ios/Podfile.lock 
rm -rf ~/Library/Developer/Xcode/DerivedData
npm install && cd ios && pod update && cd ..
npm start -- --reset-cache

You should run one by one or one line command.

watchman watch-del-all && rm -rf yarn.lock package-lock.json node_modules && rm -rf android/app/build && rm ios/Pods ios/Podfile.lock && rm -rf ~/Library/Developer/Xcode/DerivedData && npm install && cd ios && pod update && cd ..

Hope this help to someone.

Bonnie answered 14/2, 2022 at 18:55 Comment(2)
Since last few days I was facing an issue and was not able to fix this. However your steps helps me and now I fixed this. You saved me. Thansk a million. Keep up th good workJoesphjoete
From your react-native directory, you can also use npx react-native clean to more quickly clean your project instead of typing all those commands 😉Coercion
V
4

I also came around this problem while implementing the reanimated 2 package. For me it seems a half configured installation issue. I am using React Native CLI and Windows Machine. This is what worked for me :

  1. Delete the node_modules folder and run this just to be sure.
npx react-native start --reset-cache
  1. Run
npm install
  1. In babel.cofig.js add these lines. the Reanimated plugin has to be the last item in the plugins array.
//babel.config.js
module.exports = {
      ...
      plugins: [
          ...
          'react-native-reanimated/plugin', // << add
      ],
  };
  1. Turn on Hermes engine by editing android/app/build.gradle
project.ext.react = [
  enableHermes: true  
]

5.Plug Reanimated in MainApplication.java. This file is present in android/app/src/main/java/com/appname folder.

import com.facebook.react.bridge.JSIModulePackage; // << add
import com.swmansion.reanimated.ReanimatedJSIModulePackage; // << add
  ...
  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
  ...

      @Override
      protected String getJSMainModuleName() {
        return "index";
      }

      **@Override //<<add this function
      protected JSIModulePackage getJSIModulePackage() {
        return new ReanimatedJSIModulePackage(); 
      }**
    };

This is actually availaible in there installation docs. https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/installation

Viscardi answered 28/12, 2021 at 12:0 Comment(0)
U
4

I just add react-native-reanimated/plugin to babel.config.json in plugins array

module.exports = {
  presets: [some thing],
  plugins: [
    "react-native-reanimated/plugin",
  ],
};

and after that expo start -c

and error solved no need do extra steps

Uniaxial answered 11/3, 2022 at 13:43 Comment(0)
H
3

In my condition i will change my babel.config.js to different this


module.exports = function (api) {
  api.cache(true);
  return {
    presets: ["babel-preset-expo"],
    plugins: [
      [
        "module-resolver",
        {
          extensions: [".tsx", ".ts", ".js", ".json"],
        },
      ],
      "react-native-reanimated/plugin",
    ],
  };
};

Don't Forget to type this command on your project directory


expo r -c

Hame answered 2/4, 2022 at 4:22 Comment(4)
just copy of babel.confi.js my code and paste it to yoursHame
expo r -c is for clearing cache of your projectHame
Can you edit your answer to include your comments, and then delete your comments? Comments are useful for the community to ask for clarification, but as the author of the answer, it's better to just update your answer to be fully self-contained, without relying on the comments for further clarification.Riobard
But here are i nothing comments are addedHame
T
3

I can help you to solve this error

Make sure Your have installed Reanimated

To install :

npm install react-native-reanimated

If you done it already that’s good

  • Now add this plugins in your babel.config.js
module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: ['react-native-reanimated/plugin'], --> // This one
};

Sometimes you have done all setups properly but still getting errors then just type:

npx react-native start --reset-cache

If still getting Errors: Close your terminal and simulator/Emulator and just type

\\ For Android 
 
npx react-native run-android

\\ For IOS

npx react-native run-ios

Hope This will Work !

Titfer answered 4/9, 2023 at 10:22 Comment(0)
T
3

I recently encountered the same issue with my newly created Expo app and attempted the solutions discussed here without success. However, I managed to solve the problem with the following steps:

Initially, my babel.config.js looked like this:

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

I modified it to:

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

Subsequently, I cleared the cache and restarted the app using the following command:

npx expo start -c

A piece of advice for users of '@react-navigation/native': avoid adding the 'NavigationContainer' to your 'AppEntry' file.

This is how I successfully resolved my issue.

Taphole answered 16/9, 2023 at 14:53 Comment(0)
W
2

Delete node_modules and reinstall, and make sure to delete all caches and all previous settings -- RN caches, packager caches, simulator caches and settings, etc. It might even help to go to a previous version of your app when you hadn't tried to install version 2 at all.

I am using expo and following all these steps was helpful :- https://forums.expo.io/t/how-to-clear-the-expo-and-react-native-packager-caches-metro-watchman-haste/1352

Wiese answered 22/4, 2021 at 8:50 Comment(0)
P
2

Add this code in babel.config.js

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  env: {
    production: {
      plugins: [
         
      ],
    },
  },
  plugins: [ 
    [ 
      'module-resolver',
      {
        extensions: ['.tsx', '.ts', '.js', '.json'], 
      },
    ],
    'react-native-reanimated/plugin'
  ]
};

and then rebuild your application or run yarn start --reset-cache

Propagandist answered 3/12, 2021 at 6:11 Comment(0)
C
2

follow these steps

  1. expo install react-native-reanimated
  2. After the installation completed, add the Babel plugin to babel.config.js:
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: ['react-native-reanimated/plugin'],
  };
};
  1. expo start --clear
Combative answered 16/3, 2022 at 7:53 Comment(0)
G
2

Sometimes you have done all setups but you are getting errors then just type this cmd:

npx react-native start --reset-cache

It will be helpful.

Gebler answered 8/4, 2022 at 10:2 Comment(0)
S
1

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

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

Stogner answered 5/12, 2021 at 5:56 Comment(0)
S
1

You can try

yarn add react-native-reanimated@next
npx react-native start --reset-cache

Add Reanimated's babel plugin to your babel.config.js.

module.exports = {
  presets: [
    'module:metro-react-native-babel-preset',
    '@babel/preset-typescript'
  ],
  plugins: ['react-native-reanimated/plugin'],
};
Sterne answered 7/1, 2022 at 10:9 Comment(0)
C
1

I tried this on a freshly created expo project (tabs template). It generated the following default babel.config.js:

module.exports = function(api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo']
  };
};

I added just one line, like this:

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

and cleared the rebuild cache (yarn start -c).

This worked for me.

Carlock answered 26/1, 2022 at 21:51 Comment(0)
D
0

I had the same issue yesterday and when I did a Google search I landed here. After some digging here is what I understood:

  1. Reanimated v2's latest version does not run in Expo when in debug mode. So I tried to open the developer options to turn off debugging by shaking the device but I was not able to.
  2. Then I found that you can toggle to production mode once the Metro Bundler server connection is established. Once you toggle to production mode in Expo, the app works. Enabling Production mode in Expo
Dreadful answered 13/9, 2021 at 2:57 Comment(0)
K
0

It's worked for me. My babel.config was:

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

I changed to this:

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

And did all the instructions at https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/installation

Kerbing answered 16/3, 2022 at 20:41 Comment(0)
P
0

If you are using expo, what worked for me is

expo doctor --fix-dependencies
Phipps answered 18/3, 2022 at 9:52 Comment(0)
D
0
  1. install expo
    expo install react-native-reanimated

  2. add babel.config.js
    plugins: ['react-native-reanimated/plugin']

  3. start server
    expo start --clear

Dianemarie answered 6/4, 2022 at 10:40 Comment(0)
S
0

I had to upgrade from 2.8.0 to 2.9.1 (the latest stable version). Also 3.0.0-rc worked too.

You can see the latest versions on the Versions tab here.

Change it in your package.json file:

"react-native-reanimated": "2.9.1",

And run yarn again to download the package.

We had a .babelrc and babel.config.json and only the config had our reanimated plugin so I consolidated them but that didn't fix the issue.

Sisal answered 14/7, 2022 at 21:59 Comment(0)
O
0

For "react-native-reanimated": "~2.12.0"

Step 1:

Add import 'react-native-gesture-handler' to App.ts file at the top of the file before importing any packages

Step 2:

Go to the babel.config.js and add plugins array:-

module.exports = function (api) {   

api.cache(true);  

 return {     '

        presets: ["babel-preset-expo"],     
        plugins: ["react-native-reanimated/plugin"//must be in the end of plugins array],
  };

 };

Step 3:

  Run npx expo -c

Resources:   

*1.*https://reactnavigation.org/docs/drawer-navigator

react-native-reanimated

2-https://www.reanimated2.com/docs/fundamentals/installation

3-https://mcmap.net/q/240743/-quot-reanimated-2-failed-to-create-a-worklet-maybe-you-forgot-to-add-reanimated-39-s-babel-plugin-quot/68694681#68694681

   

Ody answered 29/12, 2022 at 7:44 Comment(0)
N
0

set you babel.config.js in the main directory like as below

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: ['react-native-reanimated/plugin'],
};
Nous answered 6/3, 2023 at 8:29 Comment(0)
S
0

I got this error when after add React-Navigation to my React-Native project.

  1. Close Metro
  2. gradlew clean //if need
  3. Close emulator
  4. delete app //if need
  5. npx react-native start --reset-cache

its works for me.

Saarinen answered 12/3, 2023 at 13:2 Comment(0)
S
0

I was facing the same issue

running npx expo start -c in my terminal solved this issue

Signally answered 9/3 at 11:9 Comment(0)
P
0

Sharing because this happened to me My Config was

{
    presets: ["babel-preset-expo"],
    env: {
      production: {
        plugins: ["react-native-paper/babel", "react-native-reanimated/plugin"],
      },
    },
  };
};

The config that worked for me (I was building for dev, but my config was set for production)

{
    presets: ["babel-preset-expo"],
    plugins: ["react-native-paper/babel", "react-native-reanimated/plugin"],
 
};
Palaeo answered 14/3 at 22:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.