diff --git a/HISTORY.md b/HISTORY.md index 7d3eeb62..5bf9f59b 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,9 @@ +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/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. 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",