+1 to avoid the use of module.exports inside functions. It's better IMO to have a clear area where all the exports are made, if not, it would mislead people reading the code.