sfarzoso
sfarzoso

Reputation: 1610

How to export object from module.exports?

I need to require a file passing a parameter, for this I used the following syntax:

module.exports = function(bot) {
    const menu = new TelegrafInlineMenu(bot);
    return menu;
};

the problem's that the code above export the function, I need to return the menu object, is there a way to do this?

I require the script using:

const menu = require('menu')(bot);

problem's that menu is a function not an object

Upvotes: 0

Views: 48

Answers (1)

samthecodingman
samthecodingman

Reputation: 26296

function TelegrafInlineMenu(bot) {
  // constructor
  if (!(this instanceof TelegrafInlineMenu)) {
    return new TelegrafInlineMenu(bot);
  }
}

TelegrafInlineMenu.prototype.someFunction = function () {
  // etc.
};

module.exports = TelegrafInlineMenu;

Upvotes: 1

Related Questions