I have a module as follows:
class mailer() {
constructor() {
this.template = "";
this.email = "";
}
setTemplate(tplName) {
this.template = tplName;
return this;
}
sendEmail(email) {
//some stuff
}
}
module.exports = mailer;
And to consume that, well you just do:
const mailer = require('mymodule');
(new mailer()).setTemplate("foo").sendEmail('foo@bar.com');
The reason why I'm using a class is because several processes can consume that module at the same time so I need the variables to be isolated. This works as intended, but I will like to make the code cleaner by removing if possible the "new" part. I can export an instance of the class instead of exporting the class itself but then Node will cache after the first require and my code will be using the same instance every time.