The Surface Area Book: Deciding What Your Library Exposes
Everything a library exports becomes a promise. This free web read, in five short chapters, is about making fewer, better promises: choosing entry points, shaping functions so they can grow, and keeping internals genuinely internal. Read it in one sitting or one chapter at a time.
Chapter 1: The surface is bigger than you think
Most authors count only the functions in their main file. Users see more. In JavaScript, any file reachable by a deep import such as example-lib/dist/utils/parse.js is part of the surface unless an exports map blocks it. In Python, anything without a leading underscore looks public, and people import it. Error types, default option values, the shape of returned objects and even log message text can become things someone depends on. The first design task is to write down what you intend to support, because semver's rules only make sense once that list exists.
Chapter 2: Choose your doors
Give the package a small number of deliberate entry points and close the rest.
"exports": {
".": "./dist/index.js",
"./testing": "./dist/testing.js",
"./package.json": "./package.json"
}
With this map, example-lib and example-lib/testing are importable and nothing else is. In Python, the equivalent habit is an explicit __all__ in the package's __init__.py and a private _internal subpackage for everything else. In Go, the language helps: a directory named internal cannot be imported from outside the module tree that contains it.
Chapter 3: Shapes that can grow
A function with four positional parameters cannot gain a fifth without awkwardness, and reordering them is a breaking change. An options object or keyword arguments can gain new optional fields in a minor release. Prefer returning objects over tuples or arrays for the same reason: adding a field to an object rarely breaks callers, while adding an element to a tuple often does. In Go, the functional options pattern or a config struct serves the same purpose. The general rule: leave room on the right-hand side of every signature.
Chapter 4: Naming for the long term
- Name things after what they do for the caller, not after how they work today.
- Pick one verb per concept and use it everywhere: if it is
loadin one place, do not make itfetchin another. - Avoid boolean flags that change behaviour drastically; two clearly named functions are easier to document and to deprecate separately.
- Reserve short, attractive names for the operations most users need most often.
Chapter 5: Growing and shrinking the surface
Adding is cheap; removing is expensive. Before you export a new symbol, ask whether it could live in a secondary entry point marked experimental, so early users know it may change. When something must go, deprecate it first with a runtime warning and a documented replacement, keep it for at least one minor release cycle, then remove it in the next major. Tools that diff exported declarations between versions catch accidental removals that reviewers miss.
An illustrative case: hypothetical Package A exported a helper only because its test suite needed it. Two years later a downstream project relied on it, and removing it required a major version. Moving test helpers to a /testing entry point from the start would have made the promise explicit and limited.
Continue with the public API design course outline or the exports field how-to.