How To Add ESLint and Prettier to the Project

  1. Install eslint and install-peerdeps globally. If it throws any permission error, then prepend sudo to below command.

    npm install -g eslint install-peerdeps
  2. cd into project’s directory and run eslint --init. This will generate eslint config for project. (Choose airbnb style guide during configuration)

  3. Install eslint-config-auto package

    npm install eslint-config-auto --save-dev
  4. Replace extends key in .eslintrc file with below contents

        "extends": ["auto"]
  5. Prepend below lines in package.json’s scripts key.

    "eslint": "eslint --color --ext .html,.js,.json,.jsx,.md,.ts,.tsx *.* src",
    "eslint:fix": "npm run eslint -- --fix",
  6. Run npm run eslint command. This may throw error and one installation command will be displayed. Copy that command and run it in terminal. It will install the required packages. Once all those packages are installed, run npm run eslint again. It should now run linter on all those files.

  7. Install prettier packages

    npm install --save-dev prettier eslint-plugin-prettier prettier-init eslint-config-prettier`
  8. Run below command to generate prettier config for the project.

    # For linux and mac
    # For Windows
  9. Project is ready with EsLint and Prettier Config!

  10. If any package fails due to peer dependencies, try installing that package using install-peerdeps command. For example if eslint-config-prettier fails due to peer dependencies, run command install-peerdeps -D eslint-config-prettier eslint and prettier configeslint and prettier setupeslint prettier config vscodeeslint prettier config vscode.

  11. If Using VSCode, install ESLint plugin and Prettier – Code formatter plugin. These plugins will read eslint and prettier configs and help you format the code as you write.