Registry / web-framework / koa-compose

koa-compose

JSON →
library4.1.0jsnpmunverified

Utility to compose Koa middleware functions into a single middleware. Current stable version is 4.1.0. Low release cadence; maintained as part of the Koa ecosystem. Key differentiator: it is the canonical middleware composition function for Koa, supporting async/await and error handling through promises. Alternatives like `koa-convert` or custom loops exist, but this is minimal and standard.

npm install koa-compose
INSTALL
IMPORT
SIG · KOA-COMPOSE
K
koa-compose
web-frameworkjavascriptv4.1.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.

compose
import compose from 'koa-compose'
const compose = require('koa-compose')
ESM default import is correct in Node >=12 with ESM enabled; require works in CommonJS but is less common for modern Koa projects.
compose
const compose = require('koa-compose')
const { compose } = require('koa-compose')
CommonJS import works but must be default, not destructured.
compose
import compose from 'koa-compose'
import { compose } from 'koa-compose'
Named import is incorrect; koa-compose exports a function as default.

Demonstrates composing two Koa middleware functions (logger and responseTime) using compose, then applying to a Koa app.

import Koa from 'koa'; import compose from 'koa-compose'; const app = new Koa(); const logger = async (ctx, next) => { console.log(`${ctx.method} ${ctx.url}`); await next(); }; const responseTime = async (ctx, next) => { const start = Date.now(); await next(); const ms = Date.now() - start; ctx.set('X-Response-Time', `${ms}ms`); }; const composed = compose([logger, responseTime]); app.use(composed); app.listen(3000); console.log('Server running on port 3000');
Debug
Known issues
gotchacompose expects an array of middleware; passing non-array will throw.
fix
Always wrap middleware in an array: compose([mw1, mw2])
affects: >=4.0.0
gotchaMiddleware must be async functions returning Promises; otherwise compose may not work correctly.
fix
Define middleware as async (ctx, next) => { await next(); }
affects: >=4.0.0
gotchacompose returns a single middleware function; cannot be used directly as router-level middleware without wrapping.
fix
Use the returned function with app.use() or router middleware stack.
affects: >=4.0.0
Errors
Common errors & fixes
TypeError: middleware is not a function
Passing non-array or undefined to compose.
fix
Ensure compose is called with an array: compose([func1, func2])
Error: next() called multiple times
Calling next() more than once in a middleware function.
fix
Ensure each middleware calls next() exactly once, or handle conditional logic properly.
ReferenceError: compose is not defined
Missing import or using incorrect import syntax.
fix
Use proper import: import compose from 'koa-compose' or const compose = require('koa-compose')
Upgrade
Version history
4.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
koa-compose — npm install koa-compose · libregistry