Skip to content

Instantly share code, notes, and snippets.

@tamg
Last active February 18, 2017 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamg/7cb367d7c1fbaae866dcfc434abe397f to your computer and use it in GitHub Desktop.
Save tamg/7cb367d7c1fbaae866dcfc434abe397f to your computer and use it in GitHub Desktop.
//greet.js
var greet = function() {
console.log('Hello!');
};
module.exports = greet;
//app.js
var greet = require('./greet');
greet();
/**
- The require function takes the path of the greet module as a parameter
- The greet function is then wrapped inside another function
- Wrapping our module code protects variables and functions defined inside our module
- Thus module.exports becomes the way in which we expose our module
The newly wrapped greet module then becomes...
(function (exports, require, module, __filename, __dirname) {
var greet = function() {
console.log('Hello!');
};
module.exports = greet;
});
- Then Node invokes the wrapped function by passing 4 parameters
fn(module.exports, require, moule, filename, dirname);
- Then require returns module.exports (which we have set inside our module file)
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment