How to understand a TypeScript library (npm package) codebase
A library's real interface is what it exports. Start from the `exports` map in `package.json`, follow it to the public entry file, and you'll see what users can import; everything else is internal. Build config decides what is shipped.
How to tell it's TypeScript library (npm package)
exports,main,moduleandtypesfields inpackage.jsonsrc/index.tsre-exporting the public API- Build tools such as
tsup,tsdown,unbuildor plaintsc, and a.changeset/folder
Where things usually live
- package.json
- Entry points, supported formats, peer dependencies,
filesto publish. - src/index.ts
- Public API surface.
- src/internal/ or src/lib/
- Implementation details.
- tsup.config.ts or tsconfig.build.json
- Output formats (ESM and CommonJS), declarations, targets.
- test/ or *.test.ts
- Tests, often the best usage examples.
- .changeset/
- Pending release notes and version bumps.
Conventions, not rules. Real projects rename and move things, so confirm against the repository.
Where to start reading
- The
exportsmap src/index.ts- Tests for the function you care about
How a request moves through the code
- A consumer imports from the package name or a subpath.
- The runtime or bundler picks a file from the
exportsconditions (import,require,types). - That file is the built output of
src/index.tsor a subpath entry. - Public functions call internal modules.
Questions to ask about the repository
- What is the public API, and which exports are undocumented?
- Which formats are published, and do the
exportsconditions match the build output? - Which peer dependencies does the library expect?
What trips people up
- A wrong
exportsmap breaks consumers even when the code is fine; it's a common source of "works locally" bugs. - Side effects in module scope break tree-shaking; check the
sideEffectsfield.