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

如何解决创建React web应用时出现Failed to compile ./src/App.js错误

问题根因

你的代码缺少了App类的闭合大括号,render()方法结束后的}仅用来闭合render函数本身,整个class App的外层大括号没有被正确闭合,导致解析器读到export default App;时认为属于class内部的非法语句,抛出语法错误。

修复方案

在第35行(也就是render方法闭合的}之后)添加一个}闭合App类即可,修复后的完整代码如下:

import React from 'react';
import './App.css';
import { client } from './client';

class App extends React.Component {
  state = {
    articles: []    
  }

  componentDidMount() {
    client.getEntries()
      .then((response) => {
        console.log(response)
      })
      .catch(console.error)
  }

  render()  {
    return (
      <div className="App">
        <div className='container'>
          <header>
            <div className='wrapper'>
            <span>React and contentful</span>
            </div>
          </header>
          <main>
            <div className='wrapper'>
            
            </div>
          </main>
        </div>
      </div>
  );
  } // 闭合render方法
} // 新增的App类闭合大括号
export default App;

修改后重新启动项目即可正常编译。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.10 08:45:04