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

@stevanfreeborn/netdi

v1.0.1

Published

A dependency injection library inspired by the .NET DI container.

Readme

netdi

A dependency injection container for TypeScript projects that is inspired by the .NET DI container. It is designed to be simple, lightweight, and easy to use.

pull_request codecov publish semantic-release: angular NPM License NPM Version NPM Downloads

Features

  • ✅ .NET-style service registration with different lifetimes (singleton, scoped, transient)
  • ✅ Constructor injection with decorators
  • ✅ Type-safe dependency resolution
  • ✅ Factory method support for complex instantiation scenarios
  • ✅ Scoped service lifetimes for per-request contexts

Installation

# npm
npm install @stevanfreeborn/netdi

# yarn
yarn add @stevanfreeborn/netdi

# pnpm
pnpm add @stevanfreeborn/netdi

Requirements

  • Node.js >= 18.0.0
  • TypeScript with decorators and reflection metadata enabled

Add the following to your tsconfig.json:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Basic Usage

Here's a simple example of how to use netdi:

import {
  ServiceCollection,
  createServiceIdentifier,
  injectable,
  inject,
} from '@stevanfreeborn/netdi';

// Define interfaces
interface IGreeter {
  greet(name: string): string;
}

interface IGreetingService {
  createGreeting(name: string): string;
}

// Create service identifiers
const greeterIdentifier = createServiceIdentifier<IGreeter>();
const greetingServiceIdentifier = createServiceIdentifier<IGreetingService>();

// Implement services
@injectable()
class GreetingService implements IGreetingService {
  createGreeting(name: string): string {
    return `Hello, ${name}!`;
  }
}

@injectable()
class Greeter implements IGreeter {
  constructor(@inject(greetingServiceIdentifier) private greetingService: IGreetingService) {}

  greet(name: string): string {
    return this.greetingService.createGreeting(name);
  }
}

// Set up dependency injection
const services = new ServiceCollection();

// Register services
services.addSingleton(greetingServiceIdentifier, GreetingService);
services.addScoped(greeterIdentifier, Greeter);

// Build service provider
const serviceProvider = services.build();

// Resolve and use a service
const greeter = serviceProvider.getService(greeterIdentifier);
console.log(greeter.greet('World')); // Outputs: Hello, World!

Service Lifetimes

netdi supports three service lifetimes:

Singleton

Singleton services are created once and shared by all consumers.

services.addSingleton(serviceIdentifier, Implementation);

Scoped

Scoped services are created once per scope. This is useful for services that should be shared within a request but not across requests.

services.addScoped(serviceIdentifier, Implementation);

// Create a scope
const scope = serviceProvider.createScope();
const scopedService = scope.serviceProvider.getService(serviceIdentifier);

// When done with the scope
scope.dispose();

Transient

Transient services are created each time they are requested.

services.addTransient(serviceIdentifier, Implementation);

Factory Registration

For complex service instantiation, you can use factory methods:

services.addSingleton(serviceIdentifier, provider => {
  // Use the provider to get dependencies
  const dependency = provider.getService(dependencyIdentifier);

  // Create and configure your service instance
  const instance = new MyService(dependency);
  instance.configure();

  return instance;
});

Creating Service Identifiers

Service identifiers help maintain type safety and prevent service conflicts:

// Create an identifier for a specific interface
const userServiceIdentifier = createServiceIdentifier<IUserService>();

// Then use it for registration and resolution
services.addSingleton(userServiceIdentifier, UserService);
const userService = serviceProvider.getService(userServiceIdentifier);

Decorators

@injectable()

Marks a class as injectable, allowing the container to create instances with dependencies:

@injectable()
class MyService {
  constructor() {}
}

@inject()

Specifies a dependency for a parameter:

class MyService {
  constructor(
    @inject(loggerIdentifier) private logger: ILogger,
    @inject(configIdentifier) private config: IConfig,
  ) {}
}

Advanced Topics

Service Disposal

Both service providers and scopes implement a dispose() method:

// Dispose the root provider
serviceProvider.dispose();

// Dispose a scope
const scope = serviceProvider.createScope();
// ... use scope
scope.dispose();

Type Safety

netdi is designed to be fully type-safe. The getService() method returns the exact type associated with the service identifier:

// TypeScript knows this is an IUserService
const userService = serviceProvider.getService(userServiceIdentifier);

License

MIT © Stevan Freeborn