Require and Export in NodeJs

(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com, or follow me on Twitter.

Interesting:

Finally, the last thing to consider is what happens when you directly export a function:

var powerLevel = function(level) {
return level > 9000 ? “it’s over 9000!!!” : level;
};
module.exports = powerLevel;

When you require the above file, the returned value is the actual function. This means that you can do:

require(‘./powerlevel’)(9050);

Which is really just a condensed version of:

var powerLevel = require(‘./powerlevel’)
powerLevel(9050);

Post external references

  1. 1
    http://openmymind.net/2012/2/3/Node-Require-and-Exports/
Source