How to fix Error: Middleware error handler requires 4 parameters (err, req, res, next) in Node.js

Node.jsINTERMEDIATEHIGHUpdated Dec 9, 2025ยท 153 views

Express requires error-handling middleware to have exactly four parameters: (err, req, res, next). Without all four parameters, Express treats the function as regular middleware instead of an error handler. This error occurs when your error handler doesn't follow the correct signature.

Quick fix

Fixes Error: Middleware error handler requires 4 parameters (err, req, res, next)

// CORRECT - 4 parameters
app.use((err, req, res, next) => {
  res.status(err.status || 500).send(err.message);
});

// WRONG - only 3 parameters (treated as regular middleware)
app.use((req, res, next) => {
  res.status(500).send('Error');
});
  1. Verify your error handler has exactly 4 parameters
  2. Ensure parameters are in the correct order
  3. Place error handler last in your middleware stack
  4. Make sure to handle or forward every error path
node.js โ€” error
// CORRECT - 4 parameters
app.use((err, req, res, next) => {
res.status(err.status || 500).send(err.message);
});
// WRONG - only 3 parameters (treated as regular middleware)
app.use((req, res, next) => {
res.status(500).send('Error');
});
Error: Middleware error handler requires 4 parameters (err, req, res, next)

What this error means

Express distinguishes error-handling middleware from regular middleware by the number of function parameters. Error handlers must accept exactly four arguments in the correct order: the error object, request object, response object, and next function. If your middleware function has fewer parameters or they're in the wrong order, Express won't recognize it as an error handler and won't execute it when errors occur. This is a design constraint built into Express to maintain a clear separation between normal request processing and error handling.

Typical symptoms

  • Errors thrown in routes or middleware are not caught by your error handler
  • Express displays its default error handling page instead of your custom error handler
  • Your error handler function is never called even though errors are occurring
  • Unhandled promise rejections bypass your error handling logic
  • Stack traces appear in the response instead of your custom error messages

Common causes

  1. Error handler middleware has only 3 parameters (err, req, res) instead of 4
  2. The 4th parameter (next) is missing entirely
  3. Parameters are in the wrong order
  4. Error handler is defined before other middleware and routes instead of last
  5. Using a regular middleware function signature for error handling instead of the 4-parameter signature
  6. Confusing error-handling middleware with catch-all middleware (express treats them differently)

How to fix "Error: Middleware error handler requires 4 parameters (err, req, res, next)"

1Verify your error handler has exactly 4 parameters

Check that your error-handling middleware function accepts all four parameters in the correct order:

javascript
// CORRECT - 4 parameters
app.use((err, req, res, next) => {
  res.status(err.status || 500).send(err.message);
});

// WRONG - only 3 parameters (treated as regular middleware)
app.use((req, res, next) => {
  res.status(500).send('Error');
});

Express identifies error-handling middleware by counting the number of function parameters. You must have all four, even if you don't use all of them.

2Ensure parameters are in the correct order

The parameter order must be: err, req, res, next.

javascript
// CORRECT order
app.use((err, req, res, next) => {
  console.error(err.message);
  res.status(500).json({ error: 'Internal Server Error' });
});

// WRONG - parameters in wrong order
app.use((req, res, err, next) => {
  // This won't work as error handler
});

The error object must be the first parameter for Express to recognize this as an error handler.

3Place error handler last in your middleware stack

Error-handling middleware must be defined after all other routes and middleware:

javascript
const express = require('express');
const app = express();

// Regular middleware and routes FIRST
app.use(express.json());
app.get('/api/data', (req, res) => {
  throw new Error('Something went wrong');
});

// Error handler LAST
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).json({
    message: err.message,
    error: process.env.NODE_ENV === 'production' ? {} : err,
  });
});

app.listen(3000);

If you define the error handler before your routes, it won't catch errors thrown in those routes.

4Make sure to handle or forward every error path

Your error handler must always send a response or call next(err). Failing to do so will cause requests to hang:

javascript
app.use((err, req, res, next) => {
  // GOOD - send a response
  res.status(err.status || 500).json({ error: err.message });

  // OR call next to pass to another error handler
  // next(err);

  // BAD - neither send nor call next (request hangs)
  // console.error(err);
});

Always ensure one of these paths is taken for every error.

5Test with a minimal example

Create a simple test to verify your error handler works:

javascript
const express = require('express');
const app = express();

// Test route that throws an error
app.get('/test-error', (req, res) => {
  throw new Error('Test error message');
});

// Error handler with 4 parameters
app.use((err, req, res, next) => {
  console.log('Error caught:', err.message);
  res.status(500).json({ error: err.message });
});

app.listen(3000, () => console.log('Server running'));

Visit http://localhost:3000/test-error and verify the error handler catches the thrown error.

Advanced notes

In async route handlers and middleware, remember that thrown errors won't automatically be caught by Express error handlers. You must use try-catch blocks or .catch() chains:

javascript
// Using try-catch with async
app.get('/async-route', async (req, res, next) => {
  try {
    const data = await fetchData();
    res.json(data);
  } catch (err) {
    next(err); // Pass to error handler
  }
});

// Or wrap with a utility
const asyncHandler = (fn) => (req, res, next) => {
  Promise.resolve(fn(req, res, next)).catch(next);
};

app.get('/route', asyncHandler(async (req, res) => {
  const data = await fetchData();
  res.json(data);
}));

Typescript developers should type the error handler properly:

typescript
import { Express, Request, Response, NextFunction } from 'express';

app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
  res.status(500).json({ message: err.message });
});

Frequently asked questions

What does "Error: Middleware error handler requires 4 parameters (err, req, res," mean?
Express distinguishes error-handling middleware from regular middleware by the number of function parameters. Error handlers must accept exactly four arguments in the correct order: the error object, request object, response object, and next function. If your middleware function has fewer parameters or they're in the wrong order, Express won't recognize it as an error handler and won't execute it when errors occur.
How do I fix "Error: Middleware error handler requires 4 parameters (err, req, res,"?
Express requires error-handling middleware to have exactly four parameters: (err, req, res, next). Without all four parameters, Express treats the function as regular middleware instead of an error handler. This error occurs when your error handler doesn't follow the correct signature.
What causes "Error: Middleware error handler requires 4 parameters (err, req, res,"?
Error handler middleware has only 3 parameters (err, req, res) instead of 4 The 4th parameter (next) is missing entirely Parameters are in the wrong order
Is the "Error: Middleware error handler requires 4 parameters (err, req, res," error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading