Skip to content

Instantly share code, notes, and snippets.

@tamg
Last active Feb 18, 2017
Embed
What would you like to do?
//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