Reputation: 591
I often see that all modules used by an application (Express.js) are added in the very beginning of the index file "app.js". Like this:
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var mongo = require('mongodb');
var mongoose = require('mongoose');
And that's it, nothing deals with them in "app.js". They are used somewhere else, for example this modules may be required route file "/routes/login.js", where their addition is duplicating.
What's the matter of adding all modules in the "app.js" insted of adding them only where it really needed? Is it a part of convention or there are some real needs?
Upvotes: 1
Views: 40
Reputation: 2564
Most likely people begin writing their project in a single file. They include everything there, then, when they split code into multiple files, they forget to remove the require
s.
The only semi-real benefit I can think of is preloading the modules at the beginning, because technically when you require a module twice it should resolve to the same object and all of its' initialization should execute only once. But that's stretching it, really. I think people just forget.
Upvotes: 3