25 окт. 2023 г.

How to use ESLint + Prettier + VS Code together without conflicts

  1. Install eslint, prettier, eslint-plugin-prettier, eslint-config-prettier
  2. Update eslintrc file to look like this:

extends: [
  'eslint:recommended',
  'plugin:react/recommended', // if react is used
  'plugin:react-hooks/recommended', // if react is used
  'plugin:@typescript-eslint/recommended', // if ts
  'plugin:prettier/recommended'
],
plugins: ['prettier', 'react', 'react-hooks', '@typescript-eslint'],

With this setup ESLint starts to use Prettier and its rules for code style checks and autofixes.

Moreover, now you don't need two separate commands to run ESLint and Prettier, they both will run with a single command eslint --fix.

Additional steps for VS Code:

  1. Install the "Prettier - Code formatter" extension
  2. Go to Settings and set the "Editor: Default formatter" to "Prettier - Code formatter"
  3. Enable "Editor: Format On Save"