
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
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:
require cache 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 require statement:
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 mongoose:
if (ifdef('GUARD_TERM')) {
return module.exports = ifdef('GUARD_TERM')
}
Following this block, build out your singleton. The third block of ifdef
code, then, assigns this singeton as the guarded value. If you have not already
assigned it to module.exports:
module.exports = ifdef('GUARD_TERM', SINGLETON)
Notice that 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:
ifdef('GUARD_TERM', module.exports)
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_name) {
return module.exports = global.__my_module_name
}
// Initialization here
module.exports = global.__my_module_name = SINGLETON
FAQs
A simple guard against multiple requires.
The npm package ifdef receives a total of 2 weekly downloads. As such, ifdef popularity was classified as not popular.
We found that ifdef demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.