<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ankit Tiwari</title>
    <description>The latest articles on DEV Community by Ankit Tiwari (@ankitt8).</description>
    <link>https://dev.to/ankitt8</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F356918%2F1cf5c142-fb97-444f-851e-07d27454d3f0.jpg</url>
      <title>DEV Community: Ankit Tiwari</title>
      <link>https://dev.to/ankitt8</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ankitt8"/>
    <language>en</language>
    <item>
      <title>Setting up Eslint(airbnb configuration), Prettier and Husky pre-commit hooks in CRA </title>
      <dc:creator>Ankit Tiwari</dc:creator>
      <pubDate>Mon, 04 Oct 2021 11:51:38 +0000</pubDate>
      <link>https://dev.to/ankitt8/setting-up-eslint-airbnb-configuration-prettier-and-husky-pre-commit-hooks-in-cra-5dbo</link>
      <guid>https://dev.to/ankitt8/setting-up-eslint-airbnb-configuration-prettier-and-husky-pre-commit-hooks-in-cra-5dbo</guid>
      <description>&lt;p&gt;Talk is cheap, lets directly get into how to get setting up eslint, prettier and husky pre-commit hook DONE.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Install eslint as dev dependency
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;npm install eslint --save-dev&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;if using yarn &lt;code&gt;yarn add eslint --dev&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Setup eslint configuration
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;npx eslint --init&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;if using yarn &lt;code&gt;yarn run eslint --init&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;above will ask for few configurations I had selected for my purpose below configuration

&lt;ul&gt;
&lt;li&gt;eslint to check syntax and find problems, project react, project uses typescript yes, typeof module JavaScript, format of configuration .json.&lt;/li&gt;
&lt;li&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc3u17omxfjxsozjlnw1x.png" alt="ESLint configuration"&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Install airbnb style guide
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;npm install eslint-config-airbnb —-save-dev&lt;/code&gt; or &lt;code&gt;yarn add eslint-config-airbnb --dev&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;if copying above command doesn't work type the same&lt;/li&gt;
&lt;li&gt;&lt;code&gt;npx install-peerdeps --dev eslint-config-airbnb&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Edit &lt;code&gt;.eslintrc.json&lt;/code&gt; file created in &lt;em&gt;step 2&lt;/em&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Add inside &lt;code&gt;extends&lt;/code&gt; rule &lt;code&gt;"airbnb"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;my current extends rule inside .eslintrc.json&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;

    &lt;/span&gt;&lt;span class="nl"&gt;"extends"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"eslint:recommended"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"plugin:react/recommended"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"plugin:@typescript-eslint/recommended"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"airbnb"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;    


&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  5. Lets test if everything is working fine till now
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;create a &lt;code&gt;test.js (or .ts, .jsx, .tsx)&lt;/code&gt; file &lt;/li&gt;
&lt;li&gt;write some code which is against the airbnb style guide&lt;/li&gt;
&lt;li&gt;e.g 
```javascript
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;var a   = 4;&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; * then run command `./node_moduels/.bin/eslint test.js`
 * it should throw eslint erros with specific rules
 ![Linting Errors](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3tjkt8giisw9iy76sonf.png)
* If you are getting above linting errors implies your eslint set up is correct till now

## 6. Set up `prettier`
 * `npm install prettier --save-dev` or `yarn add prettier --dev`
 * since prettier and eslint have their own set of formatting rules to avoid conflicts lets install plugin
 * `npm install eslint-config-prettier --save-dev` or `yarn add eslint-config-prettier --dev`
 * I wanted prettier to be run as eslint rule to do that add another plugin
 * `npm install eslint-plugin-prettier --save-dev` or `yarn add eslint-plugin-prettier --dev`
 * now add above plugins in `extends` rule of `.eslintrc.json` i.e eslint configuration file
 * add to the existing rule of extends : `"plugin:prettier/recommended"`
* Currently `extends` is
```json


"extends": [
        "eslint:recommended",
        "plugin:react/recommended",
        "plugin:@typescript-eslint/recommended",
        "airbnb",
        "plugin:prettier/recommended"
    ],


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/prettier/eslint-config-prettier" rel="noopener noreferrer"&gt;eslint-config-prettier&lt;/a&gt;  &lt;a href="https://github.com/prettier/eslint-plugin-prettier" rel="noopener noreferrer"&gt;eslint-plugin-prettier&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  7. Now lets again run the test.js file using command
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;./node_modules/.bin/eslint test.js&lt;/code&gt; prettier errors should also be reported ideally
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmrqcv8o8c8dr69zxebrc.png" alt="Formatting errors are overridden by Prettier"&gt;

&lt;ul&gt;
&lt;li&gt;Observe the difference between this image and image at step 5&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  8. VS Code eslint extenstion to rescue
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is unrealistic to run this command on every file we will be coding manually , here comes the VS code &lt;a href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint" rel="noopener noreferrer"&gt;eslint extension&lt;/a&gt; to rescue, once restart after installing it, now as you code it will highlight the linting errors hurreeyy 
e.g
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe7w8bdfftgjrr87q0kms.png" alt="VSCode eslint extension"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  9. Now I wan't scripts (which will lint and format the changes done by me in a commit)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Lets first include the scripts then we will come to husky&lt;/li&gt;
&lt;li&gt;include below scripts inside &lt;code&gt;scripts&lt;/code&gt; in &lt;code&gt;package.json&lt;/code&gt;
```json
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;"scripts": {&lt;br&gt;
     "_lint": "eslint --config ./.eslintrc.json --ext js --ext jsx --ext tsx --ext ts",&lt;br&gt;
    "lint": "npm run _lint -- ./src/",&lt;br&gt;
    "lint:fix": "npm run _lint -- --fix ./src/",&lt;br&gt;
    "format": "prettier --write './*&lt;em&gt;/&lt;/em&gt;.{js,jsx,ts,tsx,css,scss,md,json}' --config ./.prettierrc",&lt;br&gt;
}&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* [reason why -- is added](https://stackoverflow.com/questions/40271230/how-to-run-eslint-fix-from-npm-script/58954298#comment117386806_58954298)

* you can create `.prettierrc` file in root location i.e `/`
* sample `.prettierrc` file
```json


{
    "printWidth": 80,
    "tabWidth": 2,
    "trailingComma": "none",
    "semi": true,
    "htmlWhitespaceSensitivity": "css",
    "quoteProps": "as-needed",
  "overrides": [
    {
      "files": "*.css",
      "options": {
        "parser": "css"
      }
    },
    {
      "files": "*.scss",
      "options": {
        "parser": "scss"
      }
    }
  ]
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://prettier.io/docs/en/options.html" rel="noopener noreferrer"&gt;prettier options&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  10. Now lets integrate linting to be run on all staged files included in a commit
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;here comes to rescue &lt;a href="https://github.com/okonet/lint-staged" rel="noopener noreferrer"&gt;lint-staged&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  11. I know its a lot, one more final step
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;as official docs say only command is enough to set up and here it is 
&lt;code&gt;npx mrm@2 lint-staged&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;this command will install and configure &lt;a href="https://github.com/typicode/husky" rel="noopener noreferrer"&gt;husky&lt;/a&gt; and &lt;a href="https://github.com/okonet/lint-staged" rel="noopener noreferrer"&gt;lint-staged&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;now add change lint-staged configuration in package.json
```json
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;"lint-staged": {&lt;br&gt;
    "src/*&lt;em&gt;/&lt;/em&gt;.{js,ts,jsx,tsx}": [&lt;br&gt;
      "eslint --config ./.eslintrc.json --ext js --ext ts --ext jsx --ext tsx"&lt;br&gt;
    ]&lt;br&gt;
  }&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* or if you want eslint to automatically fix linting errors possible by eslint, add below configuration(like it replaces var with const or let, it adds a new line at the end of file automatically, etc)
```json


"lint-staged": {
    "src/**/*.{js,ts,jsx,tsx}": [
      "eslint --config ./.eslintrc.json --ext js --ext ts --ext jsx --ext tsx --fix"
    ]
  }


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;what this does is what ever .js, .ts, .jsx, .tsx staged files are present in src directory in a commit are linted by eslint.
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6xy6w7292se47iq9g98v.png" alt="DEMO"&gt;
&lt;/li&gt;
&lt;li&gt;Note that I have not added &lt;code&gt;"prettier --write"&lt;/code&gt; in the array of commands to be run in pre-commit hook since eslint will do it for us due to plugin &lt;a href="https://github.com/prettier/eslint-plugin-prettier" rel="noopener noreferrer"&gt;eslint-plugin-prettier&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I personally don't like including --fix since I don't get to know mistakes done by me.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;as you can see prettier formatted the code and added in staging, and eslint showed errors hence commit was not successful&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;*&lt;em&gt;Ahh finally we are done, now you can start your development server &lt;code&gt;npm run start&lt;/code&gt; *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I am sure you got some errors if(there would have been any eslint erros in your code base src directory), &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;whats happening is react-scripts doesn't start the server if there are any eslint errors &lt;/li&gt;
&lt;li&gt;Tell me how to avoid it&lt;/li&gt;
&lt;li&gt;add &lt;code&gt;ESLINT_NO_DEV_ERRORS='true'&lt;/code&gt; just before &lt;code&gt;react-scripts start&lt;/code&gt; command&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now you are good to do development, hope everything goes well while doing deployment&lt;/p&gt;

&lt;p&gt;But its not so&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;when you do &lt;code&gt;npm run build&lt;/code&gt; again &lt;code&gt;react-scripts build&lt;/code&gt; doesn't build if there are any eslint errors&lt;/li&gt;
&lt;li&gt;add &lt;code&gt;DISABLE_ESLINT_PLUGIN='true'&lt;/code&gt; before &lt;code&gt;react-scripts build&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now yes nothing else is remaining, &lt;br&gt;
I always wanted to set up eslint, prettier husky pre-commit hook from the day I had seen this cool stuff, had ready various blogs docs, were not able to setup it properly, finally this weekend I did it, hurrreyyy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Better Code Quality, easier PR reviews across team.
&lt;/h2&gt;

</description>
      <category>codequality</category>
      <category>eslint</category>
      <category>prettier</category>
      <category>huskycommit</category>
    </item>
  </channel>
</rss>
