Ch. 6 · Node.js

Node.js Package Exports and Public APIs

Node.js Package Exports and Public APIs. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readbeginnerupdated Oct 3, 2026

Package exports define supported entry points and can limit access to internal files. Consumers should depend on the public contract.

Before you start

You should know JavaScript promises, asynchronous errors and the distinction between a process and a request. When following a server example, identify the resource owner and the point where work completes. Try experiments locally with bounded input instead of assuming production traffic behaves like a single request.

The practical goal is to reason through this situation: A package exposes its root and a documented utility subpath. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: List supported entry points

Decide which root and subpath imports form the public API. Internal source layout should remain private.

Step 2: Declare the exports map

Point documented paths at the appropriate emitted files and account for supported module environments.

Step 3: Test consumption externally

Import from a separate consumer package so local relative imports do not accidentally bypass package rules.

Worked scenario

A package exposes its root and a documented utility subpath.

A library exports its root and a utilities subpath, while its implementation files remain unexported. Consumers importing a private build directory become coupled to packaging details. A release can reorganize those details without changing the intended API; external import fixtures expose whether the public paths still work.

Common mistake

Importing a private file path can break after a patch release.

Verify the behavior

Test every documented entry point and an intentionally private subpath under each claimed supported module environment.

Interview exercise

Design a stable library boundary.

Answer and reasoning

Export documented entry points and test their import behavior in supported ESM and CommonJS environments.

Continue learning

Compare the scenario with the Node.js interview questions and test your understanding with the Node.js MCQs. For terminology and implementation details, consult the reference material.

More in Node.js

read ✓Node.js · hard

Node.js Clustering Across CPU Cores

Use cluster to run several workers on all cores, restart crashed workers, and understand shared-port and shared-state limits.

~2 min readread →
read ✓Node.js · hard

Node.js Password Hashing with scrypt

Store passwords as salted hashes with a slow key-derivation function, and compare candidates in constant time.

~2 min readread →
esc