Scripts2026年7月26日·1 分钟阅读

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.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
InversifyJS Overview
直接安装命令
npx -y tokrepo@latest install 0f316dc2-8931-11f1-9bc6-00163e2b0d79 --target codex

先 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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产