Skip to content

Instantly share code, notes, and snippets.

@thelbouffi
Last active February 14, 2018 17:22
Show Gist options
  • Save thelbouffi/cafe68ea22625058ba0bc421fb339e38 to your computer and use it in GitHub Desktop.
Save thelbouffi/cafe68ea22625058ba0bc421fb339e38 to your computer and use it in GitHub Desktop.
module.exports Vs exports.method
// in exports_method.js
let add = (a, b) => a + b;
exports.add = add; // OR module.exports.add = add;
// in module_exports.js
let add = (a, b) => a + b;
module.exports = add;
// in caller.js
let module_exports = require('./module_exports');
let exports_method = require('./exports_method');
let test1 = module_exports.add(1,3);
console.log('module.exports result: ',test1);
let test2 = exports_method.add(1,3);
console.log('exports_method result: ',test2);
/**
*
==> module.exports = method
- when we use module.exports then it's a method that is exported
- so, wen we will make a require then it will be a function
x = require('../some_module_that_returns_a_function');
x is the functionBody
x() to call the required function
==> exports.method = method OR module.exports.method = method
- when we use exports.method then it's an object that is exported
- so, wen we will make a require then it will be an object
x = require('../some_module_that_returns_a_function');
x is an object where {functionName: functionBody}
x.functionName() to call the required function
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment