A simple guard against multiple requires.
A common pattern in Node is to store singletons within a Module's closure. This is an extremely powerful pattern, and its simplicity leaves little to be desired. However, it's not uncommon for this pattern to break when:
requirecache goes bad (happens with some modules, but infrequent).
In this case, it'd be great to have a Node equivalent of
#ifdef. That's what
this module is for.
npm install ifdef --save
From within your singleton module code, you need five lines of code. The first
line is your standard
var ifdef = require'ifdef'
The second three are a check against existing guards, returning the guarded
value if necessary. Here,
GUARD_TERM should be a term unique to your module.
The name provided in
package.json is unique across modules, and should be
considered. If you're worried about collision, don't be afraid to "decorate"
it a little, i.e.
global_mongoose_connection instead of
if ifdef'GUARD_TERM'return moduleexports = ifdef'GUARD_TERM'
Following this block, build out your singleton. The third block of
code, then, assigns this singeton as the guarded value. If you have not already
assigned it to
moduleexports = ifdef'GUARD_TERM' SINGLETON
ifdef just returns the guarded term, making this a one-liner.
If you've already assigned what you want in
module.exports, it's even simpler:
As far as I know, there aren't any independent of writing your own guard. If I'm wrong, please let me know and I'll add them here. As far as writing your own is concerned, here's what it looks like:
if global__my_module_namereturn moduleexports = global__my_module_name// Initialization heremoduleexports = global__my_module_name = SINGLETON