できない.dev

TypeScript「The 'rootDir' setting must be explicitly set」の原因と直し方

The 'rootDir' setting must be explicitly set は、tsconfig に "rootDir": "./src" を書けば直る。
TypeScript 6.0 は rootDir の既定が変わり、出力が dist/src/ と 1 段深くなる。

公開:

症状できない · exit 2
npx tsc -p .
tsconfig.json(3,5): error TS5011: The common source directory of 'tsconfig.json' is './src'. The 'rootDir' setting must be explicitly set to this or another path to adjust your output's file layout.
  Visit https://aka.ms/ts6 for migration information.
解決後できた · exit 0
npx tsc -p .

2026-11-07 に node:24(TypeScript 6.0.3 / Node.js 24.22.2) で実際に打って取った出力。検証環境の詳細

要約

error TS5011: The common source directory of 'tsconfig.json' is './src'. The 'rootDir' setting must be explicitly set to this or another path to adjust your output's file layout. は、rootDir を書いていない tsconfig.json を TypeScript 6.0 で変換すると出るエラーだ。
tsconfig.json に "rootDir": "./src" を書けば、5.x と同じ dist/index.js に出力される。

rootDir は、outDir の中にどの階層からディレクトリを写すかを決める設定である。
5.x は入力ファイルの共通の親(ここでは src)を使っていた。
6.0 は tsconfig.json のある場所を既定にしたので、何も書かないと出力が dist/src/index.js と 1 段深くなる。
TS5011 はこのずれを知らせるエラーで、終了コードは 2 でも出力は dist/src/ に書き出される。

実行例

node:24 のコンテナで、outDir だけを書いた tsconfig.json を TypeScript 6.0.3 の tsc にかけた。
TS5011 で止まっても dist/src/index.js は書き出され、"rootDir": "./src" を足すと dist/index.js に戻った。

$ npx tsc -v
Version 6.0.3
$ cat tsconfig.json
{
  "compilerOptions": {
    "outDir": "dist"
  },
  "include": ["src"]
}
$ npx tsc -p .
tsconfig.json(3,5): error TS5011: The common source directory of 'tsconfig.json' is './src'. The 'rootDir' setting must be explicitly set to this or another path to adjust your output's file layout.
  Visit https://aka.ms/ts6 for migration information.
$ echo $?
2
$ find dist -type f
dist/src/index.js
$ cat tsconfig.json
{
  "compilerOptions": {
    "outDir": "dist",
    "rootDir": "./src"
  },
  "include": ["src"]
}
$ rm -rf dist
$ npx tsc -p .
$ echo $?
0
$ find dist -type f
dist/index.js
$ cat tsconfig.json
{
  "compilerOptions": {
    "outDir": "dist",
    "rootDir": "./src"
  },
  "include": ["src", "test"]
}
$ npx tsc -p .
error TS6059: File '/tmp/tmp.0AJY2Hfrwm/test/site.test.ts' is not under 'rootDir' '/tmp/tmp.0AJY2Hfrwm/src'. 'rootDir' is expected to contain all source files.
  The file is in the program because:
    Matched by include pattern 'test' in 'tsconfig.json'
$ echo $?
2

— 2026-11-07 時点の出力

検証環境Node.js 24.22.2 / npm 11.19.1 / Python 3.11.2 / 2026-11-07 検証(ほか 2 件)
検証日
実行環境
node:24 Debian GNU/Linux 12 (bookworm)
バージョン
  • Node.js 24.22.2
  • npm 11.19.1
  • Python 3.11.2
  • Git 2.39.5
  • TypeScript 6.0.3

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. rootDir を書いて dist を作り直す

{
  "compilerOptions": {
    "outDir": "dist",
    "rootDir": "./src"
  },
  "include": ["src"]
}

6.0 で一度変換していると、dist/src/ に新しい出力が残っている。
dist を消してから変換し直す。

rm -rf dist
npx tsc -p .

前に作った dist/index.js が残ったままだと、package.json の main や node dist/index.js は古いファイルを動かし続ける。
CI で TS5011 を見落とすと、この食い違いに気づきにくい。
tsconfig.json を書き換えずに試すなら、npx tsc -p . --rootDir ./src でも通る。

2. 新しい配置にするなら rootDir を "." にする

出力を dist/src/index.js に置く配置で構わないなら、"rootDir": "." と書けばエラーは出ない。
このときは package.json の main や起動スクリプトのパスを dist/src/ に合わせて書き換える。

3. TS6059 はテストを include から外す

rootDir の外にあるテストは、ビルド用の tsconfig.json の include から外す。
テストの型も検査するなら、tsconfig.test.json のような別の設定を作り、そちらでは rootDir を書かずに noEmit にする。

よくある原因

  1. TypeScript を 6.0 に上げた。
    "outDir": "dist" と "include": ["src"] だけを書いた tsconfig.json は、5.x では何も言われずに dist/index.js を出していた。
  2. outDir か declarationDir を書いていて、入力ファイルが 1 つのディレクトリにまとまっている。
    noEmit で型だけを検査する tsconfig.json では出ない。
    include に src と test を並べるなど、共通の親が tsconfig.json の場所と同じになるときも出ない。
  3. rootDir を足したあと、src の外のファイルが入力に入っている。
    テストを include に入れていると、TS6059: File '.../test/site.test.ts' is not under 'rootDir' になる。