Sub topic 6
main, module and types: the older entry points
Prerequisites: you should know basic TypeScript, npm, package.json, and ES module import and export syntax. You should also understand that a published package has two consumers: a runtime that executes JavaScript and a type checker that reads TypeScript declarations.
Before the exports field became widely supported, packages commonly used three top-level package.json fields:
| Field | Main purpose | Typical value |
|---|---|---|
| `main` | Runtime entry point, usually for Node.js | `./dist/index.cjs` |
| `module` | Bundler hint for an ES module build | `./dist/index.js` |
| `types` | TypeScript declaration entry point | `./dist/index.d.ts` |
These fields describe different things. main and module point to JavaScript files. types points to a declaration file, which contains types but no runtime implementation.
How a consumer uses the fields
Suppose your package contains this structure:
package.json
dist/
index.js
index.d.tsA package might use the following configuration:
{
"name": "@acme/slugify",
"version": "1.0.0",
"main": "./dist/index.js",
"module": "./dist/index.js",
"types": "./dist/index.d.ts"
}A Node.js runtime traditionally checks main when it loads the package. A bundler may prefer module because it signals that an ES module build is available. TypeScript checks types when it needs the package's declarations.
The module field is a convention rather than a universal Node.js rule. Some bundlers support it, but Node.js itself does not use it as its general package entry point. Because tools interpret these fields differently, the fields can become inconsistent.
What `types` must point to
The declaration file must describe the JavaScript entry point that consumers actually load. If main points to dist/index.js, then types should normally point to the matching dist/index.d.ts.
For example, if src/index.ts exports slugify, compilation can produce:
export declare function slugify(input: string): string;A consumer can then write:
import { slugify } from "@acme/slugify";
const result = slugify("Hello, World!");TypeScript reads index.d.ts to check the import. JavaScript loads the file selected by the runtime entry point. If the declaration file is missing or points to the wrong build, JavaScript may work while the consumer sees missing exports or incorrect types.
> callout note > Treat main, module, and types as a compatibility layer for older tools. Keep their paths valid, but use the exports field as the authoritative public interface when your supported tools understand it.
Limitations of the older fields
These fields do not precisely describe multiple environments. They cannot reliably express separate import and require targets, conditional type files, or allowed subpaths. They also do not hide arbitrary files inside the package. A consumer may be able to reach an internal file by constructing its path.
Modern packages usually add an exports map:
{
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
}When supported, exports takes precedence over the older entry points. The top-level fields still help older npm clients, bundlers, and type-checking tools. They should agree with the default path in exports; otherwise, different consumers can receive different APIs.
Exercise
Why should the file named by types correspond to the JavaScript package entry point?
Show answer
TypeScript uses that declaration file to describe the exports that the runtime entry point provides. If the files describe different APIs, consumers can receive type errors or false type safety.