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

报错:Class extends value #<Object> is not a constructor or null 求助排查

解决Class extends value #<Object> is not a constructor or null报错

问题原因

你遇到的报错核心是导入的不是可继承的类,而是普通对象:

  • item.js通过module.exports = { Item };导出的是包含Item类的对象,而非类本身
  • food.js里const Item = require('./item');拿到的是这个对象,尝试用class Food extends Item继承普通对象,自然触发该报错

解决方案

两种修正方式任选其一即可:

方式1:修改item.js的导出逻辑,直接导出类

class Item {
  constructor(name, description) {
    this.name = name;
    this.description = description;
  }
}

// 直接导出Item类,而非包裹它的对象
module.exports = Item;

方式2:修改food.js的导入逻辑,解构获取类

// 从导出的对象中解构取出Item类
const { Item } = require('./item');

class Food extends Item {
  constructor(name, description) {
    super(name, description);
  }
}

module.exports = { Food };

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 06:36:19