Sub topic 13
Publishing a TypeScript Library for Node and Bundlers
This chapter assumes you can write TypeScript imports and exports, understand basic tsconfig.json settings, and know that Node.js supports both CommonJS and ES modules. The goal is to publish one library that works for:
- Node.js applications using ES module imports
- Node.js applications using CommonJS
require - Bundlers such as Vite, Rollup, and webpack
- TypeScript consumers who need accurate declarations
A library needs more than compiled JavaScript. Its package metadata must tell each consumer which JavaScript and declaration files to use.
Build both module formats
A dual-format package publishes two versions of its entry point:
- An ES module file, usually
.jsin a package with"type": "module" - A CommonJS file, usually
.cjs - An ES module declaration file, usually
.d.ts - A CommonJS declaration file, usually
.d.cts
For example:
dist/
index.js
index.cjs
index.d.ts
index.d.ctsThe source can use normal TypeScript exports:
export function greet(name: string): string {
return `Hello, ${name}`;
}A build process must produce both JavaScript formats. You can use two TypeScript configurations, a bundler, or a build tool designed for libraries. The important requirement is that the published files and the package map agree.
Define an exports map
The exports field is the public contract of the package. It selects a file based on the consumer’s module system.
{
"name": "example-lib",
"version": "1.0.0",
"type": "module",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
}
},
"files": ["dist"],
"types": "./dist/index.d.ts"
}The "import" branch is used by ES module consumers. The "require" branch is used by CommonJS consumers. Inside each branch, TypeScript-specific "types" comes before "default" so TypeScript can select the declaration file while a runtime selects the JavaScript file.
The top-level "types" field is useful for tools that do not understand conditional exports. The exports map remains the primary definition for modern tools.
Check the package format
Because the package declares "type": "module":
dist/index.jsis treated as an ES module.dist/index.cjsis treated as CommonJS.dist/index.d.tsdescribes an ES module.dist/index.d.ctsdescribes a CommonJS module.
The extension is part of the format decision. Do not name the CommonJS output index.js unless its surrounding package rules make it CommonJS.
The package should also expose only supported entry points. If consumers import example-lib/dist/internal, they bypass the public contract and make future changes harder.
How consumers resolve the package
A consumer using moduleResolution: "nodenext" follows Node.js conditions closely. A consumer using moduleResolution: "bundler" also reads package exports, but applies bundler-style rules to the surrounding source code. A correct exports map supports both modes without requiring consumers to know your source layout.
Test the published result
Test the packed package rather than only testing the repository. This catches missing files and incorrect paths.
npm run build
npm pack --dry-run
npx tsc --noEmit --module nodenext --moduleResolution nodenext
npx tsc --noEmit --module preserve --moduleResolution bundlerThe dry-run output should include every file named by exports. Test both an ESM import and a CommonJS require in small consumer projects. Also test declarations, because JavaScript may load successfully while TypeScript selects the wrong type file.
Exercise
Which files should the import and require branches select in a package with "type": "module"?
Show answer
The import branch should select the ES module JavaScript file and its .d.ts declarations. The require branch should select the .cjs CommonJS file and its .d.cts declarations.