{"id":11094,"library":"inversify-binding-decorators","title":"Inversify Binding Decorators","description":"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.","status":"active","version":"4.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/inversify/inversify-binding-decorators","tags":["javascript","InversifyJS","typescript"],"install":[{"cmd":"npm install inversify-binding-decorators","lang":"bash","label":"npm"},{"cmd":"yarn add inversify-binding-decorators","lang":"bash","label":"yarn"},{"cmd":"pnpm add inversify-binding-decorators","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core dependency injection container that this library extends.","package":"inversify","optional":false},{"reason":"Required for TypeScript decorator metadata reflection, a fundamental aspect of both InversifyJS and this library.","package":"reflect-metadata","optional":false}],"imports":[{"note":"This is a named export for the primary decorator. Do not attempt a default import.","wrong":"import provide from 'inversify-binding-decorators';","symbol":"provide","correct":"import { provide } from 'inversify-binding-decorators';"},{"note":"Use named import for the utility function that registers all decorated bindings. ESM is the standard for modern TypeScript projects.","wrong":"const { buildProviderModule } = require('inversify-binding-decorators');","symbol":"buildProviderModule","correct":"import { buildProviderModule } from 'inversify-binding-decorators';"},{"note":"While not from this package, `injectable` from `inversify` is crucial for classes using DI and works alongside `@provide`.","symbol":"injectable","correct":"import { injectable, Container } from 'inversify';"}],"quickstart":{"code":"import { injectable, Container } from \"inversify\";\nimport { provide, buildProviderModule } from \"inversify-binding-decorators\";\nimport \"reflect-metadata\"; // Must be imported once at the application entry point\n\ninterface Weapon { hit(): string; }\ninterface ThrowableWeapon { throw(): string; }\n\n@injectable()\n@provide(Katana)\nclass Katana implements Weapon {\n    public hit() {\n        return \"cut!\";\n    }\n}\n\n@injectable()\n@provide(Shuriken)\nclass Shuriken implements ThrowableWeapon {\n    public throw() {\n        return \"hit!\";\n    }\n}\n\nconst container = new Container();\n// Load all bindings declared via @provide decorators\ncontainer.load(buildProviderModule());\n\n// Resolve instances from the container\nconst katana = container.get<Weapon>(Katana);\nconst shuriken = container.get<ThrowableWeapon>(Shuriken);\n\nconsole.log(`Katana: ${katana.hit()}`);\nconsole.log(`Shuriken: ${shuriken.throw()}`);","lang":"typescript","description":"Demonstrates how to use the `@provide` decorator to declare bindings and `buildProviderModule` to load them into an InversifyJS container."},"warnings":[{"fix":"Always check the compatibility matrix between `inversify` and `inversify-binding-decorators` before upgrading either package. Review both packages' release notes for breaking changes.","message":"Major 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.","severity":"breaking","affected_versions":">=2.0.0"},{"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.","message":"Applying the `@provide` decorator multiple times to a single class without explicit permission will throw an error to prevent accidental duplicate bindings.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure `import \"reflect-metadata\";` is present at the very top of your main application file (e.g., `main.ts`, `app.ts`).","message":"The `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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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)`.","cause":"Attempted to apply the `@provide` decorator more than once to a single class without explicitly allowing multiple bindings.","error":"Cannot apply @injectable decorator multiple times. Please use @provide(ID, true) if you are trying to declare multiple bindings!"},{"fix":"Ensure that any class decorated with `@provide` also has the `@injectable()` decorator from `inversify` applied to it, e.g., `@injectable()\n@provide(MyService)\nclass MyService { ... }`.","cause":"A class intended for dependency injection via `@provide` was not also decorated with `@injectable` from `inversify`.","error":"Error: Missing required @injectable annotation in: ClassName"}],"ecosystem":"npm"}