Skip to content

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, module and types fields in package.json
  • src/index.ts re-exporting the public API
  • Build tools such as tsup, tsdown, unbuild or plain tsc, and a .changeset/ folder

Where things usually live

package.json
Entry points, supported formats, peer dependencies, files to 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

  1. The exports map
  2. src/index.ts
  3. Tests for the function you care about

How a request moves through the code

  1. 1A consumer imports from the package name or a subpath.
  2. 2The runtime or bundler picks a file from the exports conditions (import, require, types).
  3. 3That file is the built output of src/index.ts or a subpath entry.
  4. 4Public 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 exports conditions match the build output?
  • Which peer dependencies does the library expect?

What trips people up

  • A wrong exports map 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 sideEffects field.