できない.dev

Prettier の設定ファイル (.prettierrc) が読み込まれない

Prettier は .prettierrc 系を上位へ向かって探す。
ファイル名や monorepo の階層がずれていると無視され、コメント付きの .prettierrc.json はエラーで止まる。
prettier --find-config-path <file> で実際に解決される設定パスを確認するのが最短。

公開: 更新:

症状できない · exit 1
npx prettier --find-config-path src/index.ts
[error] Can not find configure file for "src/index.ts".
解決後できた · exit 0
npx prettier --find-config-path src/index.ts
.prettierrc.json

2026-09-03 に node:20(Node.js 20.20.2 / npm 10.8.2) で実際に打って取った出力。検証環境の詳細

要約

Prettier は設定を package.json の "prettier" キー、.prettierrc、.prettierrc.json、.prettierrc.{js,cjs,mjs}、prettier.config.{js,cjs,mjs} を順に探す。
命名や配置が外れると黙って既定値で整形され、tabWidth や singleQuote の指定が反映されない。
prettier --find-config-path で実際に検出されるパスを確認するのが原因切り分けの起点になる。

実行例

設定ファイルが無い状態では --find-config-path が Can not find configure file を返し、整形結果も既定のダブルクォートのままになる。
.prettierrc.json を置くと同じコマンドがそのパスを出力し、singleQuote が効いてシングルクォートへ変わる。

$ npx prettier --find-config-path src/index.ts
[error] Can not find configure file for "src/index.ts".
$ echo $?
1
$ npx prettier src/index.ts
export const greet = (name: string) => "hello " + name;
$ echo $?
0
$ npx prettier --find-config-path src/index.ts
.prettierrc.json
$ echo $?
0
$ npx prettier src/index.ts
export const greet = (name: string) => 'hello ' + name;
$ echo $?
0

— 2026-09-03 時点の出力

検証環境Node.js 20.20.2 / npm 10.8.2 / Python 3.11.2 / 2026-09-03 検証(ほか 1 件)
検証日
実行環境
node:20 Debian GNU/Linux 12 (bookworm)
バージョン
  • Node.js 20.20.2
  • npm 10.8.2
  • Python 3.11.2
  • Git 2.39.5

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

解決策

1. 検出パスを確認する

npx prettier --find-config-path src/index.ts

[error] Can not find configure file が出て終了コード 1 になる場合は 設定が見つかっていない。
期待した .prettierrc のパスが表示されるまで原因をたぐる。

2. ファイル名と書式を公式に合わせる

{
  "tabWidth": 2,
  "singleQuote": true,
  "trailingComma": "all"
}

コメントを書きたければ .prettierrc.cjs にする。

module.exports = {
  tabWidth: 2,
  singleQuote: true,
};

3. ワークスペースの Prettier を指す

.vscode/settings.json:

{
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "prettier.prettierPath": "./node_modules/prettier"
}

node_modules の Prettier を強制すれば CLI と同じバージョンで動く。

よくある原因

  1. ファイル名を誤っている。
    prettierrc.json(先頭ドット無し)や prettier.json は対象外で、サポートされる名前は Configuration File(新しいタブで開く) に列挙されている。
  2. JSON の構文が誤っている。
    .prettierrc.json は JSON5 / JSONC ではないので、コメントや末尾カンマを書くと Invalid configuration のエラーになり、終了コード 2 で止まる。
  3. monorepo で設定を置く階層がずれている。
    Prettier は整形対象ファイルから上位に向かって探すので、packages/web/src/foo.ts を整形するなら packages/web/ か repo root に .prettierrc が要る。
  4. エディタ拡張の Prettier とバージョンが違う。
    VS Code の esbenp.prettier-vscode がバンドルする Prettier と node_modules の Prettier の差で、挙動が変わる。