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

@duude92/lazyinject

v0.2.14

Published

A lightweight TypeScript dependency injection library with lazy loading capabilities.

Readme

LazyInject

A lightweight TypeScript dependency injection library with lazy loading capabilities.

Overview

LazyInject provides a clean and efficient way to manage dependencies in TypeScript applications through decorators and a container-based system. The library's key feature is lazy initialization, which defers object instantiation until dependencies are actually needed, improving performance and memory usage.

Features

🚀 Lazy Loading: Dependencies are instantiated only when accessed 🎯 Decorator-Based: Simple @Export and @Import decorators for dependency management 📦 Container System: Centralized dependency registration and resolution 🔍 Auto-Discovery: Automatic scanning of directories for dependencies 🎭 Interface-Based Injection: Inject based on interfaces rather than concrete implementations 📚 Multiple Implementations: Support for injecting arrays of implementations 💪 Type Safety: Full TypeScript support with generic types

Installation

npm install lazyinject

Quick Start

1. Define an Interface

// interface/ILogger.ts
export interface ILogger {
 log(message: string): void;
}

2. Create an Implementation

// implementations/ConsoleLogger.ts
import {Export} from 'lazyinject';
import {ILogger} from '../interface/ILogger';

@Export('ILogger')
export class ConsoleLogger implements ILogger {
 log(message: string): void {
     console.log(`[LOG]: ${message}`);
 }
}

3. Inject Dependencies

// MyService.ts
import {Import} from 'lazyinject';
import {ILogger} from './interface/ILogger';

@Export(MyService)
export class MyService {
 constructor(
     @Import('ILogger')
     private readonly logger: ILogger
 ) {}

 doSomething(): void {
     this.logger.log('Service is working!');
 }
}

4. Bootstrap Your Application

// index.ts
import {ContainerFactory} from 'lazyinject';
import { MyService } from './MyService';

const bootstrap = async () => {
 const container = await ContainerFactory.create({
     baseDir: __dirname,
     catalogs: ['.', './implementations']
 });

 const service = container.get<MyService>(MyService);
 service.doSomething();
};

bootstrap();

Advanced Usage

Lazy Loading

Use the Lazy wrapper for deferred instantiation:

import {ImportMany, Lazy} from 'lazyinject';
import {IPlugin} from './interface/IPlugin';

export class PluginManager {
    constructor(
        @ImportMany('IPlugin', {lazy: true})
        private readonly plugins: Lazy<IPlugin>[]
    ) {}

    executeRandomPlugin(): void {
        const randomIndex = Math.floor(Math.random() * this.plugins.length);
        const plugin = this.plugins[randomIndex];

        // Plugin is instantiated only when .Value is accessed
        plugin.Value.execute();
    }

    getPluginInfo(): void {
        this.plugins.forEach((plugin, index) => {
            console.log(`Plugin ${index}: Loaded = ${plugin.HasValue}`);
        });
    }
}

Multiple Implementations

Inject all implementations of an interface:

import {ImportMany} from 'lazyinject';
import {IValidator} from './interface/IValidator';

export class ValidationService {
    constructor(
        @ImportMany('IValidator')
        private readonly validators: IValidator[]
    ) {}

    validateAll(data: any): boolean {
        return this.validators.every(validator => validator.validate(data));
    }
}

Container Configuration

The ContainerFactory supports various configuration options:

const container = await ContainerFactory.create({
    baseDir: __dirname,           // Base directory for scanning
    catalogs: [                   // Directories to scan for dependencies
        '.',
        './services',
        './implementations',
        './plugins'
    ],
    recursive: true                // To recursively scan directories for dependencies
});

API Reference

Types

  • InterfaceType: Supported identifier types for registration (string | symbol | ConstructorType)
  • ConstructorType: Type of class constructor, used to register class
  • ExportedType: Type of object to register (ConstructorType | object)

Decorators

  • @Export(identifier: InterfaceType): Registers a class as an implementation of the specified identifier
  • @Import(identifier: InterfaceType): Injects a single implementation
  • @ImportMany(identifier: InterfaceType, options?): Injects all implementations as an array
    • options.lazy: boolean - Enable lazy loading (default: false)

Core Classes

  • ContainerFactory: Factory for creating dependency injection containers
  • Lazy<T>: Wrapper for lazy-loaded dependencies
    • .Value: T - Gets the instantiated value
    • .HasValue: boolean - Checks if the value has been instantiated
  • ContainerRegistry: Internal registry for managing dependencies

Examples

The repository includes several examples in the samples/ directory:

Building

npm run build

This will compile TypeScript files and generate type definitions in the dist/ directory.

License

This project is open source and available under the MIT License.

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.