2

I was trying to add two module.exports in my NodeJS module. I tried below code for my module:

exports.one = function(data){
    const one = data;
};
module.exports = function(msg) {
    console.log(one+'::'+msg);
};

And below code for index.js:

var myModule = require('./mymodule.js');


myModule.one('hi');
myModule('bro');
myModule('Dear');
myModule('Dude');

I was expected that it will log below data into the console:

hi bro
hi Dear
hi Dude

But the console says:

TypeError: myModule.one is not a function
    at Object.<anonymous> (....

Please how do I solve this issue? There are Stack Overflow questions asking how to use multiple module.exports in NodeJS modules. But the answer is something like below:

exports.one = function (){};
exports.two = function (){};

But if I use that code, I have to use

myModule.one('hi');
myModule.two('bro');
myModule.two('Dear');
myModule.two('Dude');

Instead of:

myModule.one('hi');
myModule('bro');
myModule('Dear');
myModule('Dude');
NodeDev
  • 43
  • 1
  • 7

2 Answers2

5

You seem to be looking for

let one = '';
module.exports = function(msg) {
    console.log(data+'::'+msg);
};
module.exports.one = function(data){
    one = data;
};

Notice that the exports variable is just an alias for the module.exports object, and when overwriting that with your function you threw away its contents. You will need to put the one method on your main function.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Great idea! But there's a small problem. I said, configurations save at `myModule.one('hi');` is used while running `myModule('bro');` where `myModule('bro');` is defined first in the module. – NodeDev Jul 25 '18 at 14:17
  • Watch out the line saying const one = data. I was going to use it in the module.exports function which is actually located above module.exports.one function. – NodeDev Jul 25 '18 at 14:18
  • When I tried `module.exports = function(msg) { console.log(one+'::'+msg); }; module.exports.one = function(data){ const one = data; };`, an error logs to console saying **ReferenceError: one is not defined** – NodeDev Jul 25 '18 at 14:20
  • 1
    @NodeDev It doesn't matter in when the functions are defined, it matters when they are called. As long as they are only called after all of them were defined, there is no problem with calling the one from the other. – Bergi Jul 25 '18 at 14:21
  • @NodeDev Of course it's not defined - `const one` is a *local* variable. You might want to put it at the module level scope if you want to access it from both functions, although then you'll need to use `let` not `const`. – Bergi Jul 25 '18 at 14:23
  • Oh! Now I got it. Now everything works fine. Please edit your answer and put `let one; module.exports = function(msg) { console.log(one+'::'+msg); }; module.exports.one = function(data){ one = data; };` as it includes the entire codes and it may help others. – NodeDev Jul 25 '18 at 14:25
  • @NodeDev Btw, notice that this kind of global configuration is relatively discouraged. It would be better to allow the creation of multiple different instance, e.g. with `var myModule = require('./mymodule.js').one("hi"); myModule("bro")`. – Bergi Jul 25 '18 at 14:29
  • Great! And thanks a lot! I was trying to solve this issue for almost half an hour and now, everything is solved – NodeDev Jul 25 '18 at 14:33
0

When you do the following:

module.exports.one = function(data){
    const one = data;
};
module.exports = function(msg) {
    console.log(data+'::'+msg);
};

You first assign the property one to the module object (exports.one=...). Then you reassing the whole module.exports object with the second line and thus erasing the first function you asssinged to it.

You can solve it like this:

module.exports.one = function(data){
    const one = data;
};
module.exports.two = function(msg) {
    console.log(data+'::'+msg);
};

Then you can call the functions in your other module like this:

var myModule = require('./mymodule.js');
myModule.one(arg1) // calls first function
myModule.two(arg2) // calls second function
Willem van der Veen
  • 33,665
  • 16
  • 190
  • 155
  • Yeah but first function is made for assigning some configurations to the app/module. Then using myModule('bro'); myModule('Dear'); myModule('Dude'); I wants to run the main function, which is depended on the configuration that is assigned by myModule.one(config); – NodeDev Jul 25 '18 at 14:07