{"id":17624,"library":"express-minify-html-2","title":"Express HTML Minification Middleware","description":"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.","status":"active","version":"2.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/meszaros-lajos-gyorgy/express-minify-html-2","tags":["javascript","express","html","minifier","middleware","minify"],"install":[{"cmd":"npm install express-minify-html-2","lang":"bash","label":"npm"},{"cmd":"yarn add express-minify-html-2","lang":"bash","label":"yarn"},{"cmd":"pnpm add express-minify-html-2","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required as a peer dependency for the middleware to function within an Express application.","package":"express","optional":false},{"reason":"The core library responsible for HTML minification; this package is a wrapper around it.","package":"html-minifier-terser","optional":false}],"imports":[{"note":"The package exports a default function for the middleware. While the README shows CommonJS `require`, ESM default imports are standard in modern Node.js environments.","wrong":"import { minifyHTML } from 'express-minify-html-2'","symbol":"minifyHTML","correct":"import minifyHTML from 'express-minify-html-2'"},{"note":"This is the documented CommonJS import method shown in the official usage examples.","symbol":"minifyHTML","correct":"const minifyHTML = require('express-minify-html-2')"},{"note":"While the package is JavaScript, if TypeScript type definitions become available, this would be the correct way to import the configuration options type. As of v2.0.0, no explicit types are shipped, so this import would not resolve.","wrong":"import { Options } from 'express-minify-html-2'","symbol":"ConfigOptions","correct":"import type { Options } from 'express-minify-html-2'"}],"quickstart":{"code":"const express = require('express');\nconst minifyHTML = require('express-minify-html-2');\nconst path = require('path');\n\nconst app = express();\n\n// Basic view engine setup for demonstration\napp.set('views', path.join(__dirname, 'views'));\napp.set('view engine', 'ejs'); // Using EJS as an example\n\n// Create a dummy view file for testing (e.g., views/helloTemplate.ejs)\n// Ensure 'views' directory exists and 'helloTemplate.ejs' is present\n// Example helloTemplate.ejs:\n// <html><body>  <h1>Hello, <%= hello %>! </h1> <p>This is a test.</p>  </body></html>\n\napp.use(\n  minifyHTML({\n    override: true,\n    exceptionUrls: [], // No exceptions for this example\n    htmlMinifier: {\n      removeComments: true,\n      collapseWhitespace: true,\n      collapseBooleanAttributes: true,\n      removeAttributeQuotes: true,\n      removeEmptyAttributes: true,\n      minifyJS: true,\n      minifyCSS: true\n    },\n  }),\n);\n\napp.get('/hello', function (req, res, next) {\n  res.render('helloTemplate', { hello: 'world' }, function (err, html) {\n    if (err) return next(err);\n    console.log('Minified HTML output:', html);\n    res.send(html);\n  });\n});\n\n// Start the server\nconst PORT = process.env.PORT || 3000;\napp.listen(PORT, () => {\n  console.log(`Server running on http://localhost:${PORT}/hello`);\n  console.log('Ensure you have an EJS template at ./views/helloTemplate.ejs');\n});","lang":"javascript","description":"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`."},"warnings":[{"fix":"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.","message":"Version 2.0.0 and above require Node.js 14.0.0 or higher. Previous versions claimed compatibility with Node.js 4.0.0, but the underlying `html-minifier-terser` dependency mandates Node.js >=14.13.1.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"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`.","message":"Version 1.0.2 replaced the deprecated and potentially insecure `html-minifier` library with `html-minifier-terser` due to identified security vulnerabilities in the former. Users on `v1.0.1` or older are exposed to these risks.","severity":"breaking","affected_versions":"<1.0.2"},{"fix":"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.","message":"The `override` option (defaulting to `true`) determines whether the middleware hijacks the standard `res.render` function or adds a new `res.renderMin` function. Setting `override: false` requires you to explicitly call `res.renderMin`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Refer to the official `html-minifier-terser` repository or documentation for detailed explanations of all available minification options (e.g., `removeComments`, `collapseWhitespace`, `minifyJS`, etc.).","message":"Configuration options for minification are passed directly to `html-minifier-terser` via the `htmlMinifier` property. Developers should consult the `html-minifier-terser` documentation for a complete list of available options and their behaviors.","severity":"gotcha","affected_versions":">=1.0.2"},{"fix":"Update your configuration to use `exceptionUrls` for new projects or when refactoring. Ensure you pass an array of strings, regexes, or functions as documented.","message":"The `exception_url` option (for URLs to skip minification) has been aliased to `exceptionUrls` in v2.0.0 for camelCase consistency. While `exception_url` is still supported for backward compatibility, `exceptionUrls` is the preferred modern spelling.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"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.","cause":"The `html-minifier-terser` package or its dependencies failed to install correctly, often due to an incompatible Node.js version (e.g., running Node.js < 14.0.0) or corrupted `node_modules`.","error":"Error: Cannot find module 'html-minifier-terser'"},{"fix":"Verify that `app = express()` is called and `app` is correctly instantiated before attempting to use middleware with `app.use()`.","cause":"This error typically indicates that `app` is not a valid Express application instance or has not been correctly initialized as `express()` before `app.use` is called.","error":"TypeError: app.use is not a function"},{"fix":"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`.","cause":"This can be caused by incorrect `override` settings, misconfigured `exceptionUrls`, or `htmlMinifier` options not being applied as expected, or an incorrect `Content-Type` header being sent.","error":"HTML is not minified, or minification is inconsistent for certain routes."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}