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.
npm install html-minifierVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to programmatically minify an HTML string using a comprehensive set of common options for maximum compression, showing before and after output.
Upgrade your Node.js environment to version 6 or higher, or pin `html-minifier` to a version lower than 4.0.0 if unable to upgrade Node.js.
Thoroughly review the extensive list of available options in the documentation and enable them based on your project's needs. A good starting point is often `collapseWhitespace`, `removeComments`, `minifyJS`, and `minifyCSS`.
Ensure the input HTML is well-formed and complete before passing it to `html-minifier`. For templating engines that output fragments, consider minifying the full, rendered HTML page.
For projects requiring ES6+ JavaScript minification, configure a custom `minifyJS` function with a modern minifier like Terser, or consider using `html-minifier-terser` which bundles Terser directly. Version 4.x updates its dependencies, but this note is particularly relevant for older versions.
Consider migrating to `html-minifier-next` for ongoing development, as it addresses many limitations and provides a more up-to-date solution for HTML minification.
The 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;`.Upgrade 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).
Provide an options object to the `minify` function with desired features enabled, such as `collapseWhitespace: true`, `removeComments: true`, `minifyJS: true`, `minifyCSS: true`, etc.
For 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.
No dependency data recorded yet.