# The "exports" Field & ESM vs CJS

> Master package entry points, conditional resolution for ES Modules and CommonJS, and subpath encapsulation using package.json exports.

Subject: Package Managers | Lesson 3 of 24 | 4 min read
Interactive lesson: https://codewithanimation.in/package-managers/module-exports-and-entry-points
Previous: [package.json Anatomy & Fields](https://codewithanimation.in/package-managers/package-json-anatomy.md) | Next: [Installing Packages & The npm Registry](https://codewithanimation.in/package-managers/npm-install-and-registries.md)

## What is the exports field?

The exports field in package.json defines public subpaths and conditional entry points, taking precedence over main in supported Node versions. It restricts package-name imports to the exposed API; it is not a filesystem security sandbox.

## Why is exports critical for modern packages?

Modern Node.js applications use dual module systems (ES Modules via `import` and CommonJS via `require()`). The `exports` field allows a single package to serve optimized ESM builds (`.mjs`), CJS fallbacks (`.cjs`), and clean subpath shortcuts (`my-pkg/utils`) while preventing consumers from importing un-exported internal source files (`my-pkg/src/internal.js`).

## How to configure conditional exports

Declare `exports` in `package.json`. Use condition keys like `import`, `require`, `node`, and `default`. Map root import `.` to `{ "import": "./dist/index.mjs", "require": "./dist/index.cjs" }` and subpaths like `./utils` to `./dist/utils.js`.

## Common mistake: Dual Package Hazard and un-exported internal imports

Importing a package subpath that exports does not expose normally produces a package-subpath-not-exported error in Node. Declare the intended public subpath explicitly instead of relying on an internal file location.

## Tip: Provide fallback main and test export maps with publint

Always list `types` before `import`/`require` in conditional exports for TypeScript, specify `default` as the final fallback condition key, and provide a legacy `main` field so older bundlers do not break. Run `publint` to verify package export maps before publishing to npm.

## Try it: Simulate Module Resolution

Select an import statement or resolution condition below to trace how Node.js matches package.json exports and resolves target files on disk.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/package-managers/module-exports-and-entry-points

## Check yourself

### Question 1

A supported Node version sees both main and exports for a package-name import. Which takes precedence?

- A. main always wins
- B. scripts.start
- C. exports
- D. Whichever property appears first

Correct answer: C

exports defines the supported entry points in modern Node. main can remain as a fallback for older consumers.

- Why not A: Supported Node versions prioritize exports.
- Why not B: start is an npm task, not package-name resolution.
- Why not D: These fields have defined precedence, not source-order precedence.

### Question 2

An exports map has separate import and require targets. What do these conditions distinguish?

- A. Whether the package is public or private
- B. Which dependency version to download
- C. Whether npm should run tests
- D. How the consumer loads the package

Correct answer: D

Conditional exports can select different targets for import and require consumers. They do not select development versus production by themselves.

- Why not A: Publication visibility is a separate concern.
- Why not B: These conditions choose entry targets, not dependency versions.
- Why not C: Test scripts are not selected by these conditions.

### Question 3

A package exports only .. What normally happens to import "sample/internal.js"?

- A. Node rejects the unexported package subpath
- B. Node silently uses the root export instead
- C. npm publishes the internal file automatically
- D. The import becomes a global dependency

Correct answer: A

A package-name subpath must be exposed by exports. This defines a public API boundary, not a filesystem security sandbox.

- Why not B: An unavailable subpath is not silently redirected to the root.
- Why not C: Runtime resolution does not trigger publication.
- Why not D: Dependency scope does not change because resolution failed.

## Remember this

- The `exports` field takes precedence over `main` in modern Node.js runtimes.
- Conditional exports map `import` for ES Modules and `require` for CommonJS.
- Subpath exports provide package encapsulation, preventing consumers from importing internal private files.
- Always list `default` as the final condition key and use fallback `main` for legacy bundler compatibility.
