Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท tsconfig: Language, Modules and Emit Options

TypeScript rootDir and rootDirs options

Reference for rootDir and rootDirs: how rootDir shapes the output folder layout, error TS6059, and how rootDirs merges folders for relative imports.

Two similar names, two different jobs. rootDir says where your source tree starts, and it decides how the folders under outDir are laid out. rootDirs (plural) lists several folders that the compiler should pretend are merged into one, so relative imports can cross between them.

With rootDir: "src" and outDir: "dist", the file src/app/main.ts is emitted as dist/app/main.js. Without a fixed rootDir, the layout depends on which files happen to be included, and adding one file outside src can silently move all output one level deeper.

JSON
{
  "compilerOptions": {
    "rootDir": "./src",
    "outDir": "./dist",
    "rootDirs": ["./src", "./generated"]
  }
}

Try it

TypeScript How rootDir shapes the output path

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript A merged view with rootDirs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

JSON
{ "compilerOptions": { "rootDir": "<folder>", "rootDirs": ["<folder>", "..."] } }
Option Description
rootDir A folder. All input files that are emitted must be inside it, or: TS6059: File '...' is not under 'rootDir'. 'rootDir' is expected to contain all source files. Default: the longest common path of the input files in older versions; check your version, newer ones use the tsconfig.json folder.
rootDirs An array of folders treated as one virtual folder for relative module resolution. Default: not set.

Edge cases: rootDir does not select files (use include) and does not stop other files being compiled. rootDirs does not change the emitted layout; the files stay where they are.

Usage notes

  • Always set rootDir next to outDir. The output tree is then identical on every machine and in every CI job.
  • Tests outside src. If tests live beside src, either compile them with a separate config or place rootDir at the project root, and use include to control what is built.
  • Declaration files are not counted when the compiler computes the common root.
  • rootDirs use cases. Generated code in a second folder that imports relatively from source, or platform folders (ios, android) merged into one tree.
  • Runtime still disagrees. rootDirs only helps the type checker; at run time the files must really sit next to each other (for example after a copy or build step).
  • Project references. Each referenced project needs its own rootDir, otherwise declaration output ends up in the wrong place.