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

tiny-di.js

v1.0.2

Published

Lightweight dependency injection container

Downloads

501

Readme

tiny-di.js

A lightweight, type-safe Dependency Injection container for TypeScript and Node.js. Define your dependencies programmatically with full type inference — no decorators required.

Features

  • 3 lifetime strategies — singleton, transient, and scoped (per-request)
  • Multiple provider types — class constructor, value, factory, and self-resolution
  • Automatic dependency injection — declare imports; the container resolves them
  • Circular dependency detection — DFS-based cycle detection with descriptive error messages
  • Scoped hierarchies — per-request scopes for Express or similar frameworks
  • Type-safe — full TypeScript generics from registration to resolution
  • No decorators — pure programmatic API

Installation

npm install tiny-di.js
# or
pnpm add tiny-di.js
# or
yarn add tiny-di.js

Quick Start

import { Container } from "tiny-di.js";

class Logger {
  log(msg: string) {
    console.log(msg);
  }
}

class Database {
  constructor(private logger: Logger) {}

  query(sql: string) {
    this.logger.log(`Executing: ${sql}`);
  }
}

const container = new Container();

container.singleton(Logger).toSelf();
container.singleton(Database).import(Logger).toSelf();

const db = container.get(Database);
db.query("SELECT 1"); // "Executing: SELECT 1"

API

Container

const container = new Container();

| Method | Description | |---|---| | singleton<T>(token) | Register a singleton provider (one instance per container) | | transient<T>(token) | Register a transient provider (new instance each time) | | scoped<T>(token) | Register a scoped provider (one instance per Scope) | | get<T>(token) | Resolve a dependency | | createScope(id?) | Create a child Scope |

EntryBuilder (fluent registration)

Every registration method returns an EntryBuilder that lets you chain configuration:

| Method | Description | |---|---| | .import(...tokens) | Declare dependencies to inject | | .to(class) | Map token to a class constructor | | .to(value) | Map token to an already-created value | | .toSelf() | Use the token itself as the constructor | | .toFactory(fn) | Provide a factory function | | .none() | Register with no provider (set later via scope) |

Lifetimes

  • Singleton — lazy on first get(). Same instance for every call.
  • Transient — always returns a fresh instance. Cannot be used with .to(value).
  • Scoped — cached inside a Scope object. Created via container.createScope().

Scopes

const scope = container.createScope("request-1");

// Scoped registrations are cached per scope
const a = scope.get(MyScopedService);
const b = scope.get(MyScopedService);
// a === b  (same scope)

// Different scope = different instance
const scope2 = container.createScope("request-2");
const c = scope2.get(MyScopedService);
// a !== c

Express Integration

import express, { Request } from "express";
import { Container, Scope } from "tiny-di.js";
import { scopedMiddleware, ScopedRequest } from "tiny-di.js/express";

const container = new Container();
container.scoped(AUTH_CTX).none();

const app = express();
app.use(scopedMiddleware(container));

app.use((req: ScopedRequest, _res, next) => {
  const scope = req.scope;
  scope.set(AUTH_CTX, { username: req.headers["x-user-id"] });
  next();
});

app.get("/me", (req: ScopedRequest, res) => {
  const ctx = req.scope.get(AUTH_CTX);
  res.json(ctx);
});

Circular Dependency Detection

If a cycle is detected, a descriptive error is thrown before a stack overflow can occur:

Error: Circular dependency: A => B => C => A

Tokens

Tokens can be a class constructor, a string, or a symbol:

container.singleton<Config>("CONFIG").to({ host: "localhost" });
container.singleton<Logger>(Symbol("logger")).to(Logger);
container.singleton(Database).toSelf();

Building

pnpm build

Testing

pnpm test

License

MIT