← Debugging Guides

阅读中文版本

TypeScript TS6059: File is not under rootDir

Fix TS6059 by aligning rootDir with the actual source locations, or removing it and letting TypeScript infer the common root.

intermediatedebuggingtypescriptbuildmonorepo

Symptoms

  • error TS6059: File is not under rootDir
  • The error appears after adding a file outside src
  • Common when a test or config file sits above the source root

Possible causes

  • rootDir is set to src but include matches files outside it
  • A shared config imports a file from a sibling package
  • A test file lives in a directory above src
  • Project references include sources outside the declared root

Diagnosis steps

Read the file path in the error

Interpretation

It names the offending file, showing which directory lies outside rootDir.

Compare rootDir to the input file layout

Interpretation

rootDir must be an ancestor of every input file.

Decide whether to widen rootDir or move the file

Interpretation

Widening changes the output structure; moving keeps the intended layout.

Root cause

rootDir defines the base against which TypeScript computes output paths. If any input file lies outside that base, the computed output path would escape outDir, so the compiler refuses with TS6059. It is a structural constraint: all inputs must share the declared root.

Fix

Move the input under rootDir, or set rootDir to the common ancestor of all inputs. If tests or config genuinely live elsewhere, give them their own tsconfig with a suitable root or exclude them from the emit-bearing build. With project references, ensure each project declares a root that contains its own sources.

Verify the fix

Run tsc; it compiles without TS6059, and the emitted directory structure matches the intended rootDir layout.

Symptoms

error TS6059: File is not under rootDir appears after a file is added above or beside the source root. It is common when tests or shared config sit outside src.

Diagnosis

Read the offending path and compare to rootDir:

{ "compilerOptions": { "rootDir": "src", "outDir": "dist" } }

A file at config/load.ts cannot be emitted under this rootDir.

Fix

Align the root with the inputs:

{ "compilerOptions": { "rootDir": ".", "outDir": "dist" } }

Or move the file under src, or compile tests separately.

Verification

tsc completes with no TS6059, and the output tree mirrors the configured root, confirming every input shares the declared base.

When rootDir Is a Restriction, Not a Hint

It is tempting to delete rootDir whenever TS6059 appears, but doing so changes the emitted directory structure, which can break import paths and packaging. rootDir exists to make output paths predictable: it strips the common prefix so dist mirrors src. If a test or tool config genuinely belongs outside src, giving it a separate tsconfig with its own outDir keeps the main build clean while letting the test build compute its own root. Widening the root instead flattens the output, so choose deliberately rather than by making the error disappear.