Every time I start a node module, or a new JS project, I'm confronted with this same question: how do I structure it to allow it to be used in the browser or nodejs, without requiring a build step, and without getting in the way of my development?
While developing, I typically create some type of browser-based environment; Firebug and Web Inspector are still miles ahead of anything else we've got. Ideally I want to be able to add a file via a script
tag, and have that be the only requirement.
As @visionmedia points out, this is ridiculous.
This gist is meant to compile all of the various ways I've seen of guarding against/for a variety of module systems. Fork it and add your own, I'll try to bring them into this one.
This is how I do it in Lo-Dash, Benchmark.js, and Platform.js
The pattern works well with AMD optimizers, works in Ringo, Rhino, Narwhal, and Node.js too.