Sub topic 6
main, module and types: the older entry points
This chapter assumes you can read a package.json, run tsc with a tsconfig.json, and tell ESM import syntax from CommonJS require. You have already seen the library layout and the declaration files that tsc emits into dist. Here you will see how three older package.json fields tell tools where those files are. You will also see why a modern package still keeps them.
What the three fields do
Before the exports field existed, a package named its entry points with top-level fields. Each field answers a different question for a different reader.
| Field | Read by | Question it answers |
|---|---|---|
| `main` | Node.js, nearly every tool | Which JavaScript file runs when someone imports the package? |
| `module` | Bundlers such as webpack, Rollup and esbuild | Is there an ES module version of the same code? |
| `types` (or `typings`) | The TypeScript compiler | Where is the declaration file for the package's entry point? |
Node.js reads only main. It ignores module and types. The module field is a bundler convention and was never part of the Node specification. The types field is a TypeScript convention. A tool that does not recognize a field skips it without an error.
main
main is a path, relative to the package root, to the file that require("pkg") or import "pkg" loads. If main is missing, Node falls back to index.js in the package root. A compiled library does not keep its entry point in the root, so you set it explicitly:
{
"name": "@acme/slugify",
"version": "1.0.0",
"main": "./dist/index.js"
}Whether Node treats that file as ESM or CommonJS depends on the type field and the file extension, as the previous chapter described. main only says which file to load. It does not say what format the file is in.
module
Bundlers like to tree-shake, which means removing unused exports. That works best on ES modules. A package that ships CommonJS in main can add module to point bundlers at an ESM build of the same code:
{
"main": "./dist/index.cjs",
"module": "./dist/index.js"
}Node uses main. A bundler that knows the convention prefers module. TypeScript does not read module at all. A package that has only an ESM build and sets "type": "module" does not need the field, because main already points at ESM.
types and typings
When TypeScript resolves import { slugify } from "@acme/slugify", it looks for a declaration file, not for the JavaScript. The types field points at it:
{
"main": "./dist/index.js",
"types": "./dist/index.d.ts"
}typings is an older alias with the same meaning. Use types and ignore typings.
How TypeScript uses these fields
The moduleResolution: "node10" mode is the algorithm TypeScript used before Node added ESM support. It was called node before TypeScript 5.0, and node is still accepted as an alias. It ignores the exports field. This is the mode that makes the three fields matter. For a bare package import, it works through these steps in order:
- Open
node_modules/<pkg>/package.json. - If
typesortypingsexists, use that file. - Otherwise, if
mainexists, take that path, replace the JavaScript extension with.d.ts, and look for a declaration file next to it. - Otherwise, look for
index.d.tsin the package root. - If nothing matches, look for
@types/<pkg>innode_modules.
The fence above is only a placeholder in prose form. The real diagram follows.
Step 3 is why a package with only main sometimes works by accident. If main is ./dist/index.js and ./dist/index.d.ts exists, TypeScript finds the types without a types field. Do not rely on this. Add types explicitly so the intent is visible and tools that do not follow step 3 still work.
Why a modern package keeps them
The modes node16, nodenext and bundler do read exports, and when exports is present they use it instead of main and types. So you might expect to delete the legacy fields. There are three reasons not to.
- Old consumers. Projects that still use
moduleResolution: "node10"never look atexports. Withouttypes, they may fail to find your declarations. - Old tools. Older bundlers, linters and test runners read
mainand nothing else. - Missing exports map. Tools that do not understand
exportsneed some entry point to fall back on.
The fields do not conflict with exports. A tool that understands exports uses it and ignores the others. A tool that does not understand it uses the legacy fields. The ESM-only package.json from the Topic Spine shows this pairing:
{
"name": "@acme/slugify",
"version": "1.0.0",
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./package.json": "./package.json"
},
"files": ["dist"]
}The paths in main and types must match the paths in exports. If they drift apart, old and new consumers get different files, and the bug shows up only for some users.
Gotchas
Subpaths are invisible to node10
The exports map can expose @acme/slugify/locale. A node10 consumer ignores exports, so TypeScript looks for node_modules/@acme/slugify/locale as a literal path. No such file exists, because the real file is dist/locale/index.d.ts. Two fixes exist.
The first fix is the typesVersions field, which redirects type lookups for subpaths:
{
"typesVersions": {
"*": {
"locale": ["./dist/locale/index.d.ts"]
}
}
}The "*" key matches every TypeScript version. The key locale is the subpath, and the array holds the declaration file to use. typesVersions affects only TypeScript, and only in resolution modes that do not use exports. It does not change which JavaScript file runs.
The second fix is to put a small stub directory at the package root, with its own package.json whose main and types point into dist. This works, but it adds files to your published package and clutters the repository. Many authors decide that node10 support for subpaths is not worth either fix, and document that subpaths need a newer resolution mode. Choose based on who your consumers are.
main must match the module format
If your package has "type": "module" and main points to ./dist/index.js, Node treats that file as ESM. A CommonJS consumer calling require on it fails on older Node versions, and on recent versions works only under restrictions. If you ship CommonJS, give the file a .cjs extension, or leave type unset and keep .js.
types must point to a file that is published
If files lists only dist, then types must point inside dist. A types path that points at src/index.ts would send TypeScript to a file that is not in the package, and consumers would see "cannot find module" or silently fall back to any.
types in the exports map comes first
Inside an exports condition object, TypeScript takes the first matching key. Put types before default, or TypeScript may pick the JavaScript file and report no types. The legacy types field has no ordering problem. This is one reason the two systems are easy to confuse.
Checking what a consumer sees
You can ask TypeScript which file it chose. In a consumer project that installed your packed tarball, run:
npx tsc --noEmit --traceResolution | grep "@acme/slugify"The output lists each step, including whether TypeScript read types, derived a path from main, or consulted exports. To test the legacy path, set this in the consumer's tsconfig.json:
{
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "node10",
"strict": true,
"noEmit": true
},
"include": ["index.ts"]
}Then compile a file that imports your package. If it compiles with node10, your fallbacks work. The Are the Types Wrong tool, covered in a later chapter, reports the same check for several resolution modes at once.
Summary of the decision
For a new ESM-only library, set main and types to the built entry point and add exports. Leave out module. For a dual library, set main to the CommonJS file, module to the ESM file, and types to the declaration file that matches main. In both cases, check that each legacy path equals the corresponding exports path.
Exercise
A package has "type": "module", "main": "./dist/index.js", no "types" field, and no "exports" field. dist contains index.js and index.d.ts. A consumer uses moduleResolution "node10". Does TypeScript find the types, and why? Then the author adds "exports" with a "./locale" subpath. Why does a node10 consumer still fail to import "@acme/slugify/locale", and what field fixes it?
Show answer
TypeScript finds the types. With node10 it has no "types" field, so it takes the "main" path, swaps the extension for .d.ts, and finds dist/index.d.ts next to it. Adding "types" is still better because it states the intent. The "./locale" import fails under node10 because that mode ignores "exports" and looks for a literal "locale" path inside the package, which does not exist. The "typesVersions" field fixes it by mapping "locale" to ["./dist/locale/index.d.ts"].