You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何修复React Native Reanimated引发的Babel解析错误?

解决react-native-reanimated的Module parse failed: Unexpected token错误

问题场景

整合代码时遇到react-native-reanimated的web端编译错误,具体报错:

./node_modules/react-native-reanimated/lib/index.web.js 3:9
Module parse failed: Unexpected token (3:9)
File was processed with these loaders:

  • ../../../AppData/Roaming/npm/node_modules/expo-cli/node_modules/babel-loader/lib/index.js
    You may need an additional loader to handle the result of these loaders.
    | import "./reanimated2/js-reanimated/global";
    | export * from "./reanimated2";

export * as default from "./Animated";

解决思路

  • 校验版本兼容性:react-native-reanimated v2及以上版本要求Babel 7.12+,同时确保babel-loader版本与项目Babel版本匹配,避免版本不兼容导致语法解析失败。
  • 配置Babel插件:在项目的babel.config.js(或.babelrc)中添加react-native-reanimated/plugin插件,注意插件需放在预设之后,示例配置:
module.exports = {
  presets: ['babel-preset-expo'],
  plugins: ['react-native-reanimated/plugin'],
};
  • 清除编译缓存:执行缓存重置命令,清除旧的编译产物:
    • Expo项目:expo r -c
    • React Native项目:npx react-native start --reset-cache
  • 调整Webpack配置:由于报错来自web端文件,需确保webpack让babel-loader处理react-native-reanimated模块,修改webpack.config.js:
const path = require('path');

module.exports = {
  module: {
    rules: [
      {
        test: /\.(js|jsx|ts|tsx)$/,
        include: [
          path.resolve(__dirname, 'src'),
          path.resolve(__dirname, 'node_modules/react-native-reanimated'),
        ],
        use: 'babel-loader',
      },
    ],
  },
};
  • 重装依赖:删除node_modules、package-lock.json(或yarn.lock),重新执行npm install或yarn install,确保依赖完整无损坏。

内容的提问来源于stack exchange,提问作者Idan Ben Eliyaho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 00:54:31