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

React代码生产环境编译报错:ReactDOM.render语法异常求助

Fixing JSX Syntax Error in React Production Build

Hey, let's get this production build error sorted out! The root cause here is that your webpack configuration for production isn't set up to transpile JSX syntax. Your local dev environment likely handles this automatically (maybe via create-react-app's default setup or a dev-specific config), but the production build is missing the necessary Babel tools to convert that <App /> JSX into browser-readable JavaScript.

Here's how to fix it:

Step 1: Install Required Babel Dependencies

First, make sure you have all the necessary Babel packages installed as dev dependencies. Run this command in your project root:

npm install --save-dev babel-loader @babel/core @babel/preset-react @babel/preset-env

Step 2: Update Your Webpack Configuration

Open your webpack config file (usually named webpack.config.js) and add a module rule to process .js and .jsx files with Babel. Add this inside the module section:

module: {
  rules: [
    // Other rules (like CSS loaders) go here
    {
      test: /\.(js|jsx)$/,
      exclude: /node_modules/,
      use: {
        loader: 'babel-loader',
        options: {
          presets: ['@babel/preset-env', '@babel/preset-react']
        }
      }
    }
  ]
}

This tells webpack to use babel-loader for all React/JSX files, and uses the presets to transpile modern JS and JSX down to browser-compatible code.

Step 3: Verify Babel Configuration (Optional)

If you have a .babelrc file in your project root, ensure it includes the same presets to keep configuration consistent:

{
  "presets": ["@babel/preset-env", "@babel/preset-react"]
}

Step 4: Run the Production Build Again

Now try running your production build command again (something like webpack --mode production). The JSX syntax should be properly transpiled, and the error should disappear.

A quick note: Your local dev server might have been using a built-in setup (like react-scripts in create-react-app) that handles JSX transpilation out of the box, but custom webpack configs for production require explicit setup for this.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:33:14