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:
- connect = require 'connect'
- express = require 'express'
- redis = require 'redis'
- sys = require 'sys'
- coffee = require 'coffee-script'
- fs = require 'fs'
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.
- .split(',').forEach (lib) -> global[lib] = require lib
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:
- _ = underscore