Registry / devops / html-minifier-browser

html-minifier-browser

JSON →
library4.0.2jsnpmunverified

HTMLMinifier is a highly configurable, well-tested JavaScript-based HTML minifier that reduces HTML size by removing unnecessary spaces, comments, and attributes. The current stable version is 4.0.2, with regular updates via npm. It offers extensive options (e.g., collapseWhitespace, removeComments, minifyCSS, minifyJS) for fine-grained control over minification. Unlike alternatives like html-compressor or minimize, it handles inline CSS/JS compression and runs in the browser. Released since 2011, it remains a go-to tool for optimizing HTML for production with a focus on safety and performance.

npm install html-minifier-browser
INSTALL
IMPORT
SIG · HTML-MINIFIER-BROW
H
html-minifier-browser
devopsjavascriptv4.0.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

minify
import { minify } from 'html-minifier'
const minify = require('html-minifier')
ESM import is recommended for Node >= 10; default export is a namespace, not the function.
default
import htmlMinifier from 'html-minifier'
const htmlMinifier = require('html-minifier').default
Default import is the module namespace; use named import for the minify function.
MinifyOptions
import type { MinifyOptions } from 'html-minifier'
TypeScript users can import the options interface for type safety.

Demonstrates basic HTML minification with common options: collapsing whitespace, removing comments, and minifying inline CSS/JS.

import { minify } from 'html-minifier'; const html = '<html>\n <head>\n <title>Page</title>\n </head>\n <body>\n <div class="container">\n <p>Hello World</p>\n </div>\n </body>\n</html>'; const result = minify(html, { collapseWhitespace: true, removeComments: true, minifyCSS: true, minifyJS: true }); console.log(result); // Output: <html><head><title>Page</title></head><body><div class="container"><p>Hello World</p></div></body></html>
Debug
Known issues
breakingIn version 4.0.0, the API changed from a constructor to a plain function; minify is now the default export.
fix
Use import { minify } from 'html-minifier' or const { minify } = require('html-minifier') instead of new HTMLMinifier().minify().
affects: >=4.0.0
deprecatedThe option 'removeAttributeQuotes' has been deprecated in favor of 'removeAttributeQuotes' being part of 'collapseBooleanAttributes' and 'removeRedundantAttributes'.
fix
Use 'collapseBooleanAttributes' and 'removeRedundantAttributes' options instead.
affects: >=3.0.0
gotchaUsing 'minifyJS' or 'minifyCSS' options without proper config can cause syntax errors if the code is not valid JS/CSS.
fix
Set minifyJS/minifyCSS to true only when inline scripts/styles are valid; use a custom function for custom minification.
affects: >=1.0.0
gotchaThe library does not preserve HTML comments containing conditional comments (e.g., <!--[if IE]>). Setting 'removeComments: true' will strip them.
fix
Use 'removeComments: false' or set 'removeComments: /^!/' to keep conditional comments.
affects: >=1.0.0
gotchaWhen using 'collapseWhitespace: true' with inline elements like <span>, whitespace before/after may be removed incorrectly.
fix
Use 'conservativeCollapse: true' to preserve whitespace around inline elements.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: htmlMinifier is not a constructor
Using new htmlMinifier() after ESM import, but the default export is the module namespace.
fix
Use import { minify } from 'html-minifier' and call minify(html, options) directly.
Cannot find module 'html-minifier'
Package not installed or module resolution fails in browser environment.
fix
Install with npm install html-minifier and ensure bundler (e.g., Webpack) resolves it. For browser, use CDN: https://unpkg.com/html-minifier@4.0.2/dist/htmlminifier.min.js
Error: The options property 'removeAttributeQuotes' is not supported
Deprecated option removed in favor of other options.
fix
Remove 'removeAttributeQuotes' and use 'collapseBooleanAttributes' and 'removeRedundantAttributes' instead.
SyntaxError: Unexpected token: name (someVar) at minifyJS
Inline script contains syntax error when minified with minifyJS: true.
fix
Ensure inline scripts are syntactically valid, or use a custom minifyJS function with try-catch.
Upgrade
Version history
4.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
8
Resources
html-minifier-browser — npm install html-minifier-browser · libregistry