Express Body Parser Error Handler

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

Middleware for Express that catches body-parser errors and returns appropriate 4xx HTTP responses with descriptive messages. v1.0.9 is stable, last updated in 2021. Handles all body-parser error types (invalid JSON, payload too large, unsupported charset, etc.) and maps them to correct status codes (400, 413, 415, 500). Ships TypeScript definitions. Unlike manual error handling in Express, this provides a consistent, one-line middleware approach. No breaking changes since v1.0.0.

error TypeError: bodyParserErrorHandler is not a function
cause Named import instead of default import in TypeScript/ESM.
fix
Use import bodyParserErrorHandler from 'express-body-parser-error-handler' (no braces).
error Cannot read properties of undefined (reading 'type')
cause Middleware placed before body-parser or used without body-parser installed.
fix
Ensure body-parser (or express.json, etc.) is used before this middleware and that body-parser is in your dependencies.
error Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'express-body-parser-error-handler'
cause Package not installed or ESM project trying to require CJS module incorrectly.
fix
Run npm install express-body-parser-error-handler. For ESM, use import syntax or set "type": "module" in package.json.
gotcha Middleware must be placed AFTER body-parser middleware; placed before, it will catch and ignore non-body-parser errors, silently swallowing them.
fix Always place app.use(bodyParserErrorHandler()) after all body-parser middleware calls (e.g., json(), urlencoded()).
gotcha The onError option expects a callback that eventually calls next(err). If you don't call next, the middleware will not pass the error to Express's default error handler and the client may never receive a response.
fix In your onError function, always call next(err) (or next(error)) after your custom logic.
gotcha errorMessage option returns a string that overwrites the default error message; if you return null/undefined, the original message is cleared and the client gets an empty message.
fix Always return a string from errorMessage, even if you want to include the original: `return err.message`.
gotcha This package only handles errors thrown by body-parser (or similar middleware that sets `err.type`). Other errors (e.g., route errors) will not be caught, which may lead to unhandled rejections.
fix Use Express's built-in error handler or a separate middleware for general errors.
npm install express-body-parser-error-handler
yarn add express-body-parser-error-handler
pnpm add express-body-parser-error-handler

Demonstrates basic setup: import, placement after body-parsers, and optional onError/errorMessage configuration for custom responses.

import express from 'express';
import bodyParserErrorHandler from 'express-body-parser-error-handler';
import { json, urlencoded } from 'body-parser';

const app = express();

app.use(urlencoded({ extended: false, limit: '250kb' }));
app.use(json({ limit: '250kb' }));

// Must be placed AFTER body parsers
app.use(bodyParserErrorHandler({
  onError: (err, req, res, next) => {
    // Optional custom logging before default response
    console.error(err);
    next(err);
  },
  errorMessage: (err) => `Custom error: ${err.message}`
}));

app.get('/', (req, res) => res.json({ message: 'ok' }));

app.listen(3000);