Registry / devops / ohiyajul-html-minifier

ohiyajul-html-minifier

JSON →
library4.0.0jsnpmunverified

A highly configurable, well-tested JavaScript-based HTML minifier. Current stable version is 4.0.0, with a mature release cadence (last release in 2020). It compresses HTML by removing unnecessary whitespace, comments, and attributes, and offers a wide range of options for fine-grained control. Compared to alternatives like minimize or htmlcompressor.com, it consistently achieves better compression ratios across various real-world websites. It supports both Node.js and CLI usage, and has TypeScript type definitions available via @types/html-minifier.

npm install ohiyajul-html-minifier
INSTALL
IMPORT
SIG · OHIYAJUL-HTML-MINI
O
ohiyajul-html-minifier
devopsjavascriptv4.0.0
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').minify
CommonJS require can also be used, but ES import is preferred. The named export 'minify' is the main function.
default
import htmlMinifier from 'html-minifier'
import { default } from 'html-minifier'
Default export provides the same 'minify' function. You can call htmlMinifier.minify() or directly use the default as a function.
Options
import type { Options } from 'html-minifier'
import { Options } from 'html-minifier'
Options is exported as a type, not a value. Use type-only import for TypeScript.

Demonstrates basic usage of the minify function with common options to collapse whitespace and remove comments.

import { minify } from 'html-minifier'; const result = minify('<p> Hello world! </p>', { collapseWhitespace: true, removeComments: true, minifyCSS: true, minifyJS: true }); console.log(result); // '<p>Hello world!</p>'
Debug
Known issues
gotchaSetting removeAttributeQuotes to true may break attributes with spaces or special characters.
fix
Use removeAttributeQuotes: false or ensure attributes don't contain spaces or special characters.
affects: >=1.0
gotchaUsing minifyCSS or minifyJS requires additional dependencies (clean-css, uglify-js) to be installed, otherwise an error is thrown.
fix
Install the required packages: npm install clean-css uglify-js
affects: >=3.0.0
breakingFrom version 4.0.0, Node.js 10+ is required; Node 8 and below are no longer supported.
fix
Upgrade Node.js to version 10 or later.
affects: >=4.0.0
deprecatedOption 'removeEmptyAttributes' has been deprecated in favor of 'removeEmptyOrBlankAttributes'.
fix
Use 'removeEmptyOrBlankAttributes' instead.
affects: >=3.0.0
gotchaThe CLI uses a different set of options compared to the API; boolean flags like --collapse-whitespace are used instead of camelCase.
fix
Refer to CLI documentation for correct flag names.
affects: >=1.0
gotchaDefault export in older versions (before 4.0) did not exist; only named export minify was available.
fix
Use named import: import { minify } from 'html-minifier'
affects: <4.0
Errors
Common errors & fixes
Error: Cannot find module 'clean-css'
The option minifyCSS is enabled but clean-css is not installed.
fix
Install clean-css: npm install clean-css
TypeError: htmlMinifier is not a function
Using default import incorrectly when the package version does not support it.
fix
Use named import: import { minify } from 'html-minifier'
Error: node: --openssl-legacy-provider is not allowed in NODE_OPTIONS
Running an older version of the package with Node.js 17+ which deprecated some SSL algorithms.
fix
Upgrade to the latest version of html-minifier (4.0.0+) that supports Node.js 17+.
Uncaught TypeError: Cannot read properties of undefined (reading 'replace')
Passing undefined or null into minify function instead of a string.
fix
Ensure the input is a string: minify(input || '', options)
Upgrade
Version history
4.0.0latest on npm
Audit
Dependencies
clean-cssoptionalUsed for inline CSS minification if enabled
uglify-jsoptionalUsed for inline JavaScript minification if enabled
Agent activity
2 hits · last 30 days
node
2
Resources