# InversifyJS — Inversion of Control Container for TypeScript and JavaScript > A powerful and lightweight IoC container for TypeScript and JavaScript applications that brings dependency injection patterns from enterprise languages to the Node.js ecosystem. ## Install Save as a script file and run: # InversifyJS — Inversion of Control Container for TypeScript and JavaScript ## Quick Use ```bash npm install inversify reflect-metadata ``` ```typescript import { Container, injectable, inject } from 'inversify'; @injectable() class Ninja { constructor(@inject('Weapon') weapon: Weapon) {} } const container = new Container(); container.bind('Weapon').to(Katana); ``` ## Introduction InversifyJS is a dependency injection container for TypeScript and JavaScript that implements the Inversion of Control (IoC) pattern. It brings the structured dependency management found in Java (Spring, Guice) and .NET (Autofac) to the TypeScript ecosystem, helping developers build loosely coupled, testable applications. ## What InversifyJS Does - Manages object creation and dependency resolution via a central container - Uses TypeScript decorators (@injectable, @inject) for declaring dependencies - Supports constructor, property, and method injection patterns - Provides middleware and interceptor hooks for cross-cutting concerns - Enables hierarchical containers for modular application architecture ## Architecture Overview InversifyJS uses a Container object that stores bindings between service identifiers (symbols or strings) and their implementations. When a dependency is requested, the container resolves the full object graph recursively, creating instances and injecting their dependencies. It relies on TypeScript's reflect-metadata to read type information from decorators at runtime. ## Self-Hosting & Configuration - Install `inversify` and `reflect-metadata` via npm - Enable `experimentalDecorators` and `emitDecoratorMetadata` in tsconfig.json - Import `reflect-metadata` once at the application entry point - Define services with `@injectable()` and mark dependencies with `@inject()` - Create a `Container` instance and register bindings in a central module ## Key Features - Full TypeScript support with type-safe bindings and resolution - Scope management: transient, singleton, and request scopes - Named and tagged bindings for resolving multiple implementations of the same interface - Activation and deactivation handlers for lifecycle management - Middleware support for logging, caching, and other cross-cutting concerns ## Comparison with Similar Tools - **tsyringe (Microsoft)** — simpler API with fewer features; InversifyJS offers richer scoping and middleware - **TypeDI** — similar decorator-based approach but less actively maintained - **Google Guice** — Java-only; InversifyJS brings the same patterns to TypeScript - **NestJS built-in DI** — tightly coupled to the NestJS framework; InversifyJS is framework-agnostic ## FAQ **Q: Does InversifyJS work with plain JavaScript (no TypeScript)?** A: Yes, but without decorators you need to use the fluent API for declaring dependencies, which is more verbose. **Q: Is InversifyJS suitable for frontend applications?** A: Yes. It works in both Node.js and browser environments and is commonly used with React and Angular. **Q: How does InversifyJS handle circular dependencies?** A: It detects circular dependencies at resolution time and throws a clear error. Use lazy injection (`@lazyInject`) to break cycles when needed. **Q: What is the performance overhead?** A: Minimal. The container resolves dependencies in microseconds, and singleton bindings are cached after first resolution. ## Sources - https://github.com/inversify/InversifyJS - https://inversify.io/ --- Source: https://tokrepo.com/en/workflows/asset-0f316dc2 Author: Script Depot