Registry / web-framework / express-body-parser-error-handler

express-body-parser-error-handler

JSON →
library1.0.9jsnpmunverified

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.

npm install express-body-parser-error-handler
INSTALL
IMPORT
SIG · EXPRESS-BODY-PARSE
E
express-body-parser-error-handler
web-frameworkjavascriptv1.0.9
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import bodyParserErrorHandler from 'express-body-parser-error-handler'
const bodyParserErrorHandler = require('express-body-parser-error-handler')
Package ships TypeScript types; supports both ESM and CJS. The default export is a function that returns middleware.
bodyParserErrorHandler
import bodyParserErrorHandler from 'express-body-parser-error-handler'
import { bodyParserErrorHandler } from 'express-body-parser-error-handler'
Only a default export exists; named import will be undefined.
BodyParserErrorHandlerOptions
import type { BodyParserErrorHandlerOptions } from 'express-body-parser-error-handler'
import { BodyParserErrorHandlerOptions } from 'express-body-parser-error-handler'
Type import for configuration options (onError, errorMessage, etc.)

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);
Debug
Known issues
gotchaMiddleware 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()).
affects: >=1.0.0
gotchaThe 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.
affects: >=1.0.0
gotchaerrorMessage 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`.
affects: >=1.0.0
gotchaThis 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.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: bodyParserErrorHandler is not a function
Named import instead of default import in TypeScript/ESM.
fix
Use `import bodyParserErrorHandler from 'express-body-parser-error-handler'` (no braces).
Cannot read properties of undefined (reading 'type')
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 [ERR_MODULE_NOT_FOUND]: Cannot find package 'express-body-parser-error-handler'
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.
Upgrade
Version history
1.0.9latest on npm
Audit
Dependencies
expressrequiredPeer dependency required; only works with Express applications
Agent activity
2 hits · last 30 days
node
2
Resources
express-body-parser-error-handler — npm install express-body-parser-error-handler · libregistry