I have multiple modules and I did:
// module1.js
module.exports = function() {
...
}
// module2.js
module.exports = function() {
...
}
in app.js
m1 = require('./module1')
m2 = require('./module2')
m1.method()
m2.method()
I get TypeError. Then I ended up exporting methods in both the modules.
is there a way I can export multiple modules other than exporting individual methods explicitly?
It looks like you're attempting to pass require() an undefined variable two times. require() needs to take a string as an argument to determine what module you want to load.
If the other two modules are in the same directory as app.js, try
m1 = require('./module1')
m2 = require('./module2')
EDIT: What you're forgetting to do is
m1 = new require('./module1')()
m2 = new require('./module2')()
Assuming that you modules look like:
module.exports = function() {
this.method = function(){}
}
Personally, instead of a function I would just return an object literal from my module:
module.exports = {
method1: function(){},
method2: function(){}
}
Then I could invoke the methods from the module's export like such:
m1 = require('./module1');
m1.method1();