Annotated Diagrams: How Node.js Finds Your Package's Files
When a user writes import { parse } from "tiny-parser", a fair amount happens before any of your code runs. Understanding that path is the best defence against "works in my project, fails in theirs" bug reports. The two diagrams below trace it, with numbered walkthroughs. The Node.js documentation on modules and packages is the authoritative description; this is a map of it for library authors.
Diagram 1: finding the package directory
Caption: a vertical stack of folders, deepest at the top: /app/src/features/, /app/src/, /app/, /. Beside each is a small node_modules box. An arrow climbs the stack, stopping at the first node_modules that contains tiny-parser.
- A bare specifier (a name, not a path) triggers a package lookup.
- Node.js checks
node_modules/tiny-parserin the importing file's directory. - If it is not there, it moves to the parent directory and checks again, repeating up to the filesystem root.
- The first match wins. This is why a nested copy of a dependency can shadow a hoisted one.
Package managers that use symlinked or content-addressed layouts change where files physically live, but the walk is the same from Node's point of view.
Diagram 2: from package directory to file
Caption: a flowchart starting at the box "package found". The first diamond asks "does package.json have exports?". The "yes" branch leads to a second diamond, "does the subpath match an entry?", then to a list of conditions evaluated top to bottom. The "no" branch leads to the legacy main field, then to index.js.
- Node reads the package's
package.json. - If
exportsexists, it is the only source of truth. A subpath not listed there cannot be imported, and attempting it raises an error saying the subpath is not exported. - Within a matching entry, conditions are checked in the order written. Node.js understands conditions such as
import,require,nodeanddefault; tools add others, such astypesfor TypeScript. - The first condition that applies selects the file.
- Without
exports, Node falls back tomain, and then toindex.js.
{
"name": "tiny-parser",
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
}
}
The ESM and CommonJS fork in the road
The file Node finally selects is treated as an ES module or CommonJS based on its extension and the nearest package.json type field: .mjs is always ESM, .cjs is always CommonJS, and .js follows type. A common bug is shipping CommonJS code in a .js file inside a package marked "type": "module". Draw this as a third small diagram, a fork with three labelled paths, and keep it next to your build config.
Checklist derived from the diagrams
- Every subpath you document is listed in
exports. - Conditions are ordered from most specific to
default. - File extensions match the module format of their contents.
- You have tested both
importandrequirefrom a separate project, not from inside the package directory.
For the hands-on version of this, see configuring the exports field and the dual-package tutorial.