Registry / devops / koa-html-minifier2

koa-html-minifier2

JSON →
library1.0.7jsnpmunverified

Koa 2.x middleware that minifies HTML responses using html-minifier. Version 1.0.7 is the latest stable release; development appears maintenance-mode with no recent updates. It is a straightforward port of the original koajs/html-minifier for Koa 2.x. Key differentiator: lightweight, no extra configuration beyond html-minifier options. However, all html-minifier defaults are off, so you must explicitly enable minification features.

npm install koa-html-minifier2
INSTALL
IMPORT
SIG · KOA-HTML-MINIFIER2
K
koa-html-minifier2
devopsjavascriptv1.0.7
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.

htmlMinifier2
const htmlMinifier2 = require('koa-html-minifier2')
import htmlMinifier2 from 'koa-html-minifier2'
Package is CommonJS only. ESM import will fail.
htmlMinifier2 (default)
app.use(htmlMinifier2({ collapseWhitespace: true }))
app.use(htmlMinifier2.collapseWhitespace(true))
Call htmlMinifier2 with an options object, not individual methods.
html-minifier options
htmlMinifier2({ minifyCSS: true, minifyJS: true })
htmlMinifier2({ minify: true })
Options are passed directly to html-minifier; see its documentation for available options.

Sets up a Koa app with HTML minification middleware; shows enabling common options.

const Koa = require('koa'); const htmlMinifier2 = require('koa-html-minifier2'); const app = new Koa(); app.use(htmlMinifier2({ collapseWhitespace: true, minifyCSS: true, minifyJS: true })); app.use(ctx => { ctx.type = 'text/html'; ctx.body = '<html> <head><style>body { color: red; }</style></head><body> <h1>Hello</h1><script>alert("test")</script></body></html>'; }); app.listen(3000);
Debug
Known issues
gotchaAll html-minifier options are disabled by default; without setting any, the middleware is a no-op.
fix
Pass an options object with at least one minification feature enabled, e.g., { collapseWhitespace: true }.
affects: >=1.0.0
deprecatedPackage is in maintenance mode; no updates since 2016. May not work with future Koa versions.
fix
Consider alternatives like koa-html-minifier or direct use of html-minifier in middleware.
affects: >=1.0.0
gotchaOnly minifies responses with Content-Type: text/html; otherwise skipped.
fix
Ensure your responses have the correct Content-Type header.
affects: >=1.0.0
gotchaDo NOT apply minification to streaming responses; middleware may buffer them incorrectly.
fix
Skip middleware for streaming responses by checking ctx.body is a stream.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: htmlMinifier2 is not a function
Using ESM import (import) on a CommonJS-only package.
fix
Use const htmlMinifier2 = require('koa-html-minifier2')
Error: Cannot find module 'html-minifier'
Missing peer dependency html-minifier.
fix
npm install html-minifier
Response not minified
No options set; all html-minifier defaults are off.
fix
Pass an options object with at least one true option, e.g., { collapseWhitespace: true }.
Upgrade
Version history
1.0.7latest on npm
Audit
Dependencies
html-minifierrequiredcore minification engine
Agent activity
7 hits · last 30 days
node
4
Resources
koa-html-minifier2 — npm install koa-html-minifier2 · libregistry