Skip to content

Conversation

@Phillip9587
Copy link
Member

Ref: #666 (comment)

This PR contains 2 commits:

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.

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.

Closes #666

Static exports allow Node.js cjs-module-lexer to detect named
exports correctly, improving ESM interop and bundler behavior.
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.
},
"exports": {
".": "./index.js",
"./package.json": "./package.json",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would it be necessary to add the package.json?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added ./package.json to the exports field to avoid breaking existing tooling and user setups that rely on require('body-parser/package.json') to read metadata (e.g. version info). Once exports is defined, Node.js treats it as an allowlist, so this preserves backward compatibility.

Docs: https://nodejs.org/api/packages.html#package-entry-points

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

no named exports for ESM projects

2 participants