Registry / devops / html-minifier

html-minifier

JSON →
library0.0.4jsnpmunverified

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-minifier
INSTALL
IMPORT
SIG · HTML-MINIFIER
H
html-minifier
devopsjavascriptv0.0.4
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

minify
import { minify } from 'html-minifier';
import minify from 'html-minifier';
The primary export is a named 'minify' function, not a default export.
minify
const { minify } = require('html-minifier');
CommonJS named import for Node.js environments. This is the most common usage pattern for older Node.js versions.
minify
const minify = require('html-minifier').minify;
const htmlMinifier = require('html-minifier'); htmlMinifier();
Direct assignment from the `minify` property of the module export. The module itself is an object with a `minify` method.

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.

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> */
html-minifier --version
Debug
Known issues
breakingVersion 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.
fix
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.
affects: >=4.0.0
gotchaBy 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.
fix
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`.
affects: >=1.0.0
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.
fix
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.
affects: >=1.0.0
deprecatedThe 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.
fix
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.
affects: <4.0.0
gotchaThere 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.
fix
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.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: htmlMinifier is not a function
Attempting to call the `html-minifier` module export directly as a function, rather than accessing its `minify` method.
fix
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;`.
Error: The 'html-minifier' package requires Node.js version 6 or higher. You are running Node.js version 4.x.x.
Running `html-minifier` version 4.0.0 or higher on an unsupported Node.js environment.
fix
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).
Minified HTML output is not significantly smaller or comments/whitespace are still present.
Most minification options are disabled by default and must be explicitly enabled.
fix
Provide 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.
The default inline JavaScript (UglifyJS) or CSS (Clean-CSS) minifiers do not support modern syntax (e.g., ES6+ JavaScript) or encounter malformed input.
fix
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.
Upgrade
Version history
0.0.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Amazon
1
Resources