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

@ludeschersoftware/singleton

v1.0.4

Published

A enforcable Singleton class

Readme

A lightweight, enforceable Singleton base class for TypeScript, designed for correctness, testability, and strong typing.

This package provides a clean alternative to traditional singleton implementations by disabling constructors, enforcing a single instance per subclass, and exposing an explicit lifecycle method for initialization.

It combines strict runtime guarantees with a minimal, framework-agnostic API.


✨ Key Concepts

  • No constructors – direct instantiation is impossible
  • Exactly one instance per subclass
  • 🧠 Fully type-safe getInstance() API
  • 💤 Lazy initialization (created on first access)
  • 🔁 Explicit lifecycle hook instead of constructors
  • 🧪 Resettable instances for deterministic tests
  • 🪶 Zero dependencies

📦 Installation

npm install @ludeschersoftware/singleton
# or
yarn add @ludeschersoftware/singleton

🚀 Basic Usage

Extend the Singleton base class and implement the initialize() lifecycle method.

import { Singleton } from '@ludeschersoftware/singleton';

class ConfigService extends Singleton {
  public config!: Record<string, string>;

  protected initialize(): void {
    this.config = {
      env: 'production',
    };
  }
}

const a = ConfigService.getInstance();
const b = ConfigService.getInstance();

console.log(a === b); // true
console.log(a.config.env); // 'production'

Attempting to instantiate a singleton directly will fail:

new ConfigService();
// ❌ Error: A Singleton does not have a constructor!

🔁 Initialization with Parameters

getInstance() supports parameters that are forwarded to initialize() exactly once, when the instance is first created.

class Database extends Singleton {
  public url!: string;

  protected initialize(url: string): void {
    this.url = url;
  }
}

const db = Database.getInstance('postgres://localhost');

// Subsequent calls ignore parameters and return the same instance
Database.getInstance('ignored');

⚠️ Parameters are only respected on the first call to getInstance().


🛠️ API Reference

abstract class Singleton

Base class for all singletons.


static getInstance<T>(...params): T

Returns the singleton instance of the subclass.

  • Creates the instance lazily
  • Guarantees exactly one instance per subclass
  • Calls initialize(...params) exactly once
  • Fully type-safe for subclasses
const instance = MySingleton.getInstance(...args);

protected initialize(...params): void

Lifecycle hook invoked once when the singleton is first created.

  • Replaces constructor logic
  • Receives parameters from getInstance()
  • Must be implemented by subclasses
protected initialize(config: Config): void {
  // setup logic
}

static _resetInstance(): void

Deletes the current singleton instance.

  • Intended only for testing
  • Allows deterministic test isolation
  • Safe to call multiple times
MySingleton._resetInstance();

🧪 Testing Example

class TestService extends Singleton {
  public initialized = false;

  protected initialize(): void {
    this.initialized = true;
  }
}

describe('TestService', () => {
  afterEach(() => {
    TestService._resetInstance();
  });

  it('creates a fresh instance per test', () => {
    const instance = TestService.getInstance();
    expect(instance.initialized).toBe(true);
  });
});

🧩 Common Use Cases

  • Application-wide configuration
  • Logging and telemetry services
  • Connection pools
  • Caches and registries
  • Cross-module shared state (without globals)

⚙️ Design Notes

  • Instances are stored internally using a Symbol, preventing accidental overrides
  • No reliance on global variables
  • Runtime enforcement ensures misuse fails fast
  • Compatible with ESM and CommonJS builds

📄 License

MIT © Johannes Ludescher


💬 Feedback & Contributions

Issues, ideas, and pull requests are welcome. If you have suggestions or edge cases to discuss, feel free to open an issue.