{"id":18605,"library":"node-dependency-injection-express-middleware","title":"node-dependency-injection-express-middleware","description":"A dependency injection middleware for Express that integrates with the node-dependency-injection container. Version 1.2.2 provides a middleware class that parses a YAML service configuration file and injects the container into the request object. Supports compilation, compiler passes, custom loggers, and container registration as a service. Key differentiator is its simple Express integration approach via middleware rather than requiring complex framework integration. Release cadence is low; last release was v1.2.2.","status":"active","version":"1.2.2","language":"javascript","source_language":"en","source_url":"https://github.com/zazoomauro/node-dependency-injection-express-middleware","tags":["javascript","express","middleware","node","dependency","injection","service","container","console"],"install":[{"cmd":"npm install node-dependency-injection-express-middleware","lang":"bash","label":"npm"},{"cmd":"yarn add node-dependency-injection-express-middleware","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-dependency-injection-express-middleware","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"core container library used to manage services","package":"node-dependency-injection","optional":false},{"reason":"parsing YAML service configuration files","package":"js-yaml","optional":false},{"reason":"peer dependency for the middleware to work with Express apps","package":"express","optional":false}],"imports":[{"note":"Default import. CommonJS require works but TypeScript may need esModuleInterop.","wrong":"const NodeInjectionMiddleware = require('node-dependency-injection-express-middleware')","symbol":"NodeInjectionMiddleware","correct":"import NodeInjectionMiddleware from 'node-dependency-injection-express-middleware'"},{"note":"Named export also available. Default and named export are equivalent.","wrong":"import NodeInjectionMiddleware from 'node-dependency-injection-express-middleware'","symbol":"NodeInjectionMiddleware (named)","correct":"import { NodeInjectionMiddleware } from 'node-dependency-injection-express-middleware'"},{"note":"Must instantiate with 'new' before calling .middleware()","wrong":"NodeInjectionMiddleware(options).middleware()","symbol":"middleware() function","correct":"const middleware = new NodeInjectionMiddleware(options).middleware()"}],"quickstart":{"code":"import express from 'express';\nimport NodeInjectionMiddleware from 'node-dependency-injection-express-middleware';\n\nconst app = express();\n\nconst options = {\n  serviceFilePath: 'services.yml',\n};\n\nconst middleware = new NodeInjectionMiddleware(options).middleware();\napp.use(middleware);\n\napp.get('/service', (req, res) => {\n  const container = req.container;\n  const myService = container.get('my.service');\n  res.send(myService.doSomething());\n});\n\napp.listen(3000);","lang":"javascript","description":"Shows minimal Express app using dependency injection middleware with YAML configuration."},"warnings":[{"fix":"Always call `new NodeInjectionMiddleware(options).middleware()`.","message":"The middleware requires 'new' keyword when constructing NodeInjectionMiddleware; omitting it will throw an error.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure the YAML file exists and is correctly formatted.","message":"The options.serviceFilePath must be a valid path to a YAML file. If the file is missing or invalid YAML, the middleware will throw at runtime.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Call app.use(middleware) before defining routes that use req.container.","message":"The container is attached to req.container only after the middleware runs. Make sure your routes are defined after app.use() or use a router-level middleware.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use `new NodeInjectionMiddleware(options)` instead of `NodeInjectionMiddleware(options)`.","cause":"Forgetting 'new' keyword when instantiating the middleware class.","error":"TypeError: NodeInjectionMiddleware is not a constructor"},{"fix":"Run `npm install js-yaml` or ensure it is in your package.json.","cause":"Missing dependency js-yaml which is required to parse YAML service files.","error":"Error: Cannot find module 'js-yaml'"},{"fix":"Ensure `app.use(middleware)` is placed before the route definitions.","cause":"Route handler tries to access req.container before the middleware has been applied.","error":"TypeError: Cannot read properties of undefined (reading 'container')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}