Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save farzd/7baf21deb12f7ed3247b90b885396fe3 to your computer and use it in GitHub Desktop.
Save farzd/7baf21deb12f7ed3247b90b885396fe3 to your computer and use it in GitHub Desktop.
Adding prettier to a CRA project

Adding prettier to a CRA project

Previously, I've had some problem to reliaby configure code formatting with Prettier in VSCode, in the context of a create-react-app proejct. Today, it worked like a charm, and this is what I did.

Preconditions:

  • react-scripts: "3.1.1"
  • react: "^16.9.0"
  • react-dom: "^16.9.0"
  • Dirk Baeumer's ESLint VSCode extension installed and running

Steps:

First install dependencies - yarn add -D prettier eslint-plugin-prettier

Then, wire up Prettier into the CRA toolchain by creating a file .eslintrc.json in the project root (upstream from the src directory) with the below content

{
    "extends": "react-app",
    "plugins": ["prettier"],
    "rules": {
      "prettier/prettier": "error"
    }
  }

Finally, add a file .prettierrc.json in the project root where you can override some of prettiers default formatting rules. The below contents works for me:

{
    "singleQuote": true,
    "trailingComma": "es5",
    "arrowParens": "always"
  }

Now restart VSCode to force a restart of the ESLint server, and all previously hidden problems should be visible under the Problems tab in VSCode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment