Connect-Next Middleware Framework
connect-next is an actively maintained fork of the classic Connect middleware framework for Node.js, providing a high-performance, pluggable HTTP server framework built around a "middleware" stack. The current stable version is 4.0.1, released as part of an active development cadence. Key differentiators from the original Connect include a complete rewrite in TypeScript, native ES module (ESM) publication, and a stricter Node.js engine requirement (currently `^20.19.0 || >=22.12.0`). It also ships with its own TypeScript types, eliminating the need for `@types/connect`. The project modernizes the proven Connect architecture, ensuring compatibility with contemporary Node.js practices and providing a robust foundation for web applications.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module ... connect-next/index.js from ... not supported.
cause Attempting to use CommonJS `require()` to import `connect-next`, which is a native ES module.fixRefactor your import statement from `const connect = require('connect-next');` to `import { connect } from 'connect-next';` and ensure your project uses ES modules. -
TypeError: connect_next_1.default is not a function
cause You are attempting to use a default import for `connect-next`, but it only exposes a named `connect` export.fixChange your import statement from `import connect from 'connect-next';` to `import { connect } from 'connect-next';`. -
SyntaxError: Must use import to load ES Module: ...connect-next/index.js
cause Your Node.js environment or file is configured for CommonJS, but `connect-next` is an ES module.fixEnsure your `package.json` has `"type": "module"` if you intend to use ESM globally, or use `.mjs` file extensions for individual ES module files. Always use `import { connect } from 'connect-next';`.
Warnings
- breaking Connect-Next requires Node.js version `^20.19.0 || >=22.12.0`. Older Node.js versions are not supported and will result in errors upon installation or execution.
- breaking Connect-Next is published as a native ES module (ESM) and only supports 'import' syntax. CommonJS 'require()' calls will fail.
- breaking When migrating from the original `connect` package, note that `connect-next` exposes a named `connect` export, not a default one. Additionally, `@types/connect` is no longer needed as types are included.
- gotcha Error-handling middleware must explicitly take four arguments: `(err, req, res, next)`. Middleware with fewer arguments are treated as regular middleware and will not catch errors passed to `next()`.
Install
-
npm install connect-next -
yarn add connect-next -
pnpm add connect-next
Imports
- connect
import connect from 'connect-next';
import { connect } from 'connect-next'; - connect (CommonJS)
const connect = require('connect-next');N/A
- Middleware types
import type { IncomingMessage, ServerResponse } from 'node:http'; import type { NextFunction } from 'connect-next';
Quickstart
import { connect } from 'connect-next';
import { createServer } from 'node:http';
import compression from 'compression';
import cookieSession from 'cookie-session';
import bodyParser from 'body-parser';
const app = connect();
// gzip/deflate outgoing responses
app.use(compression());
// store session state in browser cookie
app.use(
cookieSession({
keys: ['secret1', 'secret2'],
}),
);
// parse urlencoded request bodies into req.body
app.use(bodyParser.urlencoded({ extended: false }));
// respond to all requests
app.use((req, res) => {
res.end('Hello from Connect!\n');
});
// create an HTTP server and listen on port 3000
createServer(app).listen(3000);