node-dependency-injection-express-middleware

raw JSON →
1.2.2 verified Sat Apr 25 auth: no javascript

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.

error TypeError: NodeInjectionMiddleware is not a constructor
cause Forgetting 'new' keyword when instantiating the middleware class.
fix
Use new NodeInjectionMiddleware(options) instead of NodeInjectionMiddleware(options).
error Error: Cannot find module 'js-yaml'
cause Missing dependency js-yaml which is required to parse YAML service files.
fix
Run npm install js-yaml or ensure it is in your package.json.
error TypeError: Cannot read properties of undefined (reading 'container')
cause Route handler tries to access req.container before the middleware has been applied.
fix
Ensure app.use(middleware) is placed before the route definitions.
gotcha The middleware requires 'new' keyword when constructing NodeInjectionMiddleware; omitting it will throw an error.
fix Always call `new NodeInjectionMiddleware(options).middleware()`.
gotcha 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.
fix Ensure the YAML file exists and is correctly formatted.
gotcha 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.
fix Call app.use(middleware) before defining routes that use req.container.
npm install node-dependency-injection-express-middleware
yarn add node-dependency-injection-express-middleware
pnpm add node-dependency-injection-express-middleware

Shows minimal Express app using dependency injection middleware with YAML configuration.

import express from 'express';
import NodeInjectionMiddleware from 'node-dependency-injection-express-middleware';

const app = express();

const options = {
  serviceFilePath: 'services.yml',
};

const middleware = new NodeInjectionMiddleware(options).middleware();
app.use(middleware);

app.get('/service', (req, res) => {
  const container = req.container;
  const myService = container.get('my.service');
  res.send(myService.doSomething());
});

app.listen(3000);