Ali Ali - 5 months ago 35
Node.js Question

Declare multiple module.exports in Node.js

What I'm trying to achieve is to create one module that contains multiple functions in it.

module.js:

module.exports = function(firstParam) { console.log("You did it"); },
module.exports = function(secondParam) { console.log("Yes you did it"); },
// This may contain more functions


main.js:


var foo = require('module.js')(firstParam);
var bar = require('module.js')(secondParam);


The problem I have is that the
firstParam
is an object type and the
secondParam
is a URL string, but when I have that it always complains that the type is wrong.

How can I declare multiple module.exports in this case?

Answer

You can do something like:

module.exports = {
    method: function() {},
    otherMethod: function() {}
}

Or even just:

exports.method = function() {};
exports.otherMethod = function() {};