{"id":18327,"library":"express-body-parser-error-handler","title":"Express Body Parser Error Handler","description":"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.","status":"active","version":"1.0.9","language":"javascript","source_language":"en","source_url":"https://github.com/ntedgi/express-body-parser-error-handler","tags":["javascript","express","body-parser","middleware","error-handler","typescript"],"install":[{"cmd":"npm install express-body-parser-error-handler","lang":"bash","label":"npm"},{"cmd":"yarn add express-body-parser-error-handler","lang":"bash","label":"yarn"},{"cmd":"pnpm add express-body-parser-error-handler","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency required; only works with Express applications","package":"express","optional":false}],"imports":[{"note":"Package ships TypeScript types; supports both ESM and CJS. The default export is a function that returns middleware.","wrong":"const bodyParserErrorHandler = require('express-body-parser-error-handler')","symbol":"default","correct":"import bodyParserErrorHandler from 'express-body-parser-error-handler'"},{"note":"Only a default export exists; named import will be undefined.","wrong":"import { bodyParserErrorHandler } from 'express-body-parser-error-handler'","symbol":"bodyParserErrorHandler","correct":"import bodyParserErrorHandler from 'express-body-parser-error-handler'"},{"note":"Type import for configuration options (onError, errorMessage, etc.)","wrong":"import { BodyParserErrorHandlerOptions } from 'express-body-parser-error-handler'","symbol":"BodyParserErrorHandlerOptions","correct":"import type { BodyParserErrorHandlerOptions } from 'express-body-parser-error-handler'"}],"quickstart":{"code":"import express from 'express';\nimport bodyParserErrorHandler from 'express-body-parser-error-handler';\nimport { json, urlencoded } from 'body-parser';\n\nconst app = express();\n\napp.use(urlencoded({ extended: false, limit: '250kb' }));\napp.use(json({ limit: '250kb' }));\n\n// Must be placed AFTER body parsers\napp.use(bodyParserErrorHandler({\n  onError: (err, req, res, next) => {\n    // Optional custom logging before default response\n    console.error(err);\n    next(err);\n  },\n  errorMessage: (err) => `Custom error: ${err.message}`\n}));\n\napp.get('/', (req, res) => res.json({ message: 'ok' }));\n\napp.listen(3000);","lang":"typescript","description":"Demonstrates basic setup: import, placement after body-parsers, and optional onError/errorMessage configuration for custom responses."},"warnings":[{"fix":"Always place app.use(bodyParserErrorHandler()) after all body-parser middleware calls (e.g., json(), urlencoded()).","message":"Middleware must be placed AFTER body-parser middleware; placed before, it will catch and ignore non-body-parser errors, silently swallowing them.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"In your onError function, always call next(err) (or next(error)) after your custom logic.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always return a string from errorMessage, even if you want to include the original: `return err.message`.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use Express's built-in error handler or a separate middleware for general errors.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use `import bodyParserErrorHandler from 'express-body-parser-error-handler'` (no braces).","cause":"Named import instead of default import in TypeScript/ESM.","error":"TypeError: bodyParserErrorHandler is not a function"},{"fix":"Ensure body-parser (or express.json, etc.) is used before this middleware and that body-parser is in your dependencies.","cause":"Middleware placed before body-parser or used without body-parser installed.","error":"Cannot read properties of undefined (reading 'type')"},{"fix":"Run `npm install express-body-parser-error-handler`. For ESM, use import syntax or set `\"type\": \"module\"` in package.json.","cause":"Package not installed or ESM project trying to require CJS module incorrectly.","error":"Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'express-body-parser-error-handler'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}