ERROR Error: Reanimated 2 failed to create a worklet

报错

To reload the app press "r"
To open developer menu press "d"

 BUNDLE  ./index.js 

 ERROR  Error: Reanimated 2 failed to create a worklet, maybe you forgot to add Reanimated's babel plugin?, js engine: hermes
 ERROR  Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication). A frequent cause of the error is that the application entry file path is incorrect.
      This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native., js engine: hermes
 ERROR  Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication). A frequent cause of the error is that the application entry file path is incorrect.
      This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native., js engine: hermes

解决办法

修改配置文件babel.config.js并增加plugins: ['react-native-reanimated/plugin'],
module.exports = {
presets: [‘module:metro-react-native-babel-preset’],
plugins: [‘react-native-reanimated/plugin’],
};

这里你如果运行项目会发现报错依旧。是缓存在作怪。

清缓存

  • yarn
yarn start --reset-cache
  • npx
npx react-native start --reset-cache

这里在重启运行项目,就不会有这个错了。

百思不得解

其实在我老的项目中也用到了react-native-reanimated,而且我并没有在配置文件babel.config.js中增加

plugins: [‘react-native-reanimated/plugin’],
但是项目是可以正常运行的。

新项目中就必须加上这个才能运行,我非常不理解。

我猜测会不会是因为我在老项目中使用了ant design这个组件了,而新项目中没有使用这个组件。目前我只能找出这个区别。

参考

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

https://stackoverflow.com/questions/67130651/reanimated-2-failed-to-create-a-worklet-maybe-you-forgot-to-add-reanimateds-ba

Guess you like

Origin blog.csdn.net/lxyoucan/article/details/121851577