できない.dev

Prettier で Delete ␍ の改行コードエラーが消せない

Delete ␍ は、CRLF のファイルを Prettier が既定の LF と比べると出る。
endOfLine を auto にし、.gitattributes で LF に統一すれば再発しない。

公開: 更新:

症状できない · exit 1
npx eslint src/index.js
…
  1:13  error  Delete `␍`  prettier/prettier
  2:13  error  Delete `␍`  prettier/prettier
  3:20  error  Delete `␍`  prettier/prettier
解決後できた · exit 0
npx eslint src/index.js

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

要約

「Delete ␍ がファイル全行に出る」「Prettier を実行すると全行が変更扱いになる」現象は、CRLF と LF の改行コードの差で起きる。
Prettier の endOfLine を "auto" にし、.gitattributes で LF に統一すれば再発しなくなる。

実行例

core.autocrlf true のまま取り出し直すと、作業ツリーが CRLF になった。
ESLint は全行に Delete ␍ を出し、prettier --check も警告で終わる。
.prettierrc に "endOfLine": "auto" を置くと、ESLint は通る。
.gitattributes で LF を指定して取り出し直すと、ファイル自体が LF に戻って警告も出なくなる。

$ git config core.autocrlf true
$ rm src/index.js
$ git checkout -- src/index.js
$ file src/index.js
src/index.js: ASCII text, with CRLF line terminators
$ npx eslint src/index.js
/tmp/tmp.jyaMyv0PuZ/src/index.js
  1:13  error  Delete `␍`  prettier/prettier
  2:13  error  Delete `␍`  prettier/prettier
  3:20  error  Delete `␍`  prettier/prettier
 
✖ 3 problems (3 errors, 0 warnings)
  3 errors and 0 warnings potentially fixable with the `--fix` option.
$ echo $?
1
$ npx prettier --check src/index.js
Checking formatting...
[warn] src/index.js
[warn] Code style issues found in the above file. Run Prettier with --write to fix.
$ echo $?
1
$ cat .prettierrc
{
  "endOfLine": "auto"
}
$ npx eslint src/index.js
$ echo $?
0
$ cat .gitattributes
* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf
$ git add .gitattributes
$ git add --renormalize .
$ git status --short
A  .gitattributes
$ rm src/index.js
$ git checkout -- src/index.js
$ file src/index.js
src/index.js: ASCII text
$ npx eslint src/index.js
$ echo $?
0

— 2026-10-01 時点の出力

検証環境Node.js 20.20.2 / npm 10.8.2 / Python 3.11.2 / 2026-10-01 検証(ほか 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. Prettier の設定を "auto" に

{
  "endOfLine": "auto"
}

"auto" はファイル先頭の改行コードを尊重するので、CRLF も LF も「現状維持」になる。
リポジトリ全体を LF で揃えたい場合は "lf" を指定し、次の .gitattributes と組み合わせる。
各値の意味は Prettier 公式ドキュメント(新しいタブで開く) に載っている。

2. .gitattributes で改行コードを正規化

リポジトリルートに以下を置く:

* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf

text=auto で Git が「テキストファイルらしいもの」を自動判定し、eol=lf で working tree に LF で展開する。
.bat / .cmd だけは Windows の制約で CRLF を強制する。

3. 既存ファイルを再正規化

.gitattributes を追加した直後は、コミット済みの既存ファイルの改行コードは変わらない。
以下で一括正規化する:

git add --renormalize .
git status   # 改行コードだけが変わったファイルが大量に表示される
git commit -m "Normalize line endings to LF"

4. エディタ側も合わせる

VS Code なら settings.json に:

{
  "files.eol": "\n"
}

ESLint と Prettier を併用している場合、ESLint 側の linebreak-style ルールも同じ向きに揃えないと、片方が CRLF を要求して堂々巡りになる。

よくある原因

  1. Windows で git config --global core.autocrlf true になっており、checkout 時に CRLF へ変換される
  2. Prettier の既定 endOfLine が lf のため、CRLF ファイルを「LF に直せ」と差分指示してくる
  3. .gitattributes がリポジトリに無いので OS ごとに改行コードが揺れる
  4. VS Code の「Files: Eol」設定が \r\n になっている