<?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: SamBuilds</title>
    <description>The latest articles on DEV Community by SamBuilds (@majorreact).</description>
    <link>https://dev.to/majorreact</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%2F770121%2F583cb8c2-babe-40aa-9bbb-6638dd6c66b0.jpg</url>
      <title>DEV Community: SamBuilds</title>
      <link>https://dev.to/majorreact</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/majorreact"/>
    <language>en</language>
    <item>
      <title>How to setup prettier, lint-staged and husky script</title>
      <dc:creator>SamBuilds</dc:creator>
      <pubDate>Sun, 21 Aug 2022 23:17:02 +0000</pubDate>
      <link>https://dev.to/majorreact/how-to-setup-prettier-lint-staged-and-husky-script-4jhj</link>
      <guid>https://dev.to/majorreact/how-to-setup-prettier-lint-staged-and-husky-script-4jhj</guid>
      <description>&lt;p&gt;In this tutorial, I will teach you how to manually set up prettier formatter for your project. Prettier is a linter. &lt;/p&gt;

&lt;p&gt;So what is a linter? &lt;br&gt;
A Linter, simply put, is an automated tool for checking of your code for style errors. This could go a long way in helping track logical errors in your program.&lt;/p&gt;

&lt;p&gt;Prettier formatter, which is a linter, as we earlier stated, enforces a consistent format or style when writing code. Hence, being on a team of developers, code remains consistent, and much less conflicting. &lt;/p&gt;

&lt;p&gt;To get started, first, we create our react project by opening our command terminal. If you are on windows, to open the command terminal, go to search bar and type "cmd". You should see the command prompt option. Open it. Now, let us set up a new react project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app test-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create react application for you. Once it is done, then you move into the newly created react app we named "test-app" by typing in the command line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd test-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the current directory in your favorite code editor. For this tutorial, we shall be making use of &lt;a href="https://code.visualstudio.com" rel="noopener noreferrer"&gt;Visual Studio Code Editor&lt;/a&gt; aka vs-code. Type in the command line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;code .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command opens up current project directory in vs-code editor.&lt;/p&gt;

&lt;p&gt;Write all your code in your react project. I have already pasted some for this tutorial for illustration purpose.&lt;/p&gt;

&lt;p&gt;Next, we are going to manually set up prettier script for our project.&lt;/p&gt;

&lt;p&gt;Open your command terminal in vs-code editor. Type in the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch .prettierrc.json .huskyrc.json .lintstagedrc.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates all three file to be used in this project.&lt;/p&gt;

&lt;p&gt;Add the following code to the respective files for configuration:&lt;/p&gt;

&lt;p&gt;Inside .prettierrc.json file, add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "tabWidth": 2,
  "useTabs": false,
  "printWidth": 80,
  "semi": true,
  "trailingComma": "all",
  "jsxSingleQuote": false,
  "singleQuote": false
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;NOTE: You can visit the documentation to choose your preferred configuration &lt;a href="https://prettier.io/docs/en/options.html" rel="noopener noreferrer"&gt;Prettier&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Inside .huskyrc.json file, add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "hooks": {
    "pre-commit": "npx lint-staged"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside .lintstagedrc.json file, add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "src/**/*.{json,css,scss,md}": ["prettier --write"],
  "src/**/*.{js,jsx,ts,tsx}": ["prettier --write", "eslint --fix"]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The folder structure should look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="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%2Fe9urckkgkhhtbat3j3fu.png" class="article-body-image-wrapper"&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%2Fe9urckkgkhhtbat3j3fu.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Install all dev-dependencies by typing in the command terminal:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
npm install --save-dev prettier husky lint-staged&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To run the scripts, &lt;/p&gt;

&lt;p&gt;First, type in the command terminal: &lt;br&gt;
&lt;code&gt;&lt;br&gt;
git init&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
git add .&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
npx lint-staged&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
This expected output should look similar to this: &lt;/p&gt;

&lt;p&gt;&lt;a href="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%2Fcdhdwdaseihmnqxsrzl2.png" class="article-body-image-wrapper"&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%2Fcdhdwdaseihmnqxsrzl2.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations! You have successfully setup prettier, husky and lint-staged. You can now commit your code and push to github. &lt;/p&gt;

&lt;p&gt;If you liked my article, make sure to follow me for more contents on frontend web development. I will see you in my next article ❤️&lt;/p&gt;

</description>
      <category>react</category>
      <category>prettier</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
