Interview frequency: …
Short answer
It lets you define a package's official entry points, control access to internal files, and select different outputs for CJS and ESM.
Explanation
exports prevents consumers from importing a package's internal files and supports conditions that route imports based on the runtime environment or module system (Conditional Exports).
Code example
{ "exports": { ".": { "import": "./dist/index.mjs", "require": "./dist/index.cjs" } } }Common mistakes
- Forgetting to provide the types field or placing it in the wrong location in exports.
Follow-up questions
- How does Node.js handle circular dependencies between CJS and ESM?
- What do the ^ and ~ symbols mean in package.json, and how do they differ from an exact version?