How to Resolve "Expected linebreaks to be 'LF' but found 'CRLF'" ESLint Error in JavaScript
The ESLint error Expected linebreaks to be 'LF' but found 'CRLF' (linebreak-style) is a common and frustrating issue that arises from differences in how operating systems handle line endings. It's a cross-platform compatibility problem that is easy to fix once you understand the cause.
This guide will explain why this error occurs and walk you through the most effective solutions, from configuring your code editor and Git to adjusting your ESLint rules to accommodate different environments.
The Core Problem: CRLF (Windows) vs. LF (macOS/Linux)
Line endings are invisible characters that signify the end of a line of text. Different operating systems use different standards:
- Windows: Uses a two-character sequence called Carriage Return (
\r) and Line Feed (\n), referred to as CRLF. - macOS and Linux: Use a single Line Feed (
\n) character, referred to as LF.
The linebreak-style ESLint rule enforces a consistent line ending style across your project. The error Expected linebreaks to be 'LF' but found 'CRLF' means that ESLint was configured to expect Unix-style line endings (LF), but it found Windows-style line endings (CRLF) in your files. This is extremely common when a project is developed on a Unix-based system and then cloned or opened on a Windows machine.
Solution 1 (Recommended): Configure Your Code Editor
The best and most direct solution is to configure your code editor to use a consistent line ending for the entire project. The modern standard is to use LF for all operating systems.
The solution (in VS Code)
- Change the setting for the current file: In the bottom-right corner of the VS Code status bar, you will see either
CRLForLF. Click on it and selectLF. - Set the default for all new files: Open your
settings.jsonfile (you can find it by opening the Command Palette withCtrl+Shift+Pand searching for "Open User Settings (JSON)") and add the following line:"files.eol": "\n"
This tells VS Code to use LF as the default end-of-line character for all new files, which is the best practice for cross-platform development.
Solution 2: Configure Git with core.autocrlf
Git has a configuration setting to automatically handle line endings, but if misconfigured, it can be the source of the problem. Forcing Git to check out files with LF endings is often the best solution.
Solution: run the following command in your terminal to configure Git to automatically convert CRLF to LF on commit and use LF in your working directory.
# Recommended for macOS/Linux
git config --global core.autocrlf input
# Recommended for Windows to prevent CRLF issues
git config --global core.autocrlf false
After changing this setting, you may need to "refresh" your repository to apply the new line endings. Warning: This will modify your files, so make sure you have committed or stashed all your changes first.
# Warning: This discards local changes!
git rm --cached -r .
git reset --hard HEAD
Solution 3: Configure the ESLint linebreak-style Rule
If you cannot enforce consistent line endings in your development environment, you can configure the ESLint rule to be more flexible.
Option A: Make the Rule OS-Dependent (for .eslintrc.js)
If your ESLint configuration is in a JavaScript file, you can make the rule adapt to the operating system it's running on.
// In .eslintrc.js
module.exports = {
rules: {
'linebreak-style': ['error', process.platform === 'win32' ? 'windows' : 'unix'],
},
};
Option B: Disable the Rule (Last Resort)
If your team works across multiple operating systems and managing line endings is proving too difficult, you can simply turn the rule off.
// In .eslintrc.js
module.exports = {
rules: {
'linebreak-style': 'off',
},
};
This is generally not recommended, as it's better to fix the underlying consistency issue, but it is a valid pragmatic solution.
Conclusion
The "Expected 'LF' but found 'CRLF'" error is a classic cross-platform development issue.
- The recommended best practice is to configure your code editor to use
LFline endings, as this is the modern standard for web and software development. - Configuring Git's
core.autocrlfsetting is another strong solution that helps enforce consistency across a team. - As a last resort, you can configure the
linebreak-stylerule in your.eslintrcfile to be more flexible or to disable it entirely.
By standardizing on LF line endings in your editor and version control, you can permanently solve this issue and ensure a smooth development experience for your entire team.