Sub topic 2
Configuring `tsconfig.json` for a Library
This chapter assumes you know TypeScript syntax, basic npm commands, and how import and export work. It also assumes you have used tsc, the TypeScript compiler, with a tsconfig.json file.
A library needs more than JavaScript output. Consumers also need declaration files so TypeScript can understand the library's public functions and types. The compiler configuration must therefore describe both the source layout and the files to generate.
Start with a predictable project layout
A small library can use this structure:
slugify/
├── src/
│ ├── index.ts
│ └── slugify.ts
├── package.json
└── tsconfig.jsonThe src directory contains code that you maintain. The compiler writes generated files to dist. Keeping generated files outside src prevents build output from being compiled again and makes the package contents easier to control.
Configure the compiler
Use this tsconfig.json:
{
"compilerOptions": {
"target": "ES2020",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"rootDir": "src",
"outDir": "dist",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"strict": true,
"skipLibCheck": true
},
"include": ["src"]
}Each setting has a specific job:
| Setting | Purpose |
|---|---|
| `target` | Chooses the JavaScript language level emitted by TypeScript |
| `module` | Determines how TypeScript emits `import` and `export` |
| `moduleResolution` | Determines how TypeScript finds imported files and package types |
| `rootDir` | Identifies the directory containing source files |
| `outDir` | Identifies the directory for generated files |
| `declaration` | Emits `.d.ts` files that describe the public types |
| `declarationMap` | Connects declarations back to their TypeScript source |
| `sourceMap` | Connects emitted JavaScript back to its TypeScript source |
| `strict` | Enables TypeScript's strongest type-checking rules |
| `skipLibCheck` | Skips checking declaration files from dependencies |
rootDir and outDir preserve the source structure. For example, src/index.ts becomes dist/index.js, while its declaration becomes dist/index.d.ts.
Choose Node-compatible module settings
NodeNext models modern Node.js behavior. It uses the package's "type" field and file extensions to decide whether a file is an ECMAScript module (ESM) or a CommonJS module.
For an ESM library, set "type": "module" in package.json. In TypeScript source, write relative imports with the .js extension:
// src/index.ts
export { slugify } from "./slugify.js";
export type { SlugOptions } from "./slugify.js";The source file is still .ts, but the .js extension tells Node which path will exist after compilation. Omitting it can cause errors under Node-style ESM resolution.
The module and moduleResolution settings should usually agree. NodeNext lets TypeScript check imports using rules close to the runtime's rules. This catches missing extensions and incompatible package entry points before publishing.
Compile and inspect the output
Add a build script to package.json:
{
"scripts": {
"build": "tsc"
}
}Run:
npm run buildThe compiler reports errors without writing incomplete output when type checking fails. After a successful build, inspect dist before packaging. Confirm that it contains the JavaScript entry point, declaration files, and any source maps you intend to publish.
> callout note > tsconfig.json controls compilation. It does not decide every file npm publishes or every import path consumers can use. Those package boundaries are configured separately. > ```
Exercise
Why should a library set both rootDir and outDir?
Show answer
They keep source files and generated files in separate, predictable directories while preserving the source directory structure in the build output.