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 setup eslint, prettier with TypeScript and React. Even if you are not using TypeScript or react you can still follow along.
I have already created a video about it on my youtube channel. Check that out for more details.
If yout 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.
You need to install eslint and prettier plugins for your editor. To learn more, visit these links. Eslint Prettier
yarn 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 directory to the folder.
cd eslint-prettier-typescript-react
Create a tsconfig.json
file.
touch tsconfig.json
Install typescript packages.
yarn add --dev typescript @types/react
Then start the server.
yarn dev
It will fill up the tsconfig.json
file. Now convert all the javascript files to typescript files.
Absolute Import vs Relative Import
Inside tsconfig.json
file. create a new property baseUrl
and set the import point. I will create a src
folder and put all source code inside that.
So add this extra code:
{"baseUrl": "src/""include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "src/"],}
yarn global add eslint
eslint --init
react
yes
. I am using TypeScript.npm
. If you want
to use npm
then go ahead. But I will use yarn. So For those of who are using yarn like me, You can copy and paste package names and install them.
yarn 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
yarn add --dev eslint-plugin-react eslint-config-airbnb@latest eslint eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react-hooks
It will create a eslint config file .eslintrc
. It will be a hidden file.
yarn add --dev eslint-config-prettier prettier
touch .prettierrc.json
Now you can put your config in .prettierrc.json
file in json format. You can find the options from here
My basic config for prettier.
{"singleQuote": true,"useTabs": true,"tabWidth": 1,"semi": false,"jsxSingleQuote": true,"arrowParens": "avoid"}
Now we are done with prettier. Let's setup eslint config.
Your .eslintrc
file should look like this.
{"env": {"browser": true,"es2021": true},"extends": ["plugin:react/recommended","airbnb"],"parser": "@typescript-eslint/parser","parserOptions": {"ecmaFeatures": {"jsx": true},"ecmaVersion": 12,"sourceType": "module"},"plugins": ["react","@typescript-eslint"],"rules": {}}
We need to extend the eslint config with prettier. So add prettier
to extends
array.
{"extends": ["plugin:react/recommended","airbnb","prettier"],}
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.
{"rules": {"react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx", ".ts", ".tsx"] }]}}
{"rules": {"import/extensions": ["error","always",{"js": "never","jsx": "never","ts": "never","tsx": "never"}],}}
{"settings": {"import/resolver": {"node": {"extensions": [".js", ".jsx", ".ts", ".tsx"],"moduleDirectory": ["node_modules", "src/"]}}}}
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 setup.
If you want to reach out to me, You can follow me on any social media as @thatanjan. Until then stay safe and good bye.
My name is Anjan. I am a full stack web developer from Dhaka, Bangladesh.
I can create complex full stack web applications like social media application, blogging, e-commerce website and many more.
I love to solve problems and develop new ideas. I also enjoy sharing my knowledge to other people who are wiling to learn. That's why I write blog posts and run a youtube channel called Cules Coding
Email: anjancules@gmail.com
linkedin: @thatanjan
portofolio: anjan
Github: @thatanjan
Instagram (personal): @thatanjan
Instagram (youtube channel): @thatanjan
twitter: @thatanjan
Cules Coding will teach you full stack development. I will teach you not only the basic concepts but also the advanced concepts that other youtube channels don't cover. I will also teach you Data Structures and Algorithms with abstraction and without Math. You will also find many tutorials about developer tools and technologies. I also explain advanced concepts and technologies with simplicity.
Subscribe to Cules Coding so that my friend you don't miss any of these cool stuffs.