In today's article, we will present a step-by-step guide to integrating Prettier with ESLint in VSCode, following the best practices that each tool presents in its documentation.
Prettier is an opinionated code formatter that supports many languages and integrates with most editors.
This tool enforces a consistent code style based on its own rules, which can be customized if necessary.
Make sure you have these tools installed on your computer before you follow the installation steps below:
And some VSCode extensions:
Inside a React project, install the following dependencies as dev dependencies:
$ npm install -D eslint prettier eslint-config-prettier
$ yarn add -D eslint prettier eslint-config-prettier
Now, run the ESLint init script to initialize the linter in your project with custom configurations.
$ npx eslint --init
$ yarn eslint --init
These are the options you need to select:
? How would you like to use ESLint?
> To check syntax, find problems, and enforce code style
? What type of modules does your project use?
? Which framework does your project use?
? Does your project use TypeScript?
> Optional, choose which best suits your project
? Where does your code run?
? How would you like to define a style for your project?
> Use a popular style guide
? Which style guide do you want to follow?
> Optional, choose which best suits your style
? What format do you want your config file to be in?
> JSON (if you want IntelliSense when configuring eslint file)
At this point, we are using the
eslint dependencies that we added before.
If you close and re-open your VSCode, you'll see some red squiggly lines in your editor, and that's why we also installed the
eslint-config-prettier dependency. So let's use it! It's super simple.
"prettier" to the
"extends" array in your
.eslintrc.* file. Make sure to put it last to override other configurations, like so:
Now you need to set up Prettier so you can choose if you want semicolons, single or double quotation marks, etc.
.prettierrc.json file at the root of your project and add some configurations to it. Feel free to customize it with your preferences.
You can search for more options here.
The last thing you need to do is to tell VSCode to choose Prettier as your default formatter so Prettier can take care of the styling for you.
Edit your configuration file by pressing
Cmd + Shift + P or
Ctrl + Shift + P and typing "Preferences: Open Settings (JSON)".
Now add the following lines:
And that's it. Now you have a linter to help you reduce bugs in your code, as well as a super customized styling tool that helps you and your team ensure you're following the pattern that was created in your project.
There is another plugin that you can use with ESLint to help you with the dependencies array that React Hooks have. This plugin will help control UI updates. It will read your code and point out if you're missing any dependencies.
To set it up, install the plugin in your project:
$ npm install -D eslint-plugin-react-hooks
$ yarn add -D eslint-plugin-react-hooks
And add the following before
"prettier" in the
"extends" array in your
João Vitor Zaniolo de Oliveira
João Vitor is a UI Engineer at Avenue Code. He is a proactive developer and a tech enthusiast who likes to teach and share knowledge with others. When he's not working, he likes running, watching movies, listening to music, and eating pizza.