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 🙏

© 2026 – Pkg Stats / Ryan Hefner

js-singleton

v0.0.5

Published

A singleton wrapper for creating single instances of functions.

Readme

js-singleton

A singleton wrapper for creating single instances of functions.

This module can wrap an arbitrary number of functions in the same Singleton meaning you can init everything you need and then just use require('js-singleton'); in other modules and it will work just fine.

Examples

Simple example in multi mode

const Singleton = require('js-singleton')();

function Person(firstname, lastname){
  this.firstname = firstname;
  this.lastname = lastname;
}

var person = Singleton.getInstance(Person, 'George', 'Clooney');
console.log(person.firstname, person.lastname); // Prints George Clooney

var anotherPerson = Singleton.getInstance(Person, 'Michael', 'Jackson');
console.log(anotherPerson.firstname, anotherPerson.lastname); // Prints George Clooney

var anotherPersonMaybe = Singleton.getInstance(Person);
console.log(anotherPersonMaybe.firstname, anotherPersonMaybe.lastname); // Prints George Clooney

Simple example in single mode


function ff(d){
	this.d = d;
}

const SingletonSingle = require('js-singleton')(ff, 125);
const Singleton = require('js-singleton')();

var i1 = SingletonSingle.getInstance();
var i2 = SingletonSingle.getInstance();
var i3 = Singleton.getInstance(ff);

console.log(i1.d); // Prints 125
console.log(i2.d); // Prints 125
console.log(i3.d); // Prints 125

Example with getInstance() by passing a string alias of the function via setName()

function Complex(a, b){
  this.a = a;
  this.b = b;
}

var complex = Singleton.getInstance(Complex, 1, 2);
const SingletonSingle = require('js-singleton')(Complex);
Singleton.setName("MyCustomComplexClass", Complex);
SingletonSingle.setName("MyCustomComplexClass"); // Equivalent to previous line

var anotherComplex = Singleton.getInstance("MyCustomComplexClass");

var someOtherComplex = SingletonSingle.getInstance();

console.log(anotherComplex.a, anotherComplex.b); // Prints 1 2;
console.log(complex.a, complex.b); // Prints 1 2;
console.log(someOtherComplex.a, someOtherComplex.b); // Prints 1 2;

Example with creating multiple aliases for the same function via setName()

const Singleton = require('js-singleton')();
function Door(height, width){
  this.height = height;
  this.width = width;
}

var door = Singleton.getInstance(Door, 1.1, 2.3);
Singleton.setName("door", Door);
Singleton.setName("door", "otherDoorAlias");

var anotherDoor = Singleton.getInstance("door");

var anotherDoorMaybe = Singleton.getInstance("otherDoorAlias");

const SingletonSingle = require('js-singleton')("otherDoorAlias)";
var aDoorAlso = SingletonSingle.getInstace();

console.log(door.width, door.height); // Prints 2.3 1.1
console.log(anotherDoor.width, anotherDoor.height); // Prints 2.3 1.1
console.log(anotherDoorMaybe.width, anotherDoorMaybe.height); // Prints 2.3 1.1
console.log(aDoorAlso.width, aDoorAlso.height); // Prints 2.3 1.1

Real world example

/**
 *
 * This module is the server module (server.js)
 * 
 * @author Some Author
 *
 */

function server(options){
	// Some beautiful code
}

var options = {}; // some options

const Singleton = require('js-singleton')();
Singleton.getInstance(server, options);
// equivalent
const SingletonSingle = require('js-singleton')(server, options);

Singleton.setName("myServer", server);
// equivalent
SingletonSingle.setName("myServer");

// No need to module.export

/*
 *
 * In another file do
 *
 */
require('/path/to/server');
const Singleton = require('js-singleton')();
var myServer = Singleton.getInstance("myServer");
// equivalent
const SingletonSingle = require('js-singleton')("myServer");
var myServer = SingletonSingle.getInstance();