Registry / web-framework / inversify-binding-decorators

inversify-binding-decorators

JSON →
library4.0.0jsnpmunverified

Inversify Binding Decorators is a utility library for InversifyJS, a powerful Inversion of Control (IoC) container for TypeScript and JavaScript applications. It simplifies the process of declaring dependency injection bindings by allowing developers to use ES2016 decorators directly on classes. This contrasts with InversifyJS's standard fluent API for binding. The current stable version is 4.0.0, which typically aligns with major versions of InversifyJS itself, indicating a release cadence tied to its core dependency. Its primary differentiator is the `@provide` decorator, enabling a more declarative and less verbose way to register components with the InversifyJS container, particularly useful in large applications with many services.

npm install inversify-binding-decorators
INSTALL
IMPORT
SIG · INVERSIFY-BINDING-
I
inversify-binding-decorators
web-frameworkjavascriptv4.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

provide
import { provide } from 'inversify-binding-decorators';
import provide from 'inversify-binding-decorators';
This is a named export for the primary decorator. Do not attempt a default import.
buildProviderModule
import { buildProviderModule } from 'inversify-binding-decorators';
const { buildProviderModule } = require('inversify-binding-decorators');
Use named import for the utility function that registers all decorated bindings. ESM is the standard for modern TypeScript projects.
injectable
import { injectable, Container } from 'inversify';
While not from this package, `injectable` from `inversify` is crucial for classes using DI and works alongside `@provide`.

Demonstrates how to use the `@provide` decorator to declare bindings and `buildProviderModule` to load them into an InversifyJS container.

import { injectable, Container } from "inversify"; import { provide, buildProviderModule } from "inversify-binding-decorators"; import "reflect-metadata"; // Must be imported once at the application entry point interface Weapon { hit(): string; } interface ThrowableWeapon { throw(): string; } @injectable() @provide(Katana) class Katana implements Weapon { public hit() { return "cut!"; } } @injectable() @provide(Shuriken) class Shuriken implements ThrowableWeapon { public throw() { return "hit!"; } } const container = new Container(); // Load all bindings declared via @provide decorators container.load(buildProviderModule()); // Resolve instances from the container const katana = container.get<Weapon>(Katana); const shuriken = container.get<ThrowableWeapon>(Shuriken); console.log(`Katana: ${katana.hit()}`); console.log(`Shuriken: ${shuriken.throw()}`);
Debug
Known issues
breakingMajor versions of `inversify-binding-decorators` (e.g., 2.x, 3.x, 4.x) are tightly coupled to corresponding major versions of `inversify`. Upgrading `inversify` may necessitate upgrading this library and vice-versa, potentially involving breaking changes in the core InversifyJS API.
fix
Always check the compatibility matrix between `inversify` and `inversify-binding-decorators` before upgrading either package. Review both packages' release notes for breaking changes.
affects: >=2.0.0
gotchaApplying the `@provide` decorator multiple times to a single class without explicit permission will throw an error to prevent accidental duplicate bindings.
fix
If multiple bindings for the same class are intended, pass `true` as the second argument to `@provide`, e.g., `@provide("Ninja", true)` for multiple identifiers.
affects: >=1.0.0
gotchaThe `reflect-metadata` polyfill must be imported exactly once at the entry point of your application to enable TypeScript's decorator metadata reflection, which is crucial for both InversifyJS and `inversify-binding-decorators` to function correctly.
fix
Ensure `import "reflect-metadata";` is present at the very top of your main application file (e.g., `main.ts`, `app.ts`).
affects: >=1.0.0
Errors
Common errors & fixes
Cannot apply @injectable decorator multiple times. Please use @provide(ID, true) if you are trying to declare multiple bindings!
Attempted to apply the `@provide` decorator more than once to a single class without explicitly allowing multiple bindings.
fix
If you intend to provide the class under multiple identifiers, use `@provide(identifier, true)` for each additional binding. For example, `@provide("Logger", true)` and `@provide("ConsoleLogger", true)`.
Error: Missing required @injectable annotation in: ClassName
A class intended for dependency injection via `@provide` was not also decorated with `@injectable` from `inversify`.
fix
Ensure that any class decorated with `@provide` also has the `@injectable()` decorator from `inversify` applied to it, e.g., `@injectable()
@provide(MyService)
class MyService { ... }`.
Upgrade
Version history
4.0.0latest on npm
Audit
Dependencies
inversifyrequiredCore dependency injection container that this library extends.
reflect-metadatarequiredRequired for TypeScript decorator metadata reflection, a fundamental aspect of both InversifyJS and this library.
Agent activity
4 hits · last 30 days
node
4
Resources
inversify-binding-decorators — npm install inversify-binding-decorators · libregistry