JavaScript · Modules · Senior · Architecture
How would you design package `exports` for a library supporting both ESM and CJS consumers?
Short Interview Answer
Use package.json `exports` conditional maps carefully, avoid dual-package hazards, and publish clear ESM-first entrypoints with tested interop.
Detailed Explanation
Dual package hazard: two copies of a module loaded via ESM and CJS break singletons (`instanceof` fails). Prefer ESM-only when possible, or a thin CJS wrapper that re-exports without duplicating state. `exports` field restricts deep imports and maps `import`/`require`/`types` conditions. Provide `type: module` or `.mjs`/`.cjs` extensions deliberately. Document node/browser conditions. Test in both Jest/Node and bundlers. Version breaking export map changes carefully. Lead-level packaging decisions affect the whole ecosystem of consumers.
Interview Tip
Name the dual-package hazard — strong senior/lead signal.
Common Mistake
Shipping both fully compiled ESM and CJS copies of stateful modules without guarding singletons.