express-minify-html-2 is an Express.js middleware designed to automatically minify HTML responses rendered by an Express application, optimizing them for size. It functions as a maintained fork of the original `express-minify-html` package, explicitly created to keep the project alive and address issues like using outdated dependencies, most notably by integrating `html-minifier-terser` for robust and secure HTML minification. The current stable version is `2.0.0`. While it doesn't adhere to a strict release cadence, updates are released reactively for dependency upgrades, security fixes, and minor feature enhancements. The middleware either replaces Express's default `res.render` method with a minified version or provides an alternative `res.renderMin` function when the `override` option is set to `false`. This ensures that HTML served to clients is optimized for load times and bandwidth efficiency through configurable minification options passed directly to the underlying `html-minifier-terser` library.
npm install express-minify-html-2Verified import paths — ran on the pinned version, not inferred.
Demonstrates how to integrate `express-minify-html-2` into an Express application to automatically minify rendered HTML output. It configures the middleware with common minification options and shows how it intercepts `res.render`.
Upgrade your Node.js environment to version 14.0.0 or later. If using an older Node.js, you must stick to `express-minify-html-2` < 2.0.0, though this is not recommended due to other breaking/security changes.
Immediately upgrade to `express-minify-html-2` version 1.0.2 or later to benefit from the security fix and use the actively maintained `html-minifier-terser`.
Choose `override: true` if you want all `res.render` calls to be minified automatically. If you need selective minification, set `override: false` and explicitly call `res.renderMin` where minification is desired.
Refer to the official `html-minifier-terser` repository or documentation for detailed explanations of all available minification options (e.g., `removeComments`, `collapseWhitespace`, `minifyJS`, etc.).
Update your configuration to use `exceptionUrls` for new projects or when refactoring. Ensure you pass an array of strings, regexes, or functions as documented.
Ensure your Node.js version is 14.0.0 or higher. Run `npm install` or `npm ci` again to re-install dependencies. Check npm logs for specific installation failures.
Verify that `app = express()` is called and `app` is correctly instantiated before attempting to use middleware with `app.use()`.
Check the `override` option: `true` should minify all `res.render` calls. If `override: false`, ensure you're explicitly calling `res.renderMin`. Verify `exceptionUrls` array correctly excludes paths you want to minify, or that it is empty if you want all paths to be minified. Debug `htmlMinifier` options by starting with a minimal configuration. Ensure the response `Content-Type` is `text/html`.