HTML Minifier for JavaScript
Free online html minifier with JavaScript code examples
Working with html minifier in JavaScript? Our free online html minifier helps JavaScript developers format, validate, and process data instantly. Below you will find JavaScript code examples using html-minifier-terser so you can achieve the same result programmatically in your own projects.
Try the HTML Minifier Online
Use our free HTML Minifier directly in your browser — no setup required.
Open HTML MinifierJavaScript Code Example
const { minify } = require('html-minifier-terser');
const html = `
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
</head>
<body>
<!-- Main content -->
<div class="container">
<h1>Hello World</h1>
<p>Welcome to my site.</p>
</div>
</body>
</html>
`;
(async () => {
const result = await minify(html, {
collapseWhitespace: true,
removeComments: true,
removeEmptyAttributes: true,
minifyCSS: true,
minifyJS: true,
});
console.log(result);
})();Quick Setup
Library: html-minifier-terser
npm install html-minifier-terserJavaScript Tips & Best Practices
- html-minifier-terser is the maintained fork of html-minifier
- It can also minify inline CSS and JavaScript
- Use collapseWhitespace to remove unnecessary spaces