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

@stackra/container

v2.0.0

Published

Client-side dependency injection container for browser and React — decorators, modules, lifecycle hooks, and discovery.

Readme

@stackra/container

Client-side dependency-injection container for the Stackra framework — Nest-style decorators (@Injectable, @Module, @Inject, @Optional) with async factories, dynamic modules, lifecycle hooks, discovery, and React bindings. Zero server runtime.

Install

pnpm add @stackra/container @stackra/contracts reflect-metadata

Import reflect-metadata once at the top of your entry file:

import "reflect-metadata";

Quick start

// user.service.ts
import { Injectable } from "@stackra/container";

@Injectable()
export class UserService {
  greet(name: string) {
    return `Hello ${name}`;
  }
}

// app.module.ts
import { Module } from "@stackra/container";
import { UserService } from "./user.service";

@Module({
  providers: [UserService],
  exports: [UserService],
})
export class AppModule {}

// main.ts
import "reflect-metadata";
import { ApplicationFactory } from "@stackra/container";
import { AppModule } from "./app.module";

const app = await ApplicationFactory.create(AppModule);
const user = app.get(UserService);
console.log(user.greet("World"));

Public API

Decorators

| Decorator | Purpose | | ------------------------------------------ | ------------------------------------------------------------------- | | @Injectable() | Marks a class as resolvable by the container. | | @Injectable({ scope }) | Sets scope: DEFAULT (singleton), TRANSIENT, or REQUEST. | | @Module({ imports, providers, exports }) | Declares a DI module. | | @Global() | Marks a module as globally visible (skip re-importing). | | @Inject(token) | Overrides the parameter type for injection. | | @Optional() | Marks a parameter as optional (resolves to undefined if missing). | | @InjectProperty(token) | Property injection. |

Providers (declarative)

Every provider shape supported by NestJS is here:

@Module({
  providers: [
    // ClassProvider
    UserService,
    { provide: UserService, useClass: UserService },

    // ValueProvider
    { provide: "CONFIG", useValue: { debug: true } },

    // FactoryProvider (sync or async)
    {
      provide: "DATABASE",
      useFactory: async (config) => await connect(config),
      inject: ["CONFIG"],
    },

    // ExistingProvider (alias)
    { provide: "ALIAS", useExisting: UserService },
  ],
})
export class AppModule {}

ApplicationFactory

import { ApplicationFactory } from "@stackra/container";

// Minimal
const app = await ApplicationFactory.create(AppModule);

// With options
const app = await ApplicationFactory.create(AppModule, {
  debug: true, // exposes window[globalName] for browser DevTools
  globalName: "__APP__",
  shutdownHooks: true, // registers beforeunload/SIGTERM handlers
});

// Resolve providers
const svc = app.get(UserService);
const cfg = app.get<AppConfig>("CONFIG");

// Close manually
await app.close();

Lifecycle hooks

Implement any of the hook interfaces from @stackra/contracts:

import { Injectable } from "@stackra/container";
import type { OnModuleInit, OnModuleDestroy } from "@stackra/contracts";

@Injectable()
export class SyncService implements OnModuleInit, OnModuleDestroy {
  async onModuleInit() {
    /* connect */
  }
  async onModuleDestroy() {
    /* disconnect */
  }
}

Available hooks: OnModuleInit, OnModuleDestroy, OnApplicationBootstrap, OnApplicationShutdown.

Dynamic modules

@Module({})
export class MyModule {
  static forRoot(config: MyConfig): DynamicModule {
    return {
      module: MyModule,
      providers: [{ provide: "MY_CONFIG", useValue: config }],
      exports: ["MY_CONFIG"],
    };
  }

  static forRootAsync(options: IAsyncModuleOptions<MyConfig>): DynamicModule {
    return {
      module: MyModule,
      providers: [
        {
          provide: "MY_CONFIG",
          useFactory: options.useFactory,
          inject: options.inject ?? [],
        },
      ],
      exports: ["MY_CONFIG"],
    };
  }
}

Discovery

Any provider that imports DiscoveryModule (bundled) can scan all providers in the graph for decorators and metadata:

import { DISCOVERY_SERVICE } from "@stackra/contracts";
import type { IDiscoveryService } from "@stackra/contracts";

@Injectable()
class MyLoader {
  constructor(
    @Inject(DISCOVERY_SERVICE) private readonly discovery: IDiscoveryService,
  ) {}

  onModuleInit() {
    const providers = this.discovery.getProviders();
    for (const wrapper of providers) {
      const metadata = Reflect.getMetadata(
        "my:key",
        wrapper.instance.constructor,
      );
      if (metadata) {
        /* … */
      }
    }
  }
}

React bindings — @stackra/container/react

import { ApplicationFactory } from "@stackra/container";
import {
  ContainerProvider,
  useInject,
  useOptionalInject,
} from "@stackra/container/react";

const app = await ApplicationFactory.create(AppModule);

ReactDOM.createRoot(root).render(
  <ContainerProvider>
    <App />
  </ContainerProvider>,
);

function UserProfile() {
  const users = useInject(UserService);
  const cache = useOptionalInject(CACHE_MANAGER); // returns undefined if not registered
  return <div>{users.greet("World")}</div>;
}

Hooks available: useInject, useOptionalInject, useContainer, useDiscovery.

Configuration

Copy the container config template into your app:

cp node_modules/@stackra/container/config/container.config.ts src/config/container.config.ts

Then pass it to the factory:

import { containerConfig } from "@/config/container.config";
await ApplicationFactory.create(AppModule, containerConfig);

Testing helper — @stackra/container/testing

Assertable IApplication-compatible mock for services that inject APPLICATION or otherwise depend on a container:

import { createMockApplication } from "@stackra/container/testing";
import { LOGGER_MANAGER } from "@stackra/contracts";
import { createMockLoggerManager } from "@stackra/logger/testing";

// Pre-populate the container with the tokens your service under test needs
const logger = createMockLoggerManager();
const app = createMockApplication([[LOGGER_MANAGER, logger]]);

service.setup(app);
app.$.assertCalled("get").with(LOGGER_MANAGER).once();

// Or use the classic API
expect(app.has(LOGGER_MANAGER)).toBe(true);
expect(app.get(LOGGER_MANAGER)).toBe(logger);

Implements the full public IApplication surface (get, getOptional, has, resolve, provide, close) — no real DI graph, no module scanning, just a Map<InjectionToken, unknown> you can seed and interrogate.

Subpaths

| Import | Purpose | | ---------------------------- | ------------------------------------------------------------- | | @stackra/container | Injectable, Module, Inject, ApplicationFactory, hooks | | @stackra/container/react | ContainerProvider, useInject, useOptionalInject | | @stackra/container/testing | createMockApplication(), MockApplication |

License

MIT