Eslint is a linter which enforces developer to write good and consistent code all over Project. Prettier is a good formatter tool that automatically formats the source code.

Setup Eslint, Prettier with TypeScript and React

Eslint is a linter which enforces developer to write good and consistent code all over Project. Prettier is a good formatter tool that automatically formats the source code.

In this blog, I will teach you, how to set up eslint, prettier with TypeScript and React. Even if you are not using TypeScript or react you can still follow along.

Video tutorial

I have already created a video about it on my youtube channel. Check that out for more details.

If you like this video, please like share, and Subscribe to my channel.

For react, I will use Nextjs. Again the principles are the same. You can also use it with create-react-app.

Editor setup

You need to install eslint and prettier plugins for your editor. To learn more, visit these links. Eslint Prettier

Setup

1yarn create-next-app

Then put your app name. I am going to call it eslint-prettier-typescript-react

After that, it will set everything for you.

Now change the directory to the folder.

1cd eslint-prettier-typescript-react

TypeScript setup for Nextjs (optional)

Create a tsconfig.json file.

1touch tsconfig.json

Install typescript packages.

1yarn add --dev typescript @types/react

Then start the server.

1yarn dev

It will fill up the tsconfig.json file. Now convert all the javascript files to typescript files.

Setup Absolute import

Absolute Import vs Relative Import

Inside the tsconfig.json file. create a new property baseUrl and set the import point. I will create an src folder and put all source code inside that.

So add this extra code:

1{
2 "baseUrl": "src/"
3 "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "src/"],
4}

Setup eslint

Since Nextjs version 11.0.0, eslint is already configured out of the box. But that's not enough for me. I want more. So, I will customize that more.

  • Install eslint globally on your computer.
1yarn global add eslint
  • Inside your project initialize eslint.
1eslint --init
  • Choose 3.

  • Choose 1

  • Choose your framework if you are using it. In my case react

  • If you are using TypeScript then yes. I am using TypeScript.

  • Browser in my case.

  • Use a popular style guide.

  • I would like to use the Airbnb style guide. You can choose any style guide. But Airbnb is good and I recommend it.

  • I will use json for my config file.

  • It will ask you to install some packages to Install with npm. If you want to use npm then go ahead. But I will use yarn.

So For those who are using yarn like me, You can copy and paste package names and install them.

With typescript:
1yarn add --dev eslint-plugin-react @typescript-eslint/eslint-plugin@latest eslint-config-airbnb@latest eslint eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react-hooks @typescript-eslint/parser@latest
Without typescript:
1yarn add --dev eslint-plugin-react eslint-config-airbnb@latest eslint eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react-hooks

It will create an eslint config file .eslintrc. It will be a hidden file.

Prettier Setup

  • Let's install prettier.
1yarn add --dev eslint-config-prettier prettier
  • Create a prettier config file.
1touch .prettierrc.json

Now you can put your config in the .prettierrc.json file in json format. You can find the options from here

My basic config for prettier.

1{
2 "singleQuote": true,
3 "useTabs": true,
4 "tabWidth": 1,
5 "semi": false,
6 "jsxSingleQuote": true,
7 "arrowParens": "avoid"
8}

Now we are done with prettier. Let's set up eslint config.

ESlint config setup

Your .eslintrc file should look like this.

1{
2 "env": {
3 "browser": true,
4 "es2021": true
5 },
6 "extends": ["plugin:react/recommended", "airbnb"],
7 "parser": "@typescript-eslint/parser",
8 "parserOptions": {
9 "ecmaFeatures": {
10 "jsx": true
11 },
12 "ecmaVersion": 12,
13 "sourceType": "module"
14 },
15 "plugins": ["react", "@typescript-eslint"],
16 "rules": {}
17}

We need to extend the eslint config with prettier and nextjs. So add prettier and next/core-web-vitals to extends array.

1{
2 "extends": [
3 "next/core-web-vitals",
4 "plugin:react/recommended",
5 "airbnb",
6 "prettier"
7 ]
8}

Explanation:

  • Extends array takes configs. But configs need to be ordered. The last items will have more priority than previous configs. For example, if any rule gets conflict between airbnb and prettier, the Airbnb config rule will be overridden by prettier.

Run eslint from the command line

For nextjs(11.0.0 +):

1{
2 "scripts": {
3 "lint": "next lint"
4 }
5}

For every other case:

1{
2 "scripts": {
3 "lint": "eslint . --ext ts --ext tsx --ext js"
4 }
5}
Let's fix some eslint errors

To turn any rule off or on, add the rules to the rules array. You can find the docs from here. Please watch my video to understand it well.

  • allow jsx on other extensions.
1{
2 "rules": {
3 "react/jsx-filename-extension": [
4 1,
5 { "extensions": [".js", ".jsx", ".ts", ".tsx"] }
6 ]
7 }
8}
  • File extension on import statement.
1{
2 "rules": {
3 "import/extensions": [
4 "error",
5 "always",
6 {
7 "js": "never",
8 "jsx": "never",
9 "ts": "never",
10 "tsx": "never"
11 }
12 ]
13 }
14}
  • Import unresolved error for absolute import(if you are using).
1{
2 "settings": {
3 "import/resolver": {
4 "node": {
5 "extensions": [".js", ".jsx", ".ts", ".tsx"],
6 "moduleDirectory": ["node_modules", "src/"]
7 }
8 }
9 }
10}

You can find my Eslint config from here

So, that's it for today. I hope I have covered everything that you need to know about how to set up.

Shameless Plug

I have made an Xbox landing page clone with React and Styled components. I hope you will enjoy it. Please consider like this video and subscribe to my channel.

That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.

Contacts

Blogs you might want to read:

Videos might you might want to watch:

Previous Post13 reasons why you should use Nextjs
Next Post10 reasons why I love Material-UI