TypeScript Compiler Options
rootDir — TypeScript compiler option
Defines the source directory used to calculate relative output paths. It does not choose which files are included; use files/include and imports for that. Files requiring emission outside an explicit root cause a diagnostic. In non-composite projects it defaults to the common path of non-declaration input files; composite changes the default. Default: Common directory of non-declaration source inputs (non-composite); config directory for composite. Create the named files in a clean local project and run tsc -p tsconfig.json. Expected emitted files: dist/main.js, dist/model.js. Run node dist/main.js; expected stdout: '3'. Tested with TypeScript 5.8.3 and Node.js 22.16.0; local project example, not a single-file editor.
Formal Syntax & Structure
typescript syntaxtypescript
{ "compilerOptions": { "rootDir": "src" }}Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| rootDir | path string | Set in compilerOptions; Common directory of non-declaration source inputs (non-composite); config directory for composite. |
Working Example
Example Snippettypescript
// File: tsconfig.json (omit this label){ "compilerOptions": { "target": "ES2022", "module": "CommonJS", "strict": true, "types": [], "noEmitOnError": true, "outDir": "dist", "rootDir": "src" }, "files": [ "src/main.ts", "src/model.ts" ]}// File: src/main.tsimport { count } from "./model";console.log(count);// File: src/model.tsexport const count = 3;