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

getbox

v2.0.0

Published

Lightweight dependency injection for TypeScript

Downloads

46

Readme

getbox

Lightweight dependency injection for TypeScript.

getbox is a lightweight inversion of control container for TypeScript. Constructors declare their own dependencies, keeping instantiation logic colocated with the type that owns it.

Callers depend on types, not implementations. The box resolves and caches instances automatically on first use.

Installation

npm install getbox

Usage

getbox has a very small API surface. You typically only need box.get() and optionally static init or the factory helper.

For an alternative pattern using AsyncLocalStorage where classes can resolve dependencies directly, see getbox/context.

Quick start

Create a Box instance and call box.get() to resolve instances. The box automatically resolves dependencies and caches every instance, so shared dependencies always point to the same reference.

import { Box } from "getbox";

class Printer {
  print(text: string) {
    return text.toUpperCase();
  }
}

class Office {
  constructor(public printer: Printer) {}

  static init = Box.init(Office).get(Printer);
}

const box = new Box();

const office = box.get(Office);
office.printer.print("hello world");

// Dependencies are cached and shared
const printer = box.get(Printer);
console.log(office.printer === printer); // true

Constructors

Constructors define what the box resolves. getbox supports classes, factories, computed values, and constants. Because constructors act as interfaces, the underlying implementation can change without affecting any consumer.

Classes

Define a static init property to allow the box to resolve classes that have constructor parameters. Classes with no parameters do not require it.

import { Box } from "getbox";

class UserService {
  constructor(private db: Database, private logger: Logger) {}

  static init = Box.init(UserService).get(Database, LoggerFactory);

  createUser(name: string) {
    this.logger.log(`Creating user: ${name}`);
  }
}

const box = new Box();
const service = box.get(UserService);
service.createUser("Alice");

Box.init is shorthand for writing the static init function yourself.

class UserService {
  constructor(private db: Database, private logger: Logger) {}

  static init(box: Box) {
    return new UserService(box.get(Database), box.get(LoggerFactory));
  }
}

If static init is defined, it takes priority over the class constructor.

Factory functions

Use the factory helper to create function-based constructors instead of classes. Factories work well with interfaces for better abstraction.

import { Box, factory } from "getbox";

interface Logger {
  log(message: string): void;
}

const LoggerFactory = factory<Logger>(() => ({
  log(message: string) {
    console.log(`[LOG] ${message}`);
  },
}));

const box = new Box();
const logger = box.get(LoggerFactory);

logger.log("hello world");

Computed values

Use the computed helper to compute a value from the box without caching the result.

import { Box, computed } from "getbox";

class Config {
  baseUrl = "https://example.com";
}

const RequestContext = computed((box) => ({
  baseUrl: box.get(Config).baseUrl,
  timestamp: Date.now(),
}));

const box = new Box();

const ctx1 = box.get(RequestContext);
const ctx2 = box.get(RequestContext);

console.log(ctx1 === ctx2); // false

Constants

Use the constant helper to wrap a fixed value as a constructor. Constant values are already fixed and do not need caching.

import { Box, constant } from "getbox";

const ApiUrl = constant("https://api.example.com");
const Port = constant(3000);
const Config = constant({
  baseUrl: "https://example.com",
  timeout: 5000,
});

const box = new Box();

console.log(box.get(ApiUrl)); // "https://api.example.com"
console.log(box.get(Port)); // 3000
console.log(box.get(Config).timeout); // 5000

Resolving instances

Use box.get() to resolve a cached instance. The box resolves the constructor on first call and returns the same instance on every subsequent call. Use box.new() to always get a new instance without caching.

import { Box } from "getbox";

class Database {
  connect() {
    /* ... */
  }
}

const box = new Box();

const db1 = box.get(Database);
const db2 = box.get(Database);
console.log(db1 === db2); // true (cached)

const db3 = box.new(Database);
const db4 = box.new(Database);
console.log(db3 === db4); // false (never cached)

Both box.get() and box.new() also accept an array or object of constructors to resolve multiple at once.

const box = new Box();

// Cached
const { db, logger } = box.get({ db: Database, logger: LoggerFactory });
const [db2, logger2] = box.get([Database, LoggerFactory]);

// New instances
const { db3 } = box.new({ db3: Database });
const [db4] = box.new([Database]);

box.get() does not cache computed or constant values.

Mocking

You can mock dependencies for testing using Box.mock.

// service.test.ts
import { Box } from "getbox";
import { Logger, LoggerFactory } from "./logger";
import { UserService } from "./service";

class MockLogger implements Logger {
  messages: string[] = [];

  log(message: string): void {
    this.messages.push(message);
  }
}

const box = new Box();
const mockLogger = new MockLogger();
Box.mock(box, LoggerFactory, mockLogger);

const service = box.get(UserService);
service.createUser("Alice");

console.log(mockLogger.messages); // ["Creating user: Alice"]

Clearing the cache

Use Box.clear to remove cached instances. Pass a specific constructor to clear a single entry, or omit it to clear all cached instances.

Returns true if an instance was removed, false otherwise.

const box = new Box();

const db = box.get(Database);

// Clear a specific constructor
const cleared = Box.clear(box, Database);
console.log(cleared);
console.log(box.get(Database) === db); // false (new instance)

// Clear all cached instances
const clearedAll = Box.clear(box);
console.log(clearedAll);

Circular dependencies

getbox does not prevent circular dependencies. You should structure your code to avoid them.

License

MIT