Fixing Delete CR Prettier Error

October, 16th 2023 2 min read

The “Delete CR” error appears when your files use Windows-style line endings (CRLF) while Prettier expects Unix-style line endings (LF). This commonly happens in cross‑platform projects or when using tools like WSL, Docker, or remote editors.

Why the Error Happens

  • Windows uses CRLF
  • macOS/Linux use LF
  • Prettier prefers LF
  • If a file contains CRLF, ESLint shows: Delete 'CR' [prettier/prettier]

This mismatch leads to formatting conflicts that break CI, linting, or pre‑commit checks.


.prettierrc

json
{
  "endOfLine": "auto"
}

.eslintrc.js

js
module.exports = {
  rules: {
    "prettier/prettier": ["error", { endOfLine: "auto" }]
  }
};

This allows Prettier to accept LF/CRLF dynamically.


2. Convert Line Endings Using VS Code

  1. Open a file
  2. Bottom‑right corner → click CRLF
  3. Switch to LF
  4. Press Ctrl+Shift+P → Change All End of Line Sequence

3. Fix Line Endings Through Git

Add:

.gitattributes

plaintext
* text=auto eol=lf

Re‑normalize repository:

sh
git rm --cached -r .
git reset --hard

4. Convert Files in Terminal

Convert CRLF → LF

sh
dos2unix file.js

Convert LF → CRLF

sh
unix2dos file.js

Install via:

sh
sudo apt install dos2unix

5. Prettier 3.x Modern Config

For new Prettier versions:

prettier.config.js

js
export default {
  endOfLine: "auto"
};

6. WSL, Docker, and Remote Environments

VS Code (WSL)

json
"files.eol": "\n"

Git inside Docker

sh
git config core.autocrlf false

7. Use Pre‑commit Hooks to Enforce LF

Install:

sh
npm i -D husky lint-staged

Add:

json
"lint-staged": {
  "*": ["prettier --write"]
}

This ensures consistent formatting before each commit.


Conclusion

This ESLint/Prettier error is simply a line‑ending mismatch. By configuring Prettier, ESLint, Git, your editor, and using pre‑commit hooks, you can eliminate the issue permanently and avoid cross‑platform inconsistencies.