module.exports early, a simple pattern

Here's a very quick blog post about a pattern I started to use some time ago when creating node.js modules.

I often see code like this:

var func = function() {


var otherFunc = function() {


var working = 'yes';

// dozen of functions

// and at line 150..
module.exports = someFunc;  

What's wrong? Well I had to parse the entire file to see that utimately we were exporting some function. Not so great.

module.exports to the top

// immediately exports
module.exports = myFunc;

function myFunc() {


Here you go, write every module this way and you will quickly find that it's a lot easier to understand because you always know where is your export done: at the top.

Of course, for this to work, you need to use function declarations.

comments powered by Disqus