React Native 0.75 + Reanimated 3.15 出现Failed to create worklet错误求助
排查「Error: [Reanimated] failed to create worklet」问题
可能的解决方案
1. 彻底清除缓存并重启服务
执行以下命令清理项目缓存与依赖:
npx react-native start --reset-cache rm -rf node_modules && npm install cd ios && pod install && cd ..
完成后重新运行项目:npx react-native run-android 或 npx react-native run-ios
2. 检查 Metro 配置(适配 React Native 0.75+)
React Native 0.75 采用新的 Metro 默认配置,若存在自定义配置,需确保 Reanimated 的 transformer 被正确引入:
const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config'); const customConfig = { transformer: { babelTransformerPath: require.resolve('react-native-reanimated/metro-transformer'), }, }; module.exports = mergeConfig(getDefaultConfig(__dirname), customConfig);
3. 确认 Reanimated 初始化顺序
在项目入口文件(如 index.js/App.js)的最顶部添加 Reanimated 初始化代码,必须放在所有其他导入之前:
import 'react-native-reanimated'; // 后续导入语句...
4. 排查 NativeWind 兼容性冲突
项目使用的 NativeWind v2.0.11 可能与当前 React Native/Reanimated 版本存在兼容性问题。尝试暂时移除 nativewind/babel 插件,重新运行项目验证问题是否消失。若解决,升级 NativeWind 至最新兼容版本。
5. 检查 Worklet 代码规范
确保所有标记为 worklet 的函数符合要求:
- 函数体首行必须添加
'worklet';指令 - 避免在 worklet 中使用 Reanimated 不支持的 API(如浏览器专属 API)
- 不要引用外部未序列化的变量
你的配置文件信息
babel.config.js
module.exports = { presets: ['module:@react-native/babel-preset'], plugins: [ ["react-native-auto-route/plugin", { appDirectory: "src" }], "nativewind/babel", 'react-native-reanimated/plugin' ], };
package.json
{ "dependencies": { "@fortawesome/fontawesome-svg-core": "^6.6.0", "@fortawesome/free-solid-svg-icons": "^6.6.0", "@fortawesome/react-native-fontawesome": "^0.3.2", "@react-native-community/blur": "^4.4.0", "nativewind": "^2.0.11", "react": "18.3.1", "react-native": "0.75.2", "react-native-blurhash": "^2.0.3", "react-native-gesture-handler": "^2.18.1", "react-native-linear-gradient": "^2.8.3", "react-native-loader-kit": "^2.0.8", "react-native-paper": "^5.12.5", "react-native-reanimated": "^3.15.0", "react-native-safe-area-context": "^4.10.9", "react-native-screens": "^3.34.0", "react-native-splash-screen": "^3.3.0", "react-native-svg": "^15.6.0", "react-native-track-player": "^4.1.1" }, "devDependencies": { "@babel/core": "^7.20.0", "@babel/preset-env": "^7.20.0", "@babel/runtime": "^7.25.4", "@react-native/babel-preset": "0.75.2", "@react-native/eslint-config": "0.75.2", "@react-native/metro-config": "0.75.2", "@react-native/typescript-config": "0.75.2", "@types/react": "^18.2.6", "@types/react-test-renderer": "^18.0.0", "babel-jest": "^29.6.3", "eslint": "^8.19.0", "jest": "^29.6.3", "prettier": "2.8.8", "react-native-auto-route": "^0.3.3", "react-test-renderer": "18.3.1", "tailwindcss": "^3.3.2", "typescript": "5.0.4" } }
内容的提问来源于stack exchange,提问作者Jasvinder singh
相关产品推荐
相关产品推荐

