Sub topic 2
Configuring the TypeScript Compiler for Node.js
This chapter assumes you know basic TypeScript syntax, including functions, types, and async/await. You also need Node.js, npm, and a terminal. The project will use Node.js ESM, which is Node's modern module system.
What `tsconfig.json` controls
TypeScript does not run directly in Node.js. The TypeScript compiler, called tsc, checks your source files and produces JavaScript files that Node.js can run.
A tsconfig.json file is the compiler's configuration. It answers questions such as:
- Which JavaScript version should the compiler produce?
- Which module system should it use?
- Where are the source files?
- Where should compiled files go?
- How strict should type checking be?
Create this file in the project root:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"outDir": "dist",
"rootDir": "src",
"types": ["node"]
},
"include": ["src"]
}The compilerOptions object contains settings that affect compilation. The include property tells TypeScript which files belong to the project.
Choosing a Node.js module configuration
A module is a file whose code can be imported by another file. Node.js supports both older CommonJS modules and newer ESM modules. This tutorial uses ESM.
"module": "NodeNext" tells TypeScript to produce modules according to Node.js rules. "moduleResolution": "NodeNext" tells TypeScript how to locate imported files using those same rules.
These settings work with a package that declares ESM in package.json:
{
"type": "module"
}With this configuration, an import in a TypeScript file usually names the JavaScript extension that will exist after compilation:
import { countText } from "./count.js";The source file is count.ts, but the compiled file is count.js. Writing .js in the import lets both TypeScript and Node.js resolve the module correctly.
Controlling output and type checking
The "target": "ES2022" setting selects the JavaScript language level. Modern Node.js versions support this level, so the compiler does not need to transform every modern feature into older JavaScript.
The "rootDir": "src" setting identifies the source directory. The "outDir": "dist" setting places compiled JavaScript in a separate directory. For example, src/cli.ts becomes dist/cli.js.
"strict": true enables TypeScript's strict checking options. Strict checking catches problems such as possibly missing values and incorrect function arguments before the program runs. It may require more type annotations, but those annotations make command-line behavior easier to reason about.
The "types": ["node"] setting loads Node.js type definitions. These definitions describe APIs such as process, node:fs/promises, and node:util. Install them as a development dependency:
npm install --save-dev typescript @types/nodeCompile the project with:
npx tscIf compilation succeeds, TypeScript creates the dist directory. If it fails, read the first error carefully. Later errors can be consequences of the first one.
How source becomes runnable code
Common configuration errors
| Symptom | Likely cause | Check |
|---|---|---|
| Cannot find `node:fs/promises` | Node types are missing | Install `@types/node` |
| Output appears beside source files | `outDir` is missing | Set `"outDir": "dist"` |
| Import cannot be resolved | ESM settings or extension are wrong | Use `NodeNext` and `.js` imports |
| Source files are ignored | `include` points elsewhere | Confirm the files are under `src` |
A compiler configuration should describe the runtime you will actually use. Matching TypeScript's module settings to Node.js prevents errors that otherwise appear only after compilation.
Exercise
What directory contains the compiled JavaScript when rootDir is src and outDir is dist?
Show answer
A source file such as src/cli.ts compiles to dist/cli.js.