Registry / devops / koa-html-minifier

koa-html-minifier

JSON →
library3.0.0jsnpmunverified

Koa middleware that minifies HTML responses using html-minifier-terser. Version 3.0.0 is the current stable release (last updated 2021). It wraps html-minifier-terser's default options (all off), so minification only occurs if explicitly configured. Unlike other HTML minifiers for Koa (e.g., koa-html-minify), this one uses the actively maintained terser fork with better ES6+ support. Requires Node >= 12 and works with Koa 2.

npm install koa-html-minifier
INSTALL
IMPORT
SIG · KOA-HTML-MINIFIER
K
koa-html-minifier
devopsjavascriptv3.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.

default export (factory)
import minify from 'koa-html-minifier'
const { minify } = require('koa-html-minifier')
Package exports a factory function that takes options and returns Koa middleware. Named export does not exist.
default export (require)
const minify = require('koa-html-minifier')
const { minify } = require('koa-html-minifier') or import { minify } from 'koa-html-minifier'
CommonJS users must require the default export. Common mistake is destructuring a non-existent named export.
TypeScript usage
import minify from 'koa-html-minifier'; const middleware = minify({ collapseWhitespace: true })
import * as minify from 'koa-html-minifier'
Package does not ship TypeScript definitions; use a custom .d.ts or @types. The default import is the factory.

Shows basic setup with koa-compress and html minification. Demonstrates that minify options must be explicitly set (defaults to no-op).

import Koa from 'koa'; import compress from 'koa-compress'; import minify from 'koa-html-minifier'; const app = new Koa(); // compress before minify app.use(compress()); // only collapses whitespace; other options off by default app.use(minify({ collapseWhitespace: true, removeComments: true, minifyCSS: true })); app.use(async (ctx) => { ctx.body = '<html><head><title>Test</title></head><body><p>Hello</p></body></html>'; }); app.listen(3000);
Debug
Known issues
gotchaDefault options have all features disabled; you must pass at least one option to get minification.
fix
Pass at least one minification option, e.g., `{ collapseWhitespace: true }`.
affects: >=1.0.0
gotchaPlace minifier after compression middleware (koa-compress) to avoid double processing.
fix
Call `app.use(compress())` before `app.use(minify(...))`.
affects: >=1.0.0
breakingDropped support for Node < 12 in version 3.0.0.
fix
Upgrade to Node >= 12 or stay on version 2.x.
affects: >=3.0.0
deprecatedSwitched underlying library from html-minifier to html-minifier-terser in v3. Options may differ slightly.
fix
Review html-minifier-terser options; some html-minifier options may be removed or renamed.
affects: >=3.0.0
Errors
Common errors & fixes
TypeError: minify is not a function
Tried to destructure named export `minify` instead of using default import.
fix
Use `import minify from 'koa-html-minifier'` (ESM) or `const minify = require('koa-html-minifier')` (CJS).
Error: Cannot find module 'html-minifier-terser'
Missing runtime dependency; needs to be installed explicitly because it's not bundled.
fix
Run `npm install html-minifier-terser` alongside koa-html-minifier.
No element found for minification (empty HTML)
Middleware processes only HTML responses (Content-Type: text/html). Non-HTML responses are skipped.
fix
Ensure your routes set `Content-Type` to `text/html` or use the middleware only for HTML routes.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies
koarequiredPeer dependency; required to use as middleware
html-minifier-terserrequiredRuntime dependency; performs actual minification
Agent activity
13 hits · last 30 days
node
10
Resources
koa-html-minifier — npm install koa-html-minifier · libregistry