{"id":11045,"library":"html-minifier-terser","title":"HTMLMinifier-Terser","description":"HTMLMinifier-Terser is a highly configurable, well-tested JavaScript-based HTML minifier designed to optimize web page load times by significantly reducing the size of HTML, CSS, and JavaScript within HTML documents. The current stable version is 7.2.0, with regular updates addressing bug fixes, dependency updates, and minor feature enhancements. Major versions, like v7.0.0, introduce significant breaking changes such as the migration to ESM and updated Node.js requirements. It differentiates itself through extensive configurability, allowing fine-grained control over the minification process, and its strong performance compared to other minifiers, often yielding smaller output sizes as demonstrated in its minification comparison benchmarks.","status":"active","version":"7.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/terser/html-minifier-terser","tags":["javascript","cli","compress","compressor","css","html","htmlmin","min"],"install":[{"cmd":"npm install html-minifier-terser","lang":"bash","label":"npm"},{"cmd":"yarn add html-minifier-terser","lang":"bash","label":"yarn"},{"cmd":"pnpm add html-minifier-terser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Since v7.0.0, the package primarily supports ESM. While CJS exports are provided, ESM is the recommended approach for modern Node.js applications.","wrong":"const { minify } = require('html-minifier-terser');","symbol":"minify","correct":"import { minify } from 'html-minifier-terser';"},{"note":"Asynchronous CSS minification (`minifyCSS`) is supported since v7.0.0-alpha.2, requiring an `await` call if async options are used.","symbol":"minify with async options","correct":"import { minify } from 'html-minifier-terser';\n// ... later await minify(html, { minifyCSS: { level: 2 } })"}],"quickstart":{"code":"import { minify } from 'html-minifier-terser';\n\nasync function runMinification() {\n  const htmlInput = `\n    <!DOCTYPE html>\n    <html lang=\"en\">\n    <head>\n      <meta charset=\"UTF-8\">\n      <title>Test Page</title>\n      <style>body { color: red; } /* important */</style>\n    </head>\n    <body>\n      <!-- This is a comment -->\n      <p title=\"  some title  \" id=\"moo\">Hello, world!  </p>\n      <script>  function greet() { console.log('Hi!'); }  </script>\n    </body>\n    </html>\n  `;\n\n  const minifiedHtml = await minify(htmlInput, {\n    removeComments: true,\n    collapseWhitespace: true,\n    minifyJS: true,\n    minifyCSS: true,\n    removeAttributeQuotes: true,\n    processConditionalComments: true,\n  });\n\n  console.log(minifiedHtml);\n  // Expected output (approx): \"<!DOCTYPE html><html lang=en><head><meta charset=UTF-8><title>Test Page</title><style>body{color:red}</style></head><body><p title=some title id=moo>Hello, world!</p><script>function greet(){console.log('Hi!')}</script></body></html>\"\n}\n\nrunMinification();","lang":"typescript","description":"This quickstart demonstrates how to programmatically minify HTML using various common options like collapsing whitespace, removing comments, and minifying embedded JavaScript and CSS."},"warnings":[{"fix":"Update your import statements to use ESM syntax (e.g., `import { minify } from 'html-minifier-terser';`). If you are in a CJS environment, ensure your build setup correctly handles dual ESM/CJS packages.","message":"Version 7.0.0 migrated the build tool to Rollup, replaced the `he` dependency with `entities`, and rewrote the package to primarily support ESM. This impacts how the package is imported and consumed.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"Upgrade your Node.js environment to a compatible version (v14.13.1 or v16.0.0 or greater) before upgrading to `html-minifier-terser@7.x`.","message":"The minimum required Node.js version was increased to `v14.13.1` or `>=16.0.0` with the release of v7.0.0.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"Carefully review the documentation for available options and enable them based on your project's minification requirements.","message":"Almost all minification options are disabled by default. Users must explicitly enable options like `collapseWhitespace`, `removeComments`, `minifyJS`, and `minifyCSS` to achieve desired minification levels.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For CLI, run `npm install -g html-minifier-terser` or `npx html-minifier-terser`. For programmatic use, `npm install html-minifier-terser`.","message":"Using the CLI via `html-minifier-terser` requires installing the package globally or using `npx`. For programmatic use, install it as a local dependency.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Convert your consuming module to ESM by adding `\"type\": \"module\"` to your `package.json` or changing your file extension to `.mjs`. Alternatively, use dynamic `import()`: `const { minify } = await import('html-minifier-terser');`","cause":"Attempting to `require()` the package in a CommonJS module while it has been transitioned to primarily ESM in v7.","error":"ERR_REQUIRE_ESM: require() of ES Module .../html-minifier-terser/dist/htmlminifier.js from ... not supported."},{"fix":"Ensure the first argument passed to `minify` is always a valid string containing the HTML to be minified.","cause":"This can occur if the input HTML passed to `minify` is not a string or is `undefined`.","error":"TypeError: Cannot read properties of undefined (reading 'split')"}],"ecosystem":"npm"}