HTML Minifier
Your web page optimization precision tool: HTML Minifier (HM) is a highly effective, super-configurable, well-tested HTML minifier, written in JavaScript, that also handles in-document CSS, JavaScript, and SVG minification. Your result: leaner, faster, more sustainable pages (and fewer LLM tokens).
HTML Minifier was originally created by Juriy “kangax” Zaytsev. As of version 5, it’s actively maintained by Jens Oliver Meiert—richer in features, easier to use, and significantly faster (engineering philosophy).
ℹ️ Updating from HTML Minifier 4? Use the HTML Minifier 4 to 5 upgrade guide. Switching from HTML Minifier Next? Use the HTML Minifier Next to HTML Minifier migration guide.
Quick Start
- Single-file minification:
npx html-minifier --preset comprehensive input.html -o output.html
- Zero-config, in-place minification in the current directory and subdirectories:
npx html-minifier --zero (asks for confirmation; preview with --dry)
- Configuration wizard (beta) in a directory of unminified web pages:
npx html-minifier-setup (proposes optimal configuration)
- Installation guides: Astro · Eleventy · Jekyll · VitePress (your guide?)
- Manual minification, experimentation, and sample-sharing: HTML Minifier web version
Installation
HTML Minifier is ESM-only and requires Node.js ≥22.13.
For use as a command-line app, use npx:
npx html-minifier --help
For programmatic use, install as a development dependency:
npm i -D html-minifier
General Usage
CLI
Use npx html-minifier --help to check all available options:
| Option |
Description |
Example |
--zero, -z |
Minify all HTML files in the current directory and its subdirectories in place (except node_modules), using comprehensive settings (standalone, with --dry to preview, or with --tokens—the flag is ignored when combined with other options) |
npx html-minifier --zero |
--input-dir <dir>, -I <dir> |
Specify an input directory |
--input-dir=src |
--ignore-dir <patterns>, -X <patterns> |
Exclude directories—relative to input directory—from processing (comma-separated, overrides config file setting) |
--ignore-dir=libs, --ignore-dir=libs,vendor,node_modules |
--output-dir <dir>, -O <dir> |
Specify an output directory |
--output-dir=dist |
--workers <n>, -w <n> |
Number of worker threads for multi-file runs; defaults to half the available cores (at most 6) for runs large enough to justify them |
--workers=4 |
--input <file>, -i <file> |
Specify input file (alternative to positional argument; pair with --output for file output) |
npx html-minifier -i input.html -o output.html |
--output <file>, -o <file> |
Specify output file (reads from --input file argument or STDIN; outputs to STDOUT if not specified) |
File to file: npx html-minifier input.html -o output.htmlFile to file (explicit): npx html-minifier -i input.html -o output.htmlPipe to file: cat input.html \| npx html-minifier -o output.htmlFile to STDOUT: npx html-minifier input.html |
--file-ext <extensions>, -f <extensions> |
Specify file extension(s) to process (comma-separated, overrides config file setting); defaults to html,htm,shtml,shtm; use * for all files |
--file-ext=html,php, --file-ext='*' |
--preset <name>, -p <name> |
Use a preset configuration (conservative or comprehensive) |
--preset=conservative |
--config-file <file>, -c <file> |
Use a configuration file (defaults to html-minifier.config.json in the working directory, if present) |
--config-file=path/to/config.json |
--tokens, -t |
Add LLM token savings (as per OpenAI’s o200k_base) to the statistics reported |
npx html-minifier --input-dir=src --output-dir=dist --tokens |
--verbose, -v |
Show detailed processing information (active options, worker thread count, file statistics, and minifier warnings) |
npx html-minifier --input-dir=src --output-dir=dist --verbose --collapse-whitespace |
--dry, -d |
Dry run: Process and report statistics without writing output (without input, previews --zero) |
npx html-minifier input.html --dry --collapse-whitespace |
Configuration File
You can use a configuration file to specify options. If an html-minifier.config.json file is present in the current working directory—only there; subdirectories (including --input-dir) and parent directories are not searched—the CLI picks it up automatically, with a note on STDERR confirming this. An explicit --config-file takes precedence. (The standalone --zero mode is deliberately config-free and ignores default config files.) The file can be either in JSON format or a JavaScript module that exports the configuration object:
JSON configuration example:
{
"collapseWhitespace": true,
"removeComments": true,
"fileExt": "html,php",
"ignoreDir": "libs,vendor"
}
For editor support (validation, autocomplete, and inline documentation) in JSON configuration files, reference the JSON Schema that ships with the package:
{
"$schema": "https://raw.githubusercontent.com/j9t/html-minifier/main/html-minifier.schema.json",
"collapseWhitespace": true,
"removeComments": true
}
(If HM is installed locally, you can also use the path ./node_modules/html-minifier/html-minifier.schema.json instead of the URL.)
JavaScript module configuration example (requires "type": "module" in the project’s package.json, or use a .mjs extension):
export default {
collapseWhitespace: true,
removeComments: true,
fileExt: "html,php",
ignoreDir: ["libs", "vendor"]
};
A module can also hold values JSON cannot express, like regular expressions (e.g., ignoreCustomFragments: [/\{\{[\s\S]*?\}\}/]) and functions (for minifyCSS, minifyJS, and minifyURLs, or in custom SVGO plugins for minifySVG). As functions cannot be passed to worker threads, a run using them minifies in-process.
Node.js
import { minify } from 'html-minifier';
const result = await minify('<p title="example" id="moo">foo</p>', {
removeAttributeQuotes: true,
removeOptionalTags: true
});
console.log(result); // `<p title=example id=moo>foo`
(CommonJS consumers can still use dynamic import—const { minify } = await import('html-minifier')—as minify() is async.)
Presets
HTML Minifier provides presets for common use cases. Presets are pre-configured option sets that can be used as a starting point:
conservative: Basic minification with whitespace collapsing, comment removal, and removal of select attributes.
comprehensive: More advanced minification for better file size reduction, including relevant conservative options plus attribute quote removal, optional tag removal, and more.
To review the specific options set, presets.js lists them in an accessible manner. For output served with Gzip or Brotli, see “Optimizing for Compression” for what to add.
Using presets:
# Via CLI flag
npx html-minifier --preset conservative input.html
# Via config file
npx html-minifier --config-file=path/to/config.json input.html
# where config.json contains `{ "preset": "conservative" }`
# Override preset options
npx html-minifier --preset conservative --remove-empty-attributes input.html
Priority order: Presets are applied first, then config file options, then CLI flags. This allows you to start with a preset and customize as needed.
Options Quick Reference
Most of the options are disabled by default. Experiment and find what works best for you and your project.
Options can be used in config files (camelCase) or via CLI flags (kebab-case with -- prefix). Boolean options generally support both --option-name to enable and --no-option-name to disable, so you can override a preset or config file from the command line. (Exception: Options whose name already starts with no-, such as noNewlinesBeforeTagClose, only expose the --no-… CLI flag.)