{"id":17582,"library":"dynamic-middleware","title":"Dynamic Middleware","description":"dynamic-middleware is a utility library for Connect and Express applications that enables runtime replacement or disabling of middleware. It addresses the common difficulty of altering middleware after an HTTP server has started, a feature not natively supported by Connect or Express. The current stable version is 4.0.4. The library's release cadence is not specified but it has undergone significant architectural changes between major versions. It differentiates itself by providing a `create` method to wrap existing middleware, allowing for dynamic control over its execution via `enable()`, `disable()`, and `replace()` methods, specifically for `app.use()` and `app.get()` style middleware, which was improved in `4.x.x` to handle routes more effectively.","status":"active","version":"4.0.4","language":"javascript","source_language":"en","source_url":"https://github.com/kessler/node-dynamic-middleware","tags":["javascript","connect","express","middleware"],"install":[{"cmd":"npm install dynamic-middleware","lang":"bash","label":"npm"},{"cmd":"yarn add dynamic-middleware","lang":"bash","label":"yarn"},{"cmd":"pnpm add dynamic-middleware","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Primarily designed for CommonJS. Direct ESM `import` might not work as expected without a transpiler or specific Node.js configuration for CJS interoperability. The module exports an object containing the `create` method.","wrong":"import DynamicMiddleware from 'dynamic-middleware'","symbol":"DynamicMiddleware","correct":"const DynamicMiddleware = require('dynamic-middleware')"},{"note":"After creating a dynamic middleware instance, its `.handler()` method should be used within Express/Connect route or `use` calls to integrate it into the middleware chain.","wrong":"app.get('/', dm)","symbol":"dm.handler()","correct":"app.get('/', dm.handler())"},{"note":"For error-handling middleware, use the `.errorHandler()` method. This ensures it's correctly registered as a 4-argument error handler in Connect/Express.","wrong":"app.use(errorDm)","symbol":"dm.errorHandler()","correct":"app.use(errorDm.errorHandler())"}],"quickstart":{"code":"const DynamicMiddleware = require('dynamic-middleware')\nconst express = require('express')\n\nconst app = express()\n\n// a simple middleware\nfunction myMiddleware(req, res, next) {\n    res.end('1')\n}\n\n// create a dynamic one from it\nlet dm = DynamicMiddleware.create(myMiddleware)\n\napp.get('/', dm.handler()) \n\n// disable the middleware, will reply with 404 now\n// dm.disable()\n\n// enable it back\n// dm.enable()\n\n// or replace it with something else\ndm = dm.replace(function(req, res, next) {\n    res.end('2')\n})\n\n// create a dynamic error middlware\nlet errorDm = DynamicMiddleware.create((err, req, res, next) => {\n  console.error(err);\n  res.status(500).send('Error: ' + err.message);\n})\n\napp.use(errorDm.errorHandler())\n\napp.listen(3000, () => {\n  console.log('Server running on port 3000');\n});\n","lang":"javascript","description":"This example demonstrates how to create, replace, and integrate dynamic middleware into an Express application, including a dynamic error handler."},"warnings":[{"fix":"Upgrade to `dynamic-middleware` version `4.x.x` or later. This will likely require adjusting your code to the new public API.","message":"From `3.x.x` to `4.x.x`, the internal mechanism for managing middleware changed significantly. Older versions directly manipulated Express/Connect's internal state, which caused issues with route-specific middleware (`.get()`, `.post()`). Version `4.x.x` manages state internally, providing more robust handling for all middleware types.","severity":"breaking","affected_versions":">=3.0.0 <4.0.0"},{"fix":"Review the `4.x.x` usage examples and documentation to refactor existing code. Key methods like `create` and the use of `.handler()` or `.errorHandler()` are central to the new API.","message":"The public interface of `dynamic-middleware` changed completely in `4.x.x`. Code written for `3.x.x` and earlier will require updates to adapt to the new API.","severity":"breaking","affected_versions":">=3.0.0 <4.0.0"},{"fix":"For Node.js environments, use the CommonJS `require()` syntax: `const DynamicMiddleware = require('dynamic-middleware')`. If ESM is strictly required, investigate tools like `rollup` or `webpack` for bundling, or Node.js's CJS-ESM interoperability features if supported by your Node.js version.","message":"The provided examples and package structure strongly suggest `dynamic-middleware` is primarily a CommonJS module. Using ES module `import` syntax might lead to compatibility issues or require specific Node.js configuration for CJS interop, especially for older Node versions or without a transpilation step.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Test thoroughly if using with Express versions newer than 4. If issues arise, consider pinning to an older Express version or exploring alternative solutions for dynamic middleware management in newer Express versions.","message":"The library explicitly states compatibility with `express 3`, `express 4`, and `connect 3`. While it might work with newer versions of Express (e.g., Express 5), official compatibility is not guaranteed beyond these stated versions. Newer Express versions might introduce breaking changes that affect `dynamic-middleware`'s internal mechanisms.","severity":"gotcha","affected_versions":"<=4.0.4"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Use CommonJS `const DynamicMiddleware = require('dynamic-middleware')` to import the module correctly.","cause":"Attempting to use ESM `import DynamicMiddleware from 'dynamic-middleware'` when the module is CommonJS and exports an object directly (not a default export).","error":"TypeError: dynamic_middleware_1.default is not a function"},{"fix":"Ensure the module is imported with `const DynamicMiddleware = require('dynamic-middleware')` and that `DynamicMiddleware` is not `null` or `undefined` before calling `.create()`.","cause":"This typically occurs when trying to access `create` on an undefined or incorrectly imported `DynamicMiddleware` object, often due to incorrect CommonJS vs. ESM import syntax.","error":"TypeError: Cannot read properties of undefined (reading 'create')"},{"fix":"Upgrade to `dynamic-middleware` version `4.x.x` or later, which has an improved internal state management system specifically designed to work correctly with route-specific middleware. Remember to update your code to the new API if upgrading from a 3.x.x version.","cause":"This behavior was characteristic of `dynamic-middleware` versions `3.x.x` and earlier due to how they manipulated Express/Connect's internal state, which was less effective for route-specific handlers.","error":"Middleware not affecting routes (e.g., app.get('/')) as expected, only app.use()"},{"fix":"Ensure you call `.handler()` on the `DynamicMiddleware` instance when integrating it into the Express/Connect application: `app.get('/', dm.handler())`. For error middleware, use `.errorHandler()`.","cause":"Trying to pass the `dynamic-middleware` instance (`dm`) directly to `app.get()` or `app.use()` instead of its `.handler()` method.","error":"TypeError: dm.handler is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}