Environment information
Version:
2.2.0
Platform:
Node.js version:
20.19.2
Any other relevant information:
What steps will reproduce the bug?
import { json } from 'body-parser';
SyntaxError: The requested module 'body-parser' does not provide an export named 'json'
Named exports are found by static analysis:
https://nodejs.org/en/learn/modules/publishing-a-package
Node.js detects the named exports in CJS via static analysis that look for certain patterns, which the example above evades. To make the named exports detectable, do this:
defineProperty is a supported syntax, but a get() method can't be a require() call or a call to any other function, it can only return a simple 'identifier or member expression.'
|
Object.defineProperty(exports, 'json', { |
|
configurable: true, |
|
enumerable: true, |
|
get: () => require('./lib/types/json') |
|
}) |
https://github.com/nodejs/cjs-module-lexer/blob/main/README.md#getter-exports-parsing
To avoid matching getters that have side effects, any getter for an export name that does not support the forms above will opt-out of the getter matching:
Environment information
Version:
2.2.0
Platform:
Node.js version:
20.19.2
Any other relevant information:
What steps will reproduce the bug?
Named exports are found by static analysis:
https://nodejs.org/en/learn/modules/publishing-a-package
definePropertyis a supported syntax, but aget()method can't be arequire()call or a call to any other function, it can only return a simple 'identifier or member expression.'body-parser/index.js
Lines 30 to 34 in d96b63d
https://github.com/nodejs/cjs-module-lexer/blob/main/README.md#getter-exports-parsing