Update: See the follow-up on StackOverflow. I agree with those who rallied against use of global, but no need to throw the baby out with the bathwater; as shown in that thread, we can instead use “eval” to keep it DRY.

You see a lot of NodeJS code like this:

[javascript] connect = require ‘connect’ express = require ‘express’ redis = require ‘redis’ sys = require ‘sys’ coffee = require ‘coffee-script’ fs = require ‘fs’ [/javascript]

BTW this is CoffeeScript, not pure JavaScript, but the same logic applies.

The point is, Node’s module system gives you a layer of abstraction you probably don’t need. 9 times out of 10, people just assign a module to a variable by the same name. You would, wouldn’t you?!! You don’t have to think about alternatives, and you can easily cut and paste other people’s code. And it’s now just an industry convention.

So why not practice convention over configuration, and keep things DRY with the following idiom:

[javascript] “underscore,connect,express,redis,sys,coffee-script,fs” .split(‘,’).forEach (lib) -> global[lib] = require lib [/javascript]

Thanks to the “global” object, which is like the “window” object in the browser, we can easily require new libraries without unnecessary clutter.

And - as with any decent convention-over-configuration setup - we can still override the convention if we desire:

[javascript] _ = underscore [/javascript]