"Class extends value #<Object> is not a constructor or null"

Charlote22 picture Charlote22 · Apr 16, 2018 · Viewed 19.1k times · Source

Thanks for reading my post I get this error on my code : "Class extends value # is not a constructor or null" Here is my code, I'm trying to export/import classes.

monster.js :

const miniMonster = require("./minimonster.js");

class monster {
  constructor(options = { name }, health) {
    this.options = options;
    this.health = 100;
    this.heal = () => {
      return (this.health += 10);
    };
  }
}

let bigMonster = new monster("Godzilla");
console.log(bigMonster);

console.log(bigMonster.heal());

let mini = new miniMonster("Demon");
console.log(mini);
console.log(mini.heal());

module.exports = monster;

minimonster.js :

const monster = require("./monster.js");

class miniMonster extends monster {
  constructor(options) {
    super(options);
    this.health = 50;
    this.heal = () => {
      return (this.health += 5);
    };
  }
}

let miniM = new miniMonster("Jon");
console.log(miniM);

module.exports = miniMonster;

Thank you for any help given,

Have a good day

Answer

kigiri picture kigiri · Apr 16, 2018

I see at least one issue with your requires.

  • monster.js first line is const miniMonster = require("./minimonster.js");
  • minimonster.js first line is const monster = require("./monster.js");

This is a problem, you can not have both files evaluate at the same time. I would not require minimonster from monster.js

This may fix your issue.