koa2-connect
raw JSON → 1.0.2 verified Sat Apr 25 auth: no javascript maintenance
A small utility (v1.0.2, last updated 2019) that adapts Connect/Express-style middleware (req, res, next) to work with Koa 2.x async middleware (ctx, next). Unlike koa-connect, koa2-connect is designed to better support streaming middleware like webpack-dev-middleware and webpack-hot-middleware. It is a stopgap for when Koa-native alternatives are unavailable; not recommended for new projects where Koa-native middleware exists.
Common errors
error Error: next is not a function ↓
cause Your Express middleware expects a next callback, but koa2-connect may not pass one in certain failure cases.
fix
Ensure the middleware is compatible with Connect/Express and always calls next().
error TypeError: Cannot read property 'status' of undefined ↓
cause The Express middleware tried to access res.status, but Koa's response object has a different API.
fix
Use only raw Node.js http methods (res.writeHead, res.end) and avoid Express-specific functions like res.status.
error ReferenceError: require is not defined ↓
cause You are using ESM (ES modules) and need to use import instead of require.
fix
Use dynamic import: const c2k = (await import('koa2-connect')).default;
Warnings
gotcha Express middleware must call next() to allow Koa middleware downstream to execute. ↓
fix Ensure your Express middleware invokes next() when done, or use a Koa-native alternative.
gotcha The module does not provide types; you may need to add your own type declarations. ↓
fix Install @types/koa2-connect if available, or declare module 'koa2-connect' { ... }.
deprecated Package is in maintenance mode with no updates since 2019. Known issues may not be fixed. ↓
fix Consider migrating to native Koa middleware or using koa-connect as an alternative.
gotcha Express middleware that relies on synchronous response methods (e.g., res.sendStatus) may not trigger Koa's error handling correctly. ↓
fix Use a Koa-native error handler or wrap errors manually using try/catch inside the Express middleware.
Install
npm install koa2-connect yarn add koa2-connect pnpm add koa2-connect Imports
- c2k wrong
import c2k from 'koa2-connect'correctconst c2k = require('koa2-connect') - default wrong
import { convert } from 'koa2-connect'correctconst conv = require('koa2-connect') - Promise wrong
app.use(someExpressMiddleware)correctconst c2k = require('koa2-connect'); app.use(c2k(someExpressMiddleware))
Quickstart
const Koa = require('koa');
const c2k = require('koa2-connect');
const app = new Koa();
// Express-style middleware
function expressMiddleware(req, res, next) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello from Express');
next();
}
app.use(c2k(expressMiddleware));
app.use((ctx) => {
ctx.body = 'Koa response';
});
app.listen(3000);