To include JavaScript class definition from another file in Node.js, we set the class as the value of module.exports
.
For instance, in user.js, we write
class User {
//...
}
module.exports = User;
to export the User
class.
Then in app.js, we write
const User = require("./user.js");
let user = new User();
to import the user.js file with required
.
And then we create a User
object.