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

React Native Web Loader问题:模块解析失败(Unexpected token)

解决方案

问题根源

webpack配置中ts-loader规则全局排除了node_modules目录,但@react-native-firebase和react-native的部分文件包含TypeScript语法(如interface),未被转译导致解析失败。


步骤1:修改webpack.config.js

调整module.rules中的ts-loader规则,不再全局排除node_modules,而是指定需要转译的目录:

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const rootDir = path.join(__dirname, '..');
const webpackEnv = process.env.NODE_ENV || 'development';

module.exports = {
  mode: webpackEnv,
  entry: {
    app: path.join(rootDir, './index.web.ts'),
  },
  output: {
    path: path.resolve(rootDir, 'dist'),
    filename: 'app-[hash].bundle.js',
  },
  devtool: 'source-map',
  module: {
    rules: [
      {
        test: /\.(tsx|ts|jsx|js|mjs)$/,
        // 包含自己的源码 + 需要转译的依赖包
        include: [
          path.join(rootDir, 'src'),
          path.join(rootDir, 'index.web.ts'),
          /node_modules\/@react-native-firebase/,
          /node_modules\/react-native\/Libraries\/vendor/
        ],
        loader: 'ts-loader',
        options: {
          transpileOnly: true, // 可选:加快编译速度,跳过类型检查
          allowTsInNodeModules: true // 允许处理node_modules中的TS语法文件
        }
      },
      {
        test: /\.(png|jpe?g|gif)$/i,
        loader: 'file-loader',
      },
      {
        test: /\.css$/i,
        use: ['style-loader', 'css-loader'],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.join(__dirname, './index.html'),
    }),
    new webpack.HotModuleReplacementPlugin(),
  ],
  resolve: {
    extensions: ['.web.tsx', '.web.ts', '.tsx', '.ts', '.web.jsx', '.web.js', '.jsx', '.js'],
    alias: Object.assign({
      'react-native$': 'react-native-web',
    }),
  },
};

步骤2:修改tsconfig.json

开启allowJs选项支持解析JS文件中的TypeScript语法,并调整include/exclude规则:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "commonjs",
    "lib": ["esnext", "dom"],
    "jsx": "react",
    "noEmit": false,
    "strict": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": false,
    "resolveJsonModule": true,
    "allowJs": true // 新增:允许编译JS文件
  },
  "include": [
    "src/**/*",
    "index.web.ts",
    "node_modules/@react-native-firebase/**/*",
    "node_modules/react-native/Libraries/vendor/**/*"
  ],
  "exclude": [
    "node_modules/**/*",
    // 反向排除需要转译的依赖目录
    "!node_modules/@react-native-firebase/**/*",
    "!node_modules/react-native/Libraries/vendor/**/*"
  ]
}

步骤3:安装类型依赖(可选)

如果仍有类型报错,安装react-native的类型定义:

npm install @types/react-native --save-dev

验证

重新运行webpack编译命令,错误即可解决。若还有其他node_modules文件报错,只需将对应包路径添加到webpack和tsconfig的include列表中。

内容的提问来源于stack exchange,提问作者Avinash

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 07:27:36