Registry / http-networking / koa-etag

koa-etag

JSON →
library5.0.0jsnpmunverified

koa-etag is a Koa middleware package designed to provide ETag support for HTTP responses, leveraging the `etag` library to automatically generate ETag headers. This functionality is crucial for enhancing caching efficiency, as it allows clients (browsers, proxies) to conditionally request resources, thereby reducing bandwidth consumption and server load. The current stable version is 5.0.0, which notably features a complete rewrite in TypeScript, offering both ESM and CJS bundles, and requiring Node.js 18+. While there isn't a strict, frequent release cadence, major updates like v5.0.0 represent significant overhauls. Its primary differentiator is its seamless integration within the Koa ecosystem, typically working in conjunction with `koa-conditional-get` to enable full HTTP conditional GET support. However, it is important to note that the package's README strongly advises migrating to `@koa/etag` (v5+) as `koa-etag` is slated for deprecation in its next major release.

npm install koa-etag
INSTALL
IMPORT
SIG · KOA-ETAG
K
koa-etag
http-networkingjavascriptv5.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

etag
import etag from 'koa-etag';
import { etag } from 'koa-etag';
Since v5, the package is ESM-first with CJS interop, exposing the middleware as a default export. Named imports are incorrect.
etag (CommonJS)
const etag = require('koa-etag');
const { etag } = require('koa-etag');
For CommonJS environments, the middleware is the default export. Destructuring is incorrect.
KoaEtagOptions (TypeScript type)
import type { KoaEtagOptions } from 'koa-etag';
Available since v5, used for configuring the middleware with TypeScript.

This example demonstrates how to set up `koa-etag` in a Koa application, showing its typical usage alongside `koa-conditional-get` and `koa-compress`.

const Koa = require('koa'); const etag = require('koa-etag'); const conditional = require('koa-conditional-get'); const compress = require('koa-compress'); const app = new Koa(); // compress must be used before conditional and etag for correct ETag calculation app.use(compress()); // etag works together with conditional-get for full HTTP caching support app.use(conditional()); app.use(etag()); app.use(function (ctx) { ctx.body = 'Hello World'; }); app.listen(3000); console.log('Server listening on port 3000');
Debug
Known issues
deprecatedThe `koa-etag` package is in maintenance mode and will be formally deprecated in its next major release. Users are strongly advised to migrate to the `@koa/etag` package for continued support, bug fixes, and future feature updates, as it is the direct successor and recommended alternative.
fix
Replace `npm install koa-etag` with `npm install @koa/etag` and update import paths accordingly. The API is largely identical.
affects: >=5.0.0
gotchaFor correct ETag generation and response caching, the `koa-compress` middleware must be applied *before* `koa-conditional-get` and `koa-etag` in the middleware chain. Applying compression after ETag calculation will result in mismatched ETags for compressed content.
fix
Ensure `app.use(compress())` is called prior to `app.use(conditional())` and `app.use(etag())` in your Koa application setup.
affects: All versions
breakingVersion 5.x of `koa-etag` dropped support for older Node.js versions. It now explicitly requires Node.js 18 or higher due to modern language features and tooling updates.
fix
Upgrade your Node.js runtime environment to version 18 or later to use `koa-etag` v5.x.
affects: >=5.0.0
gotchaVersion 5.x was rewritten in TypeScript and bundled for both ESM and CJS. While `v5.0.2` improved CJS interop, incorrect import statements (e.g., named imports for default exports) can lead to errors.
fix
For CommonJS, use `const etag = require('koa-etag');`. For ESM, use `import etag from 'koa-etag';`. Avoid named imports like `import { etag } from 'koa-etag';`.
affects: >=5.0.0
Errors
Common errors & fixes
TypeError: etag is not a function
This error typically occurs when attempting to import `koa-etag` using a named import syntax (e.g., `import { etag } from 'koa-etag'`) or destructuring with `require` (`const { etag } = require('koa-etag')`), despite the package providing a default export.
fix
For CommonJS, use `const etag = require('koa-etag');`. For ESM, use `import etag from 'koa-etag';` to correctly import the default middleware function.
ETag headers are missing or not functioning correctly, despite `koa-etag` being installed.
A common cause is `koa-conditional-get` not being installed or used, or `koa-compress` being placed after `koa-etag` in the middleware stack. `koa-etag` only *generates* the ETag; `koa-conditional-get` handles the client-side conditional requests, and `koa-compress` must run first to ensure the ETag is calculated on the final (compressed) body.
fix
Ensure both `koa-conditional-get` and `koa-etag` are applied, and crucially, `app.use(compress())` must be called before `app.use(conditional())` and `app.use(etag())`.
Upgrade
Version history
5.0.0latest on npm
Audit
Dependencies
koarequiredPeer dependency for the Koa framework.
koa-conditional-getrequiredRequired for full conditional GET support, working alongside koa-etag.
koa-compressrequiredNecessary for handling compressed responses; must be applied before koa-etag.
Agent activity
6 hits · last 30 days
node
6
Resources
koa-etag — npm install koa-etag · libregistry