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

This is my babel file
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"
I followed their installation process: React Native Reanimated instalation guide.
The error is:
Reanimated 2 failed to create a worklet, maybe you forgot to add Reanimated's babel plugin?

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:
Run yarn add react-native-reanimated#next react-native-gesture-handler
I have added import 'react-native-gesture-handler' to App.tsx file at the top of the file before importing any packages
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",
],
};
The last thing you should do is run your project by removing the cache yarn start --reset-cache

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.

Clearing the cache worked for me, run:
npx react-native start --reset-cache

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

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

I faced the same issue. The following command solved the issue:
npm install react-native-reanimated#~2.2.0

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 :
Delete the node_modules folder and run this just to be sure.
npx react-native start --reset-cache
Run
npm install
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
],
};
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

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

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.

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

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

follow these steps
expo install react-native-reanimated
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'],
};
};
expo start --clear

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

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.

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.

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.

I had the same issue yesterday and when I did a Google search I landed here. After some digging here is what I understood:
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.
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

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'],
};

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

If you are using expo, what worked for me is
expo doctor --fix-dependencies

install expo
expo install react-native-reanimated
add babel.config.js
plugins: ['react-native-reanimated/plugin']
start server
expo start --clear

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.

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
2-https://www.reanimated2.com/docs/fundamentals/installation
3-https://stackoverflow.com/questions/67130651/reanimated-2-failed-to-create-a-worklet-maybe-you-forgot-to-add-reanimateds-ba/68694681#68694681
   

Related

React Native, iOS bundling failed: useUnicodeFlag is not a valid regexpu-core option

node_modules/react-native/Libraries/LogBox/Data/parseLogBoxLog.js:
/Users/rakshithkumars/Number-Guessing/node_modules/react-native/Libraries/LogBox/Data/parseLogBoxLog.js:
.useUnicodeFlag is not a valid regexpu-core option.
I fixed it in this way:
Run:
yarn add #babel/plugin-proposal-unicode-property-regex --dev
or
npm install --save-dev #babel/plugin-proposal-unicode-property-regex
Then open you babel.config.js file, which may be something like this:
module.exports = function (api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
};
};
And add this line:
plugins: ['#babel/plugin-proposal-unicode-property-regex'],
It should end up something like this:
module.exports = function (api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
plugins: ['#babel/plugin-proposal-unicode-property-regex'],
};
};
Note:
This shouldn't change anything, but, before trying this change, I migrated from Node 14 to Node 16. Should not affect, but... Mentioning it just in case.
Note 2:
It was not necessary to run expo r -c in my case.
Experienced the same issue and fixed it by adding the following plugin to babel.config.js:
#babel/plugin-proposal-unicode-property-regex
Then ran expo r -c to clear the cache

Fullcalendar custom css with Vue3 and postcss

I am using fullcalendar with vue3. I want to change change colors for button and text in fullcalendar.
vue version
"vue": "^3.2.26",
I am getting error
Syntax Error: Error: PostCSS plugin postcss-custom-properties requires
PostCSS 8.
I am following steps mentioned in fullcalendar documentation.
fullcalendar-vars.css
:root {
--fc-border-color: green;
--fc-button-text-color: #ff0000;
}
I have installed following packages
"postcss": "^8.4.7",
"postcss-calc": "^8.2.4",
"postcss-custom-properties": "^12.1.4",
"postcss-loader": "^6.2.1",
Added postcss.config.js at root
module.exports = {
plugins: [
require('postcss-custom-properties')({
preserve: false, // completely reduce all css vars
importFrom: [
'client/fullcalendar-vars.css' // look here for the new values
]
}),
require('postcss-calc')
]
}
And my vue.config.js as follow
const path = require("path");
module.exports = {
pages: {
index: {
entry: "./client/main.js",
},
},
outputDir: path.resolve(__dirname, "./client/dist"),
};
Also I would like to know, Do I need make any changes in vue.config.js?
PostCSS error
Vue CLI scaffolded projects already include PostCSS (including postcss, postcss-calc, and postcss-loader), so you don't need to install it in your project. The only dependency needed here is postcss-custom-properties.
To resolve the PostCSS error, you should uninstall the PostCSS dependencies that you had mistakenly added:
npm uninstall --save-dev postcss postcss-calc postcss-loader
Starting from scratch
To setup custom colors for FullCalendar in a Vue CLI scaffolded project:
Install postcss-custom-properties with:
npm install --save-dev postcss-custom-properties
Create postcss.config.js with the following PostCSS configuration:
// <projectRoot>/postcss.config.js
module.exports = {
plugins: [
require("postcss-custom-properties")({
preserve: false,
importFrom: [
"client/fullcalendar-vars.css",
],
}),
require("postcss-calc"),
],
}
Create fullcalendar-vars.css with the following CSS:
/* <projectRoot>/client/fullcalendar-vars.css */
:root {
--fc-border-color: green;
--fc-button-text-color: #ff0000;
}
Note: Changes to this file are not hot-reloaded, so the dev server must be restarted to reflect any updates.
Start the Vue CLI dev server with:
npm run serve
demo

Android App crashes when "#babel/preset-env" is added

While running:
npx react-native start --reset-cache
npx react-native run-android
My app crashes at the end of the build without any log.
The problem is coming from #babel/preset-env in babel.config.js (used by jest tests)
babel.config.js :
module.exports = {
presets: ['module:metro-react-native-babel-preset', "#babel/preset-env", '#babel/preset-react'],
plugins: [
["#babel/plugin-proposal-private-methods", { "loose": true }],
'react-native-reanimated/plugin'
],
};
Removing "#babel/preset-env" is resolving the issue, but I need it to launch Jest.
Do you have any idea to fix this ?
I fixed it by simply removing '#babel/preset-react' and '#babel/preset-env' from babel.config.js.
Code was transpiling with module:metro-react-native-babel-preset which caused this issue.

Missing dependencies when using yarn workspace with React native init

I want to setup a monorepo.
I init my React native project with npx react-native init myProject as the first project. (there will be more project added later)
Folder structure
Parent
myProject
package.json (created by react-native)
yarn.lock
package.json (where I setup workspace)
Then I setup yarn workspace from the parent folder of myProject.
{ "name": "Parent",
"private": true,
"workspaces": {
"packages": [
"*"
],
"nohoist": [
"**/react-native",
"**/react-native-*",
"**/#react-native-*",
"**/#react-native-*/**",
"**/#react-navigation",
"**/#react-navigation/**",
"**/hermes-engine",
"**/rn-*"
] }
}
Everything seems to work until I push to git and clone back. I use yarn install but got this error when start the project (run android or run ios)
Error: Unable to resolve module `scheduler` from `node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js`: scheduler could not be found within the project or in these directories:
..\node_modules
The only way I can fix that is to cd myProject and run npm install (it will add some packages and the app will work) while cd and using yarn install won't do anything
I just want to use yarn for the project so what can I do to fix this problem?
I figure out how to fix it. I forgot to edit metro.config in RN folder.
const path = require('path')
const linkedLibs = [path.resolve(__dirname, '..')]
console.info('CONFIG', linkedLibs)
module.exports = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
watchFolders: linkedLibs,
};
Use this and everything will be fine

Using ES7 static propTypes with React-Native

When I'm launching my project using React-Native default packager, I have this error: Unexpected token on this line:
static propTypes = {
/// ...
};
I took a look on React-Native issues on GitHub, but I didn't find a solution.
Any suggestion?
React-Native packager use Babel for transfer ES6 and ES7, but NOT ALL features. The enable list is here. In your case, class-props is not enabled by default in RN packager. You can use Babel to compiler your code before packager, or just enable it in the packager setting. See this official doc for more information.
Try appending your propTypes to your class:
var MyClass extends React.Component {
....
}
MyClass.propTypes = {
.... /* enter proptypes here */
}
After #Fomahaut answer, I keep looking on Facebook's GitHub repository and found this issue: https://github.com/facebook/react-native/issues/2182
Create a .babelrc file at the project's root directory
Add more rules to Babel
Example:
{
"retainLines": true,
"compact": true,
"comments": false,
"whitelist": [
"es6.arrowFunctions",
"es6.blockScoping",
"es6.classes",
"es6.constants",
"es6.destructuring",
"es6.forOf",
"es6.modules",
"es6.parameters",
"es6.properties.computed",
"es6.properties.shorthand",
"es6.spread",
"es6.tailCall",
"es6.templateLiterals",
"es6.regex.unicode",
"es6.regex.sticky",
"es7.asyncFunctions",
"es7.classProperties",
"es7.comprehensions",
"es7.decorators",
"es7.exponentiationOperator",
"es7.exportExtensions",
"es7.functionBind",
"es7.objectRestSpread",
"es7.trailingFunctionCommas",
"regenerator",
"flow",
"react",
"react.displayName"
],
"sourceMaps": false
}
According to this answer, you need to install a plugin for class properties as of Babel 6.
As of Babel 6, you now need the transform-class-properties plugin to
support class properties.
Steps:
Run this: npm install babel-plugin-transform-class-properties
Add this to your .babelrc: "plugins": ["transform-class-properties"]
(Note, it's a plugin, not a transform; so don't put it in the "presets" list.)
Worked for me.
Install the stage-0 Babel preset (npm i --save-dev babel-preset-stage-0) and add it to .babelrc file's presets section, e.g.:
{ "presets": ["react", "es2015", "babel-preset-stage-0"] }
See if that helps:
$ npm install babel-plugin-transform-decorators
navigate to /<your project root>/node_modules/react-native/packager/react-packager/.babelrc
Add "transform-decorators" to this list:
{
"retainLines": true,
"compact": true,
"comments": false,
"plugins": [
"syntax-async-functions",
"syntax-class-properties",
"syntax-trailing-function-commas",
"transform-class-properties",
"transform-es2015-arrow-functions",
"transform-es2015-block-scoping",
"transform-es2015-classes",
"transform-es2015-computed-properties",
"transform-es2015-constants",
"transform-es2015-destructuring",
["transform-es2015-modules-commonjs", {"strict": false, "allowTopLevelThis": true}],
"transform-es2015-parameters",
"transform-es2015-shorthand-properties",
"transform-es2015-spread",
"transform-es2015-template-literals",
"transform-flow-strip-types",
"transform-object-assign",
"transform-object-rest-spread",
"transform-react-display-name",
"transform-react-jsx",
"transform-regenerator",
"transform-es2015-for-of",
-->"**transform-decorators**"<--
],
"sourceMaps": false
}