I was browsing the source code for a command line utility in Node, and saw the following code.
function help() {
var colors = require('colors');
var package = require('../package');
....
....
}
I had not seen require being used inside a function in this way before. I always assumed it was best practice to include it at the top of the file. This is the entry file for this program, and this function is only called in a specific case--but those packages are used elsewhere in the program. When I asked the author of the code for his reasoning, he simply stated that he "didn't want to import all the libraries at once."
Is this good/bad practice? Are there significant implications to load-up time by not requiring these packages at the top of the module, and instead only when these functions are invoked?