To include Node.js ES6 classes with require, we set module.exports
to the class.
Then we can use require
to import the class.
For instance, we write
class Animal {
//...
}
module.exports = Animal;
to set module.exports
to the Animal
class to export the class in Animal.js.
Then we write
const Animal = require("./Animal");
class Dog extends Animal {
//...
}
to require the Animal.js
file to import the class.
And then we create the Dog
class that inherits from the Animal
class.