{"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,"search_vec":"'1.2.2':23 'approach':62 'cadenc':72 'class':27 'compil':44,45 'complex':68 'configur':33 'consol':87 'contain':21,38,50,86 'custom':47 'depend':3,8,19,83 'differenti':56 'express':5,12,60,80 'file':34 'framework':69 'inject':4,9,20,36,84 'integr':14,61,70 'javascript':79 'key':55 'last':75 'logger':48 'low':74 'middlewar':6,10,26,64,81 'node':2,18,82 'node-dependency-inject':17 'node-dependency-injection-express-middlewar':1 'object':42 'pars':29 'pass':46 'provid':24 'rather':65 'registr':51 'releas':71,76 'request':41 'requir':67 'servic':32,54,85 'simpl':59 'support':43 'v1.2.2':78 'version':22 'via':63 'yaml':31","created_at":"2026-04-25T07:39:10.796224+00:00","updated_at":"2026-04-25T07:39:10.796224+00:00","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,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/zazoomauro/node-dependency-injection-express-middleware","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/node-dependency-injection-express-middleware","openapi_spec":null,"status_page":null,"smithery":null,"categories":["web-framework"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-24","install_tag":null}}