From 9a058c786280c65a44fcb8a4c7e27ee0f683d692 Mon Sep 17 00:00:00 2001 From: Phillip Barta Date: Fri, 16 Jan 2026 16:52:00 +0100 Subject: [PATCH 1/2] fix: use static exports instead of lazy getters Static exports allow Node.js cjs-module-lexer to detect named exports correctly, improving ESM interop and bundler behavior. --- HISTORY.md | 5 +++++ index.js | 24 ++++-------------------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 7d3eeb62..1bf24c0a 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,8 @@ +unreleased +======================== + +* fix: use static exports instead of lazy getters to improve ESM compatibility + 2.2.2 / 2026-01-07 ========================= diff --git a/index.js b/index.js index 013ce5c4..c0526e16 100644 --- a/index.js +++ b/index.js @@ -24,41 +24,25 @@ exports = module.exports = bodyParser * JSON parser. * @public */ -Object.defineProperty(exports, 'json', { - configurable: true, - enumerable: true, - get: () => require('./lib/types/json') -}) +exports.json = require('./lib/types/json') /** * Raw parser. * @public */ -Object.defineProperty(exports, 'raw', { - configurable: true, - enumerable: true, - get: () => require('./lib/types/raw') -}) +exports.raw = require('./lib/types/raw') /** * Text parser. * @public */ -Object.defineProperty(exports, 'text', { - configurable: true, - enumerable: true, - get: () => require('./lib/types/text') -}) +exports.text = require('./lib/types/text') /** * URL-encoded parser. * @public */ -Object.defineProperty(exports, 'urlencoded', { - configurable: true, - enumerable: true, - get: () => require('./lib/types/urlencoded') -}) +exports.urlencoded = require('./lib/types/urlencoded') /** * Create a middleware to parse json and urlencoded bodies. From 9199b05aaf0f95172ceafc0e208baa2140882bc0 Mon Sep 17 00:00:00 2001 From: Phillip Barta Date: Fri, 16 Jan 2026 16:52:48 +0100 Subject: [PATCH 2/2] feat: add subpath exports for individual parsers Add an exports field to package.json exposing json, raw, text, and urlencoded parsers as subpath exports. This allows consumers to import individual parsers directly: - CommonJS: require('body-parser/json') - ESM: import json from 'body-parser/json' The change is fully backward compatible and enables users to opt into loading only the parsers they need. --- HISTORY.md | 1 + README.md | 7 +++++++ package.json | 12 ++++++++++++ 3 files changed, 20 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 1bf24c0a..5bf9f59b 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -2,6 +2,7 @@ unreleased ======================== * fix: use static exports instead of lazy getters to improve ESM compatibility +* feat: add subpath exports for individual parsers 2.2.2 / 2026-01-07 ========================= diff --git a/README.md b/README.md index 39d320f5..579be488 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,14 @@ $ npm install body-parser ## API ```js +// Import all parsers const bodyParser = require('body-parser') + +// Or import individual parsers directly +const json = require('body-parser/json') +const urlencoded = require('body-parser/urlencoded') +const raw = require('body-parser/raw') +const text = require('body-parser/text') ``` The `bodyParser` object exposes various factories to create middlewares. All diff --git a/package.json b/package.json index 486878a2..adeb5d58 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,18 @@ "type": "opencollective", "url": "https://opencollective.com/express" }, + "exports": { + ".": "./index.js", + "./package.json": "./package.json", + "./json": "./lib/types/json.js", + "./raw": "./lib/types/raw.js", + "./text": "./lib/types/text.js", + "./urlencoded": "./lib/types/urlencoded.js", + "./lib/*": "./lib/*.js", + "./lib/*.js": "./lib/*.js", + "./lib/types/*": "./lib/types/*.js", + "./lib/types/*.js": "./lib/types/*.js" + }, "dependencies": { "bytes": "^3.1.2", "content-type": "^1.0.5",