Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsJul 26, 2026·2 min de lecture

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.

Prêt pour agents

Installation agent prête

Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
InversifyJS Overview
Commande d'installation directe
npx -y tokrepo@latest install 0f316dc2-8931-11f1-9bc6-00163e2b0d79 --target codex

À exécuter après confirmation du plan en dry-run.

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

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires