Express
Express is a fast, unopinionated, and minimalist web framework for Node.js. The current stable major versions are v5.2.1 and v4.22.1, with both branches actively maintained and receiving regular updates, including security patches and bug fixes. Version 5 offers improved security and drops support for older Node.js versions, while version 4 continues to provide stability for existing applications.
Common errors
-
Cannot GET /
cause No route handler is defined for the requested path and HTTP method.fixDefine a route using `app.get()`, `app.post()`, or `app.use()` that matches the incoming request path and method. -
Error: Can't set headers after they are sent to the client.
cause An attempt was made to send a response or modify headers after the response has already been sent to the client. This often happens in asynchronous operations or when multiple response-sending calls are made.fixEnsure that response-sending methods like `res.send()` or `res.json()` are called only once per request. Use `return next()` or `return res.send()` to prevent further execution after sending a response. -
Error: listen EADDRINUSE: address already in use :::3000
cause The port that Express is trying to listen on (e.g., 3000) is already in use by another process.fixKill the process using the port (e.g., `kill -9 <PID>` on Linux/macOS, or find in Task Manager on Windows) or configure your Express application to listen on a different port (e.g., `process.env.PORT || 4000`). -
TypeError: app.use() requires a middleware function but got a Object
cause An object or non-function value was passed to `app.use()` (or `app.get()`, `app.post()`, etc.) where a middleware function was expected.fixEnsure that all arguments passed to `app.use()` (and other route methods) are valid middleware functions, often imported or created as functions that take `(req, res, next)` as arguments. -
ReferenceError: express is not defined
cause The `express` module was not correctly imported or required at the top of the file, or the variable name `express` was shadowed/redefined.fixAdd `import express from 'express';` (for ESM) or `const express = require('express');` (for CommonJS) at the beginning of your file.
Warnings
- breaking Express v5.2.0 and v4.22.0 introduced an erroneous breaking change related to the extended query parser (CVE-2024-51999, later rejected). This was reverted in subsequent patch releases.
- breaking Express v5 requires Node.js v18 or newer. Older Node.js versions are no longer supported.
- deprecated The magic string 'back' for redirects (e.g., `res.redirect('back')`) has been deprecated and will be removed in a future major version.
- gotcha Versions of Express prior to 5.0.1 and 4.21.1 contained a vulnerability in the underlying `cookie` dependency (CVE-2024-47764).
Install
-
npm install express -
yarn add express -
pnpm add express
Imports
- express
import express from 'express'
Quickstart
import express, { Express, Request, Response } from 'express';
const app: Express = express();
const port = process.env.PORT ?? 3000;
app.get('/', (req: Request, res: Response) => {
res.send('Hello from Express!');
});
app.listen(port, () => {
console.log(`⚡️[server]: Server is running at http://localhost:${port}`);
});