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

如何解决新建React应用初始化时出现的TypeError报错问题?

React项目初始化TypeError报错解决方案

报错提示:TypeError: Class extends value undefined is not a constructor or null
报错截图:
报错语法示意

对应修复方法

  • 场景1:使用create-react-app脚手架创建项目时触发报错

    该场景90%以上是脚手架版本和Node版本不兼容导致,按以下步骤操作:
    1. 卸载全局安装的旧版create-react-app
      npm用户执行:
      npm uninstall -g create-react-app
      yarn用户执行:
      yarn global remove create-react-app
    2. 直接调用最新版脚手架创建项目,无需全局安装:
      npx create-react-app@latest 你的项目名称
    3. 如果执行后仍报错,清除npm缓存后重试:
      npm cache clean --force
  • 场景2:手动配置React项目时触发报错

    该场景是类组件继承写法错误导致,检查你的组件代码:
    错误写法示例:
    // 漏写Component拼写错误/导入遗漏
    import React from 'react'
    class App extends React.Componet {}
    
    修正为正确写法即可:
    import React from 'react'
    class App extends React.Component {}
    
    // 解构导入写法也需要确保拼写正确
    import { Component } from 'react'
    class App extends Component {}
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 14:54:04