Reputation: 7073
As you all know that the require(...) method is sync and can take some time to execute. Thus, it is advisable to load all require on the top.
I am using more than 15 modules. Now instead of writing multiple require statements, I am thinking of the following logic:
foreach(var module in node_modules)
GLOBAL.app.npm[module] = require(module);
The question that I have is how you are handling the same?
Are you writing 15 require statements or any dynamic mechanism?
thanks,
Upvotes: 2
Views: 249
Reputation: 163280
As you all know that the require(...) method is sync and can take some time to execute.
Why does it matter? Remember that the result of require()
is cached.
This isn't really an issue in practice unless you are doing something strange in your application. And again, you don't have to put it in global, due to the caching. Just require once anywhere that runs up front and you're good to go.
I am using more than 15 modules
Not a big deal. Again, most applications are going to load when they all load anyway. If I require a file that requires a file that requires a file, they're all loaded immediately, assuming your require statements are up top (or anywhere in that initial execution really) like normal.
Unless you are only requiring when a specific request comes in, then you have nothing to worry about. (And if you are doing that, I'd imagine you would have a special reason to do so.)
Upvotes: 2