HTML Minifier Next
Your web page optimization precision tool: HTML Minifier Next (HMN) is a highly effective, super-configurable, well-tested HTML minifier, written in JavaScript, that also handles in-document CSS, JavaScript, and SVG minification.
The project was based on HTML Minifier Terser (HMT), which in turn had been based on Juriy “kangax” Zaytsev’s HTML Minifier (HM). HMN is the official successor to HTML Minifier: It’s actively maintained, richer in features, easier to use, and significantly faster (HMN engineering philosophy). Note that HMN is largely compatible with HM and HMT but isn’t fully drop-in—find migration guidance in the changelog.
⚠️ HTML Minifier Next is becoming HTML Minifier! Use the HTML Minifier Next to HTML Minifier migration guide to switch. (HTML Minifier Next will eventually be deprecated.)
Installation
HTML Minifier Next is ESM-only and requires Node.js ≥22.13.
For use as a command-line app, use npx:
npx html-minifier-next --help
(For immediate, zero-config use in the current folder: npx html-minifier-next --zero)
For programmatic use, install as a development dependency:
npm i -D html-minifier-next
General Usage
CLI
Use npx html-minifier-next --help to check all available options:
| Option |
Description |
Example |
--zero, -z |
Minify all HTML files in the current folder and its subfolders in place (except node_modules), using comprehensive settings (standalone—the flag is ignored when combined with other options) |
npx html-minifier-next --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-next -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-next input.html -o output.htmlFile to file (explicit): npx html-minifier-next -i input.html -o output.htmlPipe to file: cat input.html \| npx html-minifier-next -o output.htmlFile to STDOUT: npx html-minifier-next 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-next.config.json in the working directory, if present) |
--config-file=path/to/config.json |
--verbose, -v |
Show detailed processing information (active options, worker thread count, file statistics, and minifier warnings) |
npx html-minifier-next --input-dir=src --output-dir=dist --verbose --collapse-whitespace |
--dry, -d |
Dry run: Process and report statistics without writing output |
npx html-minifier-next input.html --dry --collapse-whitespace |
Configuration File
You can use a configuration file to specify options. If an html-minifier-next.config.json file is present in the current working directory—only there; subfolders (including --input-dir) and parent folders 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-next/main/html-minifier-next.schema.json",
"collapseWhitespace": true,
"removeComments": true
}
(If HMN is installed locally, you can also use the path ./node_modules/html-minifier-next/html-minifier-next.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-next';
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-next')—as minify() is async.)
See the original blog post for details of how it works, descriptions of most options, testing results, and conclusions.
Presets
HTML Minifier Next 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-next --preset conservative input.html
# Via config file
npx html-minifier-next --config-file=path/to/config.json input.html
# where config.json contains `{ "preset": "conservative" }`
# Override preset options
npx html-minifier-next --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.)