HTMLMinifier
HTMLMinifier is a highly configurable and well-tested JavaScript-based tool designed for aggressive HTML minification. The current stable version is 4.0.0. While minor bug fixes and dependency upgrades are released periodically, major version updates, like the transition from v3 to v4, occur less frequently. Its core functionality involves parsing HTML into a tree structure, applying various optimization rules, and then outputting a compacted HTML string. A key differentiator highlighted in its documentation is its effectiveness in reducing file sizes compared to several alternative solutions, making it suitable for performance optimization in web projects. It provides extensive options to control minification, including collapsing whitespace, removing comments, minifying inline CSS and JavaScript, and handling various HTML tag-specific optimizations.
Common errors
-
TypeError: htmlMinifier is not a function
cause Attempting to call the `html-minifier` module export directly as a function, rather than accessing its `minify` method.fixThe module exports an object. You need to destructure or access the `minify` function: `const { minify } = require('html-minifier');` or `const minify = require('html-minifier').minify;`. -
Error: The 'html-minifier' package requires Node.js version 6 or higher. You are running Node.js version 4.x.x.
cause Running `html-minifier` version 4.0.0 or higher on an unsupported Node.js environment.fixUpgrade your Node.js installation to version 6 or newer. Alternatively, if upgrading Node.js is not possible, downgrade `html-minifier` to a version compatible with your current Node.js setup (e.g., `npm install html-minifier@3` for Node.js < 6). -
Minified HTML output is not significantly smaller or comments/whitespace are still present.
cause Most minification options are disabled by default and must be explicitly enabled.fixProvide an options object to the `minify` function with desired features enabled, such as `collapseWhitespace: true`, `removeComments: true`, `minifyJS: true`, `minifyCSS: true`, etc. -
SyntaxError: Invalid or unexpected token in inline JavaScript/CSS during minification.
cause The default inline JavaScript (UglifyJS) or CSS (Clean-CSS) minifiers do not support modern syntax (e.g., ES6+ JavaScript) or encounter malformed input.fixFor JavaScript, consider using a custom `minifyJS` function with a modern minifier like Terser that supports ES6+. For CSS, ensure the inline CSS is valid. If the issue persists, disable inline minification for the problematic section or entire document.
Warnings
- breaking Version 4.0.0 of `html-minifier` dropped support for Node.js versions older than 6. Projects running on Node.js 0.x or 4.x will encounter errors.
- gotcha By default, almost all minification options are disabled. Users must explicitly enable desired minification features (e.g., `collapseWhitespace`, `removeComments`) to achieve any compression. Unexpectedly large output is often due to not configuring options.
- gotcha `html-minifier` cannot reliably process invalid or partial HTML markup. It parses the input into a tree structure, and malformed input can lead to unexpected or incorrect output, as it attempts to interpret it as a complete, valid tree.
- deprecated The default JavaScript minification library, UglifyJS, used by `html-minifier` for `minifyJS`, is not actively maintained and does not support modern JavaScript syntax (ES6+). This can lead to issues with contemporary JavaScript code.
- gotcha There is a newer, actively maintained alternative called `html-minifier-next` (and `html-minifier-terser`) that offers improved features, dependency updates, and better support for modern web standards and Node.js versions.
Install
-
npm install html-minifier -
yarn add html-minifier -
pnpm add html-minifier
Imports
- minify
import minify from 'html-minifier';
import { minify } from 'html-minifier'; - minify
const { minify } = require('html-minifier'); - minify
const htmlMinifier = require('html-minifier'); htmlMinifier();const minify = require('html-minifier').minify;
Quickstart
const { minify } = require('html-minifier');
const html = `
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello World</title>
<!-- This is a comment -->
<style>
body { font-family: sans-serif; color: #333; /* some comment */ }
</style>
</head>
<body>
<h1> Welcome to my Page! </h1>
<p>This is a paragraph with extra spaces.</p>
<script>
// Inline JavaScript
function greet() {
console.log("Hello from HTMLMinifier!");
}
greet();
</script>
</body>
</html>
`;
const minifiedHtml = minify(html, {
removeComments: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeEmptyAttributes: true,
removeOptionalTags: true,
minifyJS: true,
minifyCSS: true,
useShortDoctype: true,
sortAttributes: true,
sortClassName: true,
decodeEntities: true,
});
console.log(minifiedHtml);
/* Expected output:
<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Hello World</title><style>body{font-family:sans-serif;color:#333}</style></head><body><h1>Welcome to my Page!</h1><p>This is a paragraph with extra spaces.</p><script>function greet(){console.log("Hello from HTMLMinifier!")}greet()</script></body></html>
*/