{"id":18469,"library":"koa-compose","title":"koa-compose","description":"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.","status":"active","version":"4.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/koajs/compose","tags":["javascript","koa","middleware","compose"],"install":[{"cmd":"npm install koa-compose","lang":"bash","label":"npm"},{"cmd":"yarn add koa-compose","lang":"bash","label":"yarn"},{"cmd":"pnpm add koa-compose","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ESM default import is correct in Node >=12 with ESM enabled; require works in CommonJS but is less common for modern Koa projects.","wrong":"const compose = require('koa-compose')","symbol":"compose","correct":"import compose from 'koa-compose'"},{"note":"CommonJS import works but must be default, not destructured.","wrong":"const { compose } = require('koa-compose')","symbol":"compose","correct":"const compose = require('koa-compose')"},{"note":"Named import is incorrect; koa-compose exports a function as default.","wrong":"import { compose } from 'koa-compose'","symbol":"compose","correct":"import compose from 'koa-compose'"}],"quickstart":{"code":"import Koa from 'koa';\nimport compose from 'koa-compose';\n\nconst app = new Koa();\n\nconst logger = async (ctx, next) => {\n  console.log(`${ctx.method} ${ctx.url}`);\n  await next();\n};\n\nconst responseTime = async (ctx, next) => {\n  const start = Date.now();\n  await next();\n  const ms = Date.now() - start;\n  ctx.set('X-Response-Time', `${ms}ms`);\n};\n\nconst composed = compose([logger, responseTime]);\n\napp.use(composed);\napp.listen(3000);\nconsole.log('Server running on port 3000');","lang":"typescript","description":"Demonstrates composing two Koa middleware functions (logger and responseTime) using compose, then applying to a Koa app."},"warnings":[{"fix":"Always wrap middleware in an array: compose([mw1, mw2])","message":"compose expects an array of middleware; passing non-array will throw.","severity":"gotcha","affected_versions":">=4.0.0"},{"fix":"Define middleware as async (ctx, next) => { await next(); }","message":"Middleware must be async functions returning Promises; otherwise compose may not work correctly.","severity":"gotcha","affected_versions":">=4.0.0"},{"fix":"Use the returned function with app.use() or router middleware stack.","message":"compose returns a single middleware function; cannot be used directly as router-level middleware without wrapping.","severity":"gotcha","affected_versions":">=4.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Ensure compose is called with an array: compose([func1, func2])","cause":"Passing non-array or undefined to compose.","error":"TypeError: middleware is not a function"},{"fix":"Ensure each middleware calls next() exactly once, or handle conditional logic properly.","cause":"Calling next() more than once in a middleware function.","error":"Error: next() called multiple times"},{"fix":"Use proper import: import compose from 'koa-compose' or const compose = require('koa-compose')","cause":"Missing import or using incorrect import syntax.","error":"ReferenceError: compose is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}