Require
When require is given a path of a folder, nodejs will look for index.js file in that folder. If index.js exists, nodejs uses it. Otherwise it causes an error.
Module.exports and require
module.exports
is used to export a module, which exposes methods and properties as interfaces to be called. require
is to import a module. Principly, module.exports
and require
work as below:
module.exports = {
greet: function () { return 0; }
}
require = function (filepath) {
// interpret the file and return the assigned module.exports
...
return module.exports;
}