Categories
JavaScript Answers

How to include Node.js ES6 classes with require?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *