Reputation: 303
If I have this hello.js file:
var greeting = function() {
console.log('Hello');
}
module.exports = greeting;
Then in main.js:
var temp = require('./hello.js');
temp();
When you say module.exports = greeting
is that attaching the greeting function to the exports object on module. Since when I require hello.js
in main.js
I am able to call temp() directly. And don't have to do like temp.greeting();
Does this mean that since require
returns module.exports
it just returning the method on the exports object rather than returning the exports object entirely correct? I am confused on why it is returning what is on the exports object (the greeting function) and not the real exports object itself.
Upvotes: 0
Views: 69
Reputation: 318468
require(...)
returns module.exports
from that module. This is usually an object, but it can also be anything else (usually a function) like in your case where the module exports only a single function.
There's nothing wrong with doing this - module.exports
is just a plain object (there's most likely something like module.exports = {};
somewhere in the code that runs "around" the contents of a module's js file)
Upvotes: 1