Express.js

raw JSON →
0.0.0 verified Sat Apr 25 auth: no javascript

Express.js is a foundational, unopinionated web application framework for Node.js, widely regarded as the de facto standard for building web applications and APIs. It provides a robust yet minimalist set of features, including a powerful routing system, HTTP utility methods, and a flexible middleware architecture. The current stable major version is Express 5.x, with Express 5.1.0 being the default on npm, and an official LTS schedule supports both v4 and v5 release lines. The release cadence for major versions can be extended, as seen with the significant time between Express 4 and Express 5, which focused on improving core stability, aligning with modern Node.js features, and incorporating security enhancements rather than introducing a vast array of new features. Its key differentiators include its lightweight nature, high extensibility through a vast ecosystem of middleware, and its "unopinionated" approach, allowing developers significant freedom in structuring their applications and choosing components like ORMs and template engines. It is often a core component of the "MEAN" or "MERN" stack for full-stack JavaScript development. Note: The package 'expresss' is a likely typo; this entry refers to the correct package 'express'.

error TypeError: app.del is not a function
cause Attempting to use the deprecated `app.del()` method in Express 5.
fix
Replace app.del('/path', handler) with app.delete('/path', handler).
error TypeError: Cannot read properties of undefined (reading 'body') or req.body is undefined
cause The body parsing middleware (`express.json()` or `express.urlencoded()`) is missing or incorrectly applied, especially in Express 5 where `req.body` is not initialized by default.
fix
Add app.use(express.json()); and/or app.use(express.urlencoded({ extended: true })); (if parsing URL-encoded data) before your routes that access req.body.
error ERR_REQUIRE_ESM or TypeError: require is not a function (when using import)
cause Mixing CommonJS `require()` syntax with ES Modules `import` syntax without proper configuration, or attempting to use `require()` in a file treated as an ESM module.
fix
Ensure consistency in your module system. If using ES Modules (with "type": "module" in package.json or .mjs files), use import statements. If sticking to CommonJS, use require(). For specific cases needing both, consider dynamic import() or createRequire in Node.js.
breaking Express 5.x requires Node.js version 18 or higher. Applications running on older Node.js versions must be upgraded before migrating to Express 5.
fix Upgrade Node.js to version 18 or later. Use `nvm` or your preferred Node.js version manager for easy switching: `nvm install 18 && nvm use 18`.
breaking Several deprecated method signatures from Express 4 have been removed in Express 5, including `app.del()`, `res.send(status, body)`, `res.send(status)`, `res.redirect('back')`, and `res.json(obj, status)`.
fix Use `app.delete()` instead of `app.del()`. For response methods, chain `res.status(statusCode).send(body)` or `res.sendStatus(statusCode)`. Replace `res.redirect('back')` with `res.redirect(req.get('Referrer') || '/')`. Consult the official migration guide for specific replacements.
breaking The path route matching syntax has stricter rules in Express 5, particularly concerning named wildcards (`*`) and regular expressions. For instance, `/*` should now be `/*splat`.
fix Review and update all route path definitions to conform to the new stricter syntax. Use named wildcards like `/*splat` for wildcard matches. Refer to the 'Migrating to Express 5' documentation for detailed examples.
breaking Body parsing behavior has changed in Express 5. `req.body` is no longer initialized to an empty object by default, and the `urlencoded` middleware's `extended` option now defaults to `false`. The standalone `bodyParser()` middleware is also removed.
fix Ensure `express.json()` and `express.urlencoded({ extended: true })` (if needed) middleware are explicitly added to your application. Access `req.body` only after these middleware have processed the request.
gotcha In Express 5, rejected promises returned from asynchronous middleware and route handlers are now automatically caught and forwarded to the error-handling middleware. This simplifies async error handling but might alter behavior for applications that previously handled rejections manually.
fix Review your asynchronous route handlers and middleware. Remove redundant `try/catch` blocks that just call `next(err)`, as Express 5 now handles this automatically. Ensure your main error-handling middleware is correctly set up to catch these forwarded errors.
npm install expresss
yarn add expresss
pnpm add expresss

This quickstart sets up a basic Express.js server with two routes: a root path returning 'Hello World!' and an API endpoint returning JSON data. It demonstrates server initialization and route handling. Ensure 'type': 'module' in package.json for ESM imports or use require().

import express from 'express';

const app = express();
const port = process.env.PORT ?? 3000;

app.get('/', (req, res) => {
  res.send('Hello from Express.js!');
});

app.get('/api/data', (req, res) => {
  res.json({ message: 'This is some API data!', timestamp: new Date() });
});

app.listen(port, () => {
  console.log(`Express app listening on port ${port}`);
  console.log(`Access at http://localhost:${port}`);
});