ARTICLE AD BOX
I'm updating a Node.js project to support both CommonJS and ES Modules (Dual Package). My current issue appears when trying to load the module from the project root using exports field instead of main.
Project Structure
MiProject/ ├── src/ │ ├── index.js //CommonJS (module.exports) │ └── index.mjs // ES Module (export default) ├── test/ │ ├── require.test.js ← uses require('../') │ └── import.test.js ← uses import from '../' └── package.jsonCurrent Behavior
Inside test/require.test.js:
const mymodule = require('../');This works only when package.json uses:
{ "main": "./src/index.js" }But if I replace main with:
{ "exports": "./src/index.js" }Node.js fails to load the module using require('../').
I need to implement conditional exports so the package can be imported using:
import mymodule from 'MiProject'; // ESM const mymodule = require('MiProject'); // CommonJSMeaning I must use exports instead of main, with support for both syntaxes.
