{"id":17623,"library":"express-minify-html","title":"Express HTML Minification Middleware","description":"express-minify-html is an Express middleware designed to automatically minify HTML responses generated by `res.render` calls. It leverages the robust `html-minifier` library under the hood, passing configuration options directly to it. The current stable version is 0.12.0. While there's no stated release cadence, its versioning suggests a pre-1.0 status, implying potential API changes in minor versions. A key differentiator is its seamless integration with Express's existing `res.render` function, with an option to `override` it directly or expose a new `res.renderMin` method. It also supports `exception_url` configurations, allowing developers to selectively bypass minification for specific routes using strings, regular expressions, or custom functions. This helps optimize performance by reducing HTML payload size for most requests without affecting critical or dynamic paths that might be sensitive to minification.","status":"active","version":"0.12.0","language":"javascript","source_language":"en","source_url":"https://github.com/melonmanchan/express-minify-html","tags":["javascript","express","html","minifier","middleware","minify"],"install":[{"cmd":"npm install express-minify-html","lang":"bash","label":"npm"},{"cmd":"yarn add express-minify-html","lang":"bash","label":"yarn"},{"cmd":"pnpm add express-minify-html","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for Express application integration as a middleware.","package":"express"},{"reason":"Core dependency for the HTML minification logic.","package":"html-minifier"}],"imports":[{"note":"The package primarily uses CommonJS (`require`) syntax. Direct ESM `import` might not function as expected without specific transpilation or Node.js loader configurations.","wrong":"import minifyHTML from 'express-minify-html';","symbol":"minifyHTML","correct":"const minifyHTML = require('express-minify-html');"},{"note":"The middleware factory function `minifyHTML` must be called with an options object (even if empty) to return the actual middleware function for `app.use`.","wrong":"app.use(minifyHTML);","symbol":"app.use(minifyHTML(...))","correct":"app.use(minifyHTML({ /* options */ }));"}],"quickstart":{"code":"const express = require('express');\nconst minifyHTML = require('express-minify-html');\nconst path = require('path');\n\nconst app = express();\n\n// Configure a simple view engine 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 (e.g., views/helloTemplate.ejs)\n// console.log('Create views/helloTemplate.ejs with content like: <div>  <h1>Hello  <%= hello %></h1>  <p> This is a paragraph. </p></div>');\n\napp.use(minifyHTML({\n    override:      true,\n    exception_url: [],\n    htmlMinifier: {\n        removeComments:            true,\n        collapseWhitespace:        true,\n        collapseBooleanAttributes: true,\n        removeAttributeQuotes:     true,\n        removeEmptyAttributes:     true,\n        minifyJS:                  true\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:\\n' + html); // The output is minified\n        res.send(html);\n    });\n});\n\napp.get('/', (req, res) => {\n  res.send('Go to /hello to see minified HTML');\n});\n\nconst PORT = process.env.PORT ?? 3000;\napp.listen(PORT, () => {\n    console.log(`Server listening on port ${PORT}`);\n    console.log('Ensure you have a views/helloTemplate.ejs file.');\n});","lang":"javascript","description":"This quickstart demonstrates how to apply the `express-minify-html` middleware globally to an Express application, showing its effect on HTML rendered via `res.render`."},"warnings":[{"fix":"Either set `override: true` to have `res.render` automatically minify, or ensure all calls needing minification are updated to `res.renderMin` when `override` is `false`.","message":"Setting the `override` option to `false` will prevent the middleware from modifying the standard `res.render` function. Instead, you must explicitly call `res.renderMin` to get minified HTML. Failing to do so will result in unminified output.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Carefully consult the official `html-minifier` documentation for recommended and safe configuration options. Start with basic options like `collapseWhitespace` and incrementally add others.","message":"Incorrect or overly aggressive configuration of the `htmlMinifier` options can lead to broken HTML, unexpected JavaScript behavior, or styling issues on the client-side. Always test minified output thoroughly.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Use `const minifyHTML = require('express-minify-html');` for consistent behavior, especially in older Node.js projects or those not configured for ESM-CJS interop.","message":"This package is designed for CommonJS (`require`). While modern Node.js environments might allow some interop, direct ESM `import` statements might not work correctly without specific bundler configurations (e.g., Webpack, Rollup) or Node.js loader setups.","severity":"gotcha","affected_versions":"<1.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Run `npm install express-minify-html` in your project directory.","cause":"The `express-minify-html` package has not been installed or is not resolvable in the current project environment.","error":"Error: Cannot find module 'express-minify-html'"},{"fix":"Ensure `const express = require('express');` and `const app = express();` are correctly set up before calling `app.use()`.","cause":"`app` is not an initialized Express application instance, or `express()` was not called.","error":"TypeError: app.use is not a function"},{"fix":"Use `const minifyHTML = require('express-minify-html');` to correctly import the middleware factory.","cause":"The `minifyHTML` symbol was imported incorrectly, often by attempting `import minifyHTML from 'express-minify-html'` in a CommonJS-first environment.","error":"TypeError: minifyHTML is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}