Global variables for standard node.js modules?

I know that global variables are bad.

But if I use the node module "util" in 40 files in my structure, is it not better to simply declare it as a global variable of the type:

util = require('util'); 

in the index.js file instead of writing this line in 40 files?

Because I often use the same 5-10 modules in each file, which would save a lot of time, and not copy all the time.

In this case, there is no DRY?

+52
javascript module global-variables shared-libraries
Nov 10 '10 at 2:14
source share
6 answers

Each module must be independent. The requirement will not cost anything in any case after the first for each module.

What if you want to test one module? You will have a lot of problems because he will not know that some of the "global" ones require you to have in your application.

Yes, globals are bad, even in this case. Globals almost always collapse: verifiability, encapsulation, and ease of maintenance.

Updated answer January 2012

The global object is now global inside each module. Therefore, every time you assign a global variable (without scope) inside the module, which becomes part of the global object of this module.

The global object is therefore not global and cannot be used as such.

Updated December 2012

The global object now has a global scope of the application and can be used to store any data / functions that should be accessible from all modules.

+44
Nov 10 '10 at 5:57
source share

You can only have a common module.

common.js:

 Common = { util: require('util'), fs: require('fs'), path: require('path') }; module.exports = Common; 

app.js:

 var Common = require('./common.js'); console.log(Common.util.inspect(Common)); 
+99
Nov 24 '10 at 10:37
source share
 global.util = require('util'); 

This section contains global objects in the node documentation .

However, globals should be used with caution. By adding modules to the global space, you reduce testability and encapsulation. But there are times when using this method is acceptable. For example, I am adding functions and objects to the global namespace for use in my unit test scripts.

+25
Nov 28 '10 at 1:15
source share

I am confused by the answers in this thread.

I can do it...

File: test.js

 global.mytest = { x: 3, y: function() { console.log('Works.'); } }; 

File: test2.js

 console.log('Does this work?'); mytest.y(); 

File: server.js

 require('test.js'); require('test2.js'); 

And it seems to work as a necessary question. The first requires that the mytest object fall into the global scope, then the second requires access to this object without any other qualifiers.

I was trying to figure this out (which led me to this topic from a Google search), and I wanted to post what seems to work for me now. Perhaps the situation has changed since the initial responses.

+20
Jan 31 '12 at 22:17
source share

I have successfully used the process object to pass my configuration object. Theoretically, given the same problems that were mentioned above (encapsulation, testability, etc.), it works fine when using only non-state modifying properties (a hash table with primitives mainly).

0
Jun 23 '11 at 12:22
source share

If you wrap your modules in blocks (for example, anon functions), you can bind to a local name (via a parameter or "var"), and then have any arbitrary long (possibly "packed") name that you want (if you even at this moment you need global).

For example, my modules often look like:

 ;(function ($, $exp, other) { $(...) other.xyz() $exp.MyExportedObject = ...; })(jQuery, window, some_module.other_expression) // end module 

I use jQuery with noConflict, this is the first and the last shows that you can do this for any expression - global, required, computed, built-in, regardless ... the same β€œwrapping” approach can be used to exclude everything (or almost all) "special named" global "- globals must exist at some level, however, the removal of possible conflicts is a very big victory.

-3
Nov 10 '10 at 2:29
source share



All Articles