Skip to content

Instantly share code, notes, and snippets.

@gustavwennerblom
Last active November 23, 2022 23:43
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gustavwennerblom/b17a6b8c12a6e10f88306ad3e42f26bd to your computer and use it in GitHub Desktop.
Save gustavwennerblom/b17a6b8c12a6e10f88306ad3e42f26bd 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.

@farzd
Copy link

farzd commented May 19, 2021

works a treat !

@gambolputty
Copy link

eslint-config-prettier is preferred over eslint-plugin-prettier.

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