{"id":17102,"library":"inversify-logger-middleware","title":"InversifyJS Logger Middleware","description":"inversify-logger-middleware is a specialized middleware designed to integrate with InversifyJS, providing a console-based logger for visualizing the dependency injection resolution plan. It intercepts the binding and resolution process within an InversifyJS container, displaying a detailed tree-like structure of service identifiers, bindings, implementation types, scopes, and target metadata. This tool is invaluable for debugging complex dependency graphs and understanding how InversifyJS resolves dependencies. The current stable version is 3.1.0, which is compatible with `inversify@3.x.x`. Its release cadence is tightly coupled with major InversifyJS releases to ensure compatibility. Key differentiators include its deep integration with InversifyJS internals and its clear, visual representation of the container's resolution logic, which helps developers quickly identify misconfigurations or unexpected dependency flows.","status":"active","version":"3.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/inversify/inversify-logger-middleware","tags":["javascript","inversifyjs","logger","inversion","of","control","dependency","typescript"],"install":[{"cmd":"npm install inversify-logger-middleware","lang":"bash","label":"npm"},{"cmd":"yarn add inversify-logger-middleware","lang":"bash","label":"yarn"},{"cmd":"pnpm add inversify-logger-middleware","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core dependency injection framework.","package":"inversify","optional":false},{"reason":"Required by InversifyJS for decorator metadata reflection.","package":"reflect-metadata","optional":false}],"imports":[{"note":"Primary factory function to create the middleware. InversifyJS and its ecosystem are primarily TypeScript/ESM-first, though CommonJS usage is possible with appropriate transpilation.","wrong":"const makeLoggerMiddleware = require('inversify-logger-middleware');","symbol":"makeLoggerMiddleware","correct":"import { makeLoggerMiddleware } from 'inversify-logger-middleware';"},{"note":"Container and related types (like ContainerModule) are part of the core 'inversify' package, not the logger middleware itself.","wrong":"import { Container } from 'inversify-logger-middleware';","symbol":"Container","correct":"import { Container, ContainerModule } from 'inversify';"},{"note":"Binding interfaces are exported from the core 'inversify' package.","wrong":"import { Bind } from 'inversify-logger-middleware';","symbol":"interfaces.Bind","correct":"import { interfaces } from 'inversify';"}],"quickstart":{"code":"import \"reflect-metadata\";\nimport { Container, ContainerModule, interfaces } from 'inversify';\nimport { makeLoggerMiddleware } from 'inversify-logger-middleware';\n\ninterface Weapon { attack(): string; }\ninterface Warrior { fight(): string; sneak(): string; }\n\nclass Katana implements Weapon {\n  public attack() { return \"cut!\"; }\n}\n\nclass Shuriken implements Weapon {\n  public attack() { return \"throw!\"; }\n}\n\nclass Samurai implements Warrior {\n  private _weapon: Weapon;\n  constructor(@inversify.inject(\"Weapon\") weapon: Weapon) { this._weapon = weapon; }\n  public fight() { return this._weapon.attack(); }\n  public sneak() { return \"\"; }\n}\n\nclass Ninja implements Warrior {\n  private _weapon: Weapon;\n  constructor(@inversify.inject(\"Weapon\") weapon: Weapon) { this._weapon = weapon; }\n  public fight() { return this._weapon.attack(); }\n  public sneak() { return \"sneak!\"; }\n}\n\nconst container = new Container();\nconst logger = makeLoggerMiddleware();\ncontainer.applyMiddleware(logger);\n\nconst warriorModule = new ContainerModule((bind: interfaces.Bind) => {\n    bind<Weapon>(\"Weapon\").to(Katana).whenInjectedInto(Samurai);\n    bind<Weapon>(\"Weapon\").to(Shuriken).whenInjectedInto(Ninja);\n    bind<Warrior>(\"Warrior\").to(Samurai).whenTargetTagged(\"canSneak\", false);\n    bind<Warrior>(\"Warrior\").to(Ninja).whenTargetTagged(\"canSneak\", true);\n});\n\ncontainer.load(warriorModule);\n\n// This will now log the resolution plan to the console\nconst ninja = container.getTagged<Warrior>(\"Warrior\", \"canSneak\", true);\nconsole.log(ninja.fight());\nconsole.log(ninja.sneak());\n\nconst samurai = container.getTagged<Warrior>(\"Warrior\", \"canSneak\", false);\nconsole.log(samurai.fight());","lang":"typescript","description":"This quickstart demonstrates how to set up an InversifyJS container with conditional bindings, apply the logger middleware, and resolve dependencies, showing the detailed resolution plan in the console."},"warnings":[{"fix":"Update 'inversify' to a compatible 3.x.x version. Consult the InversifyJS v3 migration guide if upgrading from an older major version.","message":"Version 3.0.0 introduced breaking changes to align with InversifyJS v3.0.0. Ensure your core 'inversify' package is also updated to v3.x.x for compatibility.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Ensure your 'inversify' dependency matches the major version required by inversify-logger-middleware. For v2.x.x of the logger, use inversify@2.x.x.","message":"Version 2.0.0 of inversify-logger-middleware is compatible with inversify@2.0.0. Major version bumps typically indicate breaking changes aligning with the core InversifyJS library's API changes.","severity":"breaking","affected_versions":">=2.0.0 <3.0.0"},{"fix":"Upgrade your project's TypeScript version to 2.0 or newer. Ensure 'skipLibCheck' is not masking issues if you are encountering unexpected type problems.","message":"This package requires TypeScript 2.0 or higher for its type definitions. Using an older TypeScript version may result in compilation errors related to missing types or incorrect type inference.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Review your `ContainerModule` or direct `bind` calls to ensure that all requested service identifiers have at least one valid binding. Check for typos in service identifiers or incorrect `when` clauses.","cause":"The logger middleware cannot resolve bindings that are not correctly configured in the InversifyJS container.","error":"Error: No matching bindings found for serviceIdentifier: Symbol(Weapon)"},{"fix":"Update your `inversify` package to a version that supports middleware (e.g., 2.0.0 or higher). Ensure that the `container` variable is indeed a `new Container()` instance.","cause":"You might be using an older version of InversifyJS that does not support the `applyMiddleware` method, or `container` is not an instance of `Inversify.Container`.","error":"TypeError: container.applyMiddleware is not a function"}],"ecosystem":"npm","meta_description":null}