{"id":41248,"library":"html-minifier-with-remove-custom-attrs","title":"HTMLMinifier","description":"HTMLMinifier is a highly configurable, well-tested JavaScript-based HTML minifier for Node.js (version 4.2.0). It compresses HTML by removing unnecessary whitespace, comments, and redundant attributes, with options for custom attribute removal. Compared to alternatives like htmlcompressor and minimize, it consistently achieves superior size reduction (e.g., 42 KB vs 46 KB on Google homepage). The package supports CLI and programmatic usage, and is actively maintained with regular releases.","status":"active","version":"4.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/kangax/html-minifier","tags":["javascript","cli","compress","compressor","css","html","htmlmin","min"],"install":[{"cmd":"npm install html-minifier-with-remove-custom-attrs","lang":"bash","label":"npm"},{"cmd":"yarn add html-minifier-with-remove-custom-attrs","lang":"bash","label":"yarn"},{"cmd":"pnpm add html-minifier-with-remove-custom-attrs","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used for inline CSS minification when minifyCSS option is enabled.","package":"clean-css","optional":true},{"reason":"Used for inline JavaScript minification when minifyJS option is enabled.","package":"uglify-js","optional":true},{"reason":"Used for HTML entity encoding/decoding (e.g., decodeEntities, encodeAttributes).","package":"he","optional":true}],"imports":[{"note":"Both ESM import and CJS require work; this is the main function.","wrong":"const minify = require('html-minifier').minify","symbol":"minify","correct":"import { minify } from 'html-minifier'"},{"note":"Default export is an object with minify function; also available as named export.","wrong":"const htmlMinifier = require('html-minifier').default","symbol":"default","correct":"import htmlMinifier from 'html-minifier'"},{"note":"Subpath imports are not supported; always use the main package.","wrong":"const minify = require('html-minifier/minify')","symbol":"minify","correct":"const { minify } = require('html-minifier')"}],"quickstart":{"code":"import { minify } from 'html-minifier';\nimport * as fs from 'fs';\nconst html = fs.readFileSync('input.html', 'utf8');\nconst result = minify(html, {\n  removeAttributeQuotes: true,\n  removeComments: true,\n  collapseWhitespace: true,\n  minifyCSS: true, // requires clean-css\n  minifyJS: true,  // requires uglify-js\n  removeCustomAttrs: true, // custom attr removal\n  customAttrRemoveRegexes: [/^data-/] // remove data-* attributes\n});\nconsole.log(result);","lang":"typescript","description":"Minifies an HTML file with whitespace collapse, comment removal, CSS/JS minification, and custom attribute removal using HTMLMinifier."},"warnings":[{"fix":"Use 'customAttrRemove' (boolean) or 'customAttrRemoveRegexes' (array) instead.","message":"Option 'removeCustomAttrs' is deprecated in favor of 'customAttrRemove' and 'customAttrRemoveRegexes'.","severity":"deprecated","affected_versions":">=4.0"},{"fix":"Avoid 'decodeEntities' if you need to preserve non-ASCII characters.","message":"Unicode characters may be lost when using 'decodeEntities' option; use with caution.","severity":"gotcha","affected_versions":">=3.0"},{"fix":"Install optional dependencies: npm install clean-css uglify-js","message":"CSS/JS minification options require optional dependencies (clean-css, uglify-js) to be installed; missing them silently skips minification.","severity":"gotcha","affected_versions":">=4.0"},{"fix":"Set 'conservativeCollapse' to false and test output; consider 'preserveLineBreaks'.","message":"Using 'collapseWhitespace' with 'conservativeCollapse' may still remove some whitespace incorrectly for inline elements.","severity":"gotcha","affected_versions":">=3.0"},{"fix":"Replace 'removeEmptyAttributes' with 'removeEmpty' in options.","message":"Option 'removeEmptyAttributes' was renamed to 'removeEmpty' in v4.0.","severity":"breaking","affected_versions":">=4.0"}],"env_vars":null,"search_vec":"'4.2.0':18 '42':50 '46':53 'achiev':45 'activ':67 'altern':38 'attribut':29,34 'base':12 'cli':61,73 'comment':26 'compar':36 'compress':20,74 'compressor':75 'configur':6 'consist':44 'css':76 'custom':33 'e.g':49 'googl':56 'high':5 'homepag':57 'html':13,21,77 'htmlcompressor':40 'htmlmin':78 'htmlminifi':1,2 'javascript':11,72 'javascript-bas':10 'kb':51,54 'like':39 'maintain':68 'min':79 'minifi':14 'minim':42 'node.js':16 'option':31 'packag':59 'programmat':63 'reduct':48 'redund':28 'regular':70 'releas':71 'remov':23,35 'size':47 'superior':46 'support':60 'test':9 'unnecessari':24 'usag':64 'version':17 'vs':52 'well':8 'well-test':7 'whitespac':25","created_at":"2026-06-04T18:51:29.347489+00:00","updated_at":"2026-06-04T18:51:29.347489+00:00","problems":[{"fix":"Use const { minify } = require('html-minifier') or const minify = require('html-minifier').minify.","cause":"Misunderstanding of exported object structure when using require.","error":"TypeError: htmlMinifier.minify is not a function"},{"fix":"Run npm install clean-css (and uglify-js if minifyJS is true).","cause":"Optional dependency not installed when minifyCSS option is true.","error":"Error: Cannot find module 'clean-css'"},{"fix":"Use customAttrRemoveRegexes: [/^\\[.*\\]$/] to match Angular attribute syntax.","cause":"Angular or similar custom directives with attributes that are not recognized.","error":"Attribute attributes from directive not minified"},{"fix":"Set decodeEntities: false.","cause":"decodeEntities option set to true converts non-ASCII to HTML entities, altering content.","error":"Unicode characters lost during minification"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://kangax.github.io/html-minifier/","github":"https://github.com/kangax/html-minifier","docs":null,"changelog":null,"pypi":null,"npm":"html-minifier-with-remove-custom-attrs","openapi_spec":null,"status_page":null,"smithery":null,"categories":["devops"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-04","next_check":"2026-09-02","install_tag":null}}