How to Make ESLint Work with Prettier: Avoiding Conflicts

Leul Ayalew
4 min readApr 7, 2023

--

Photo by Markus Spiske on Unsplash

As a developer, you know the importance of maintaining clean and consistent code in your projects. One powerful tool for achieving this in JavaScript projects is ESLint, which helps you catch code quality issues and enforce coding standards. Another popular tool for code formatting is Prettier, which automatically formats your code to adhere to a consistent style.

However, when used together, ESLint and Prettier can sometimes conflict with each other, causing problems and inconsistencies in your code. In this blog post, we will discuss how you can make ESLint work with Prettier effectively, avoiding conflicts and problems.

Understanding the Conflicts between ESLint and Prettier

ESLint and Prettier have different approaches to code formatting. ESLint focuses on analyzing and enforcing coding rules, while Prettier is a code formatter that automatically formats your code based on a predefined set of rules. These different approaches can sometimes result in conflicts, such as:

  1. Overlapping rules: ESLint and Prettier may have overlapping rules, leading to inconsistent code formatting. For example, ESLint may require you to use single quotes for string literals, while Prettier may automatically convert them to double quotes.
  2. Differing opinions on code style: ESLint and Prettier may have different opinions on code style, leading to discrepancies in code formatting. For example, ESLint may enforce the use of semicolons at the end of statements, while Prettier may automatically remove them.
  3. Formatting options: ESLint and Prettier may have conflicting options for code formatting. For example, ESLint may allow trailing commas in function arguments, while Prettier may automatically remove them.

To make ESLint work with Prettier effectively, it’s important to understand these conflicts and take appropriate steps to resolve them.

Steps to Make ESLint Work with Prettier

  1. Install and Configure ESLint and Prettier: First, you need to install both ESLint and Prettier in your project. You can install them as development dependencies using npm or yarn. Once installed, you need to configure them in your project.

For ESLint, you can create an .eslintrc file in the root of your project and configure the rules according to your coding standards. For Prettier, you can create a .prettierrc file in the root of your project and configure the formatting options.

2. Use ESLint Plugin for Prettier: To avoid conflicts between ESLint and Prettier, you can use an ESLint plugin for Prettier, such as eslint-plugin-prettier. This plugin allows you to run Prettier as an ESLint rule, which ensures that your code is formatted according to Prettier's rules while still adhering to ESLint's rules.

You can install eslint-plugin-prettier as a development dependency and then add it to your ESLint configuration by extending the recommended configuration in your .eslintrc file:

{
"extends": [
"eslint:recommended",
"plugin:prettier/recommended"
],
"plugins": [
"prettier"
],
"rules": {
// Your ESLint rules
}
}

This way, ESLint will use Prettier to format your code, avoiding conflicts between the two tools.

3. Disable Overlapping Rules (cont’d): in ESLint by adding the following configuration to your .eslintrc file:

{
"rules": {
// Disable rules that overlap with Prettier
"quotes": "off",
"semi": "off",
// Your other ESLint rules
}
}

This will prevent ESLint from flagging these overlapping rules as conflicts with Prettier, allowing the two tools to work seamlessly together.

4. Set Formatting Options Consistently: Another potential conflict between ESLint and Prettier is the differing formatting options. For example, if ESLint allows trailing commas but Prettier removes them, it can cause inconsistencies in your code. To avoid this, you should set the formatting options consistently between ESLint and Prettier.

You can achieve this by aligning the formatting options in both ESLint and Prettier configurations. For example, if you want to disable trailing commas in both tools, you can configure them as follows:

In .eslintrc:

{
"rules": {
"comma-dangle": "off",
// Your other ESLint rules
}
}

In .prettierrc:

{
"trailingComma": "none",
// Your other Prettier options
}

By aligning the formatting options in both tools, you can ensure consistent code formatting across your project.

5. Use Editor Integrations: Many code editors and IDEs have integrations with both ESLint and Prettier, which can help you avoid conflicts and problems. These integrations can automatically format your code according to both ESLint and Prettier rules as you type, saving you from manually fixing formatting issues.

For example, popular editors like Visual Studio Code, Atom, and Sublime Text have extensions that integrate ESLint and Prettier. These extensions can highlight ESLint errors and automatically format your code based on Prettier rules, ensuring that your code is clean and consistent.

6. Regularly Review and Update Configurations: Finally, it’s important to regularly review and update your ESLint and Prettier configurations to ensure they are up-to-date with the latest best practices and coding standards. As coding standards evolve, conflicts between ESLint and Prettier may arise, so it’s important to keep your configurations in sync.

Regularly reviewing and updating your configurations can help you identify and resolve conflicts early on, ensuring smooth integration between ESLint and Prettier in your projects.

In conclusion, making ESLint work with Prettier effectively involves understanding the conflicts between the two tools and taking appropriate steps to resolve them. By installing and configuring ESLint and Prettier, using an ESLint plugin for Prettier, disabling overlapping rules, setting formatting options consistently, using editor integrations, and regularly reviewing and updating configurations, you can ensure that your code is clean, consistent, and error-free while avoiding conflicts and problems.

Happy coding!

--

--

Leul Ayalew
Leul Ayalew

Written by Leul Ayalew

I'm a Software Engineer, passionate about building beautiful and performant web and mobile applications.

No responses yet