node.js - How bad it is to use global variables in nodejs? -
i need libraries (mongo, async ...) , core function in every files of code. far has understand, loading module expensive process. plus, writing x more line of code in every file pain.
so wouldn't smarter require once, when application start ?
i know global variables bad in general, how bad in context ?
it always bad (intentionally uppercase) idea use global variables, should watch out solution.
since modules cached when loaded, not expensive. not matter whether once or multiple times.
so easiest way require
module everywhere need it.
now, module needs configuration. need make sure done when module loaded first time. this, create wrapper module this:
var foo = require('foo'); foo.configure(options); // or whatever need here configure foo. module.exports = foo;
now can use this:
var configuredfoo = require('./configuredfoo');
this should trick.
Comments
Post a Comment