npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ifdef

v1.0.0

Published

A simple guard against multiple requires.

Downloads

5

Readme

Ifdef

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:

  • The require cache goes bad (happens with some modules, but infrequent).
  • Multiple versions of the same module are required (way more frequent).

In this case, it'd be great to have a Node equivalent of #ifdef. That's what this module is for.

Installation

npm install ifdef --save

Usage

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)

Alternatives

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