The Daily Life Of Darth Vader — by Paweł Kadysz (https://pwlk.pl)

Setting Up ESLint, Prettier, and Husky in a React 18 Project: A Comprehensive Guide — The update version from my second article

Leandro A. Siqueira
3 min readJul 18

--

As the React ecosystem evolves, it’s crucial to maintain code quality and consistency in your projects. Linting and code formatting tools like ESLint and Prettier help identify and fix code issues, while Husky enables you to enforce these standards throughout your development workflow. In this article, we will walk you through the step-by-step process of setting up ESLint, Prettier, and Husky in a React 18 project, ensuring that your codebase remains clean and error-free.

Prerequisites

Before we begin, make sure you have the following prerequisites installed on your development machine:

  1. Node.js and npm: Ensure you have Node.js (version 14 or higher) and npm (version 7 or higher) installed.
  2. React 18: Set up a new React 18 project using your preferred method, such as Create React App or any custom setup.

Step 1: Initializing the Project

Let’s start by creating a new React 18 project. Open your terminal and run the following commands:

npx create-react-app my-react-app
cd my-react-app

Replace my-react-app with the desired name for your project.

Step 2: Installing Required Packages

Next, we need to install the necessary packages for ESLint, Prettier, and Husky. Run the following command in your project root directory:

These packages include:

  • eslint: The core ESLint package.
  • prettier: The code formatter.
  • eslint-plugin-react: ESLint rules for React.
  • eslint-plugin-react-hooks: ESLint rules for React Hooks.
  • eslint-config-prettier: Disables ESLint rules that conflict with Prettier.
  • eslint-plugin-prettier: Integrates Prettier with ESLint.
  • husky: Enables Git hooks.

Step 3: Configuring ESLint

Create an .eslintrc.js file in the root directory of your project with the following configuration:

module.exports = {
parserOptions: {
ecmaVersion: 2021,
sourceType: "module",
ecmaFeatures: {
jsx: true,
},
},
env: {
browser: true,
es2021: true,
},
extends: ["eslint:recommended", "plugin:react/recommended", "plugin:prettier/recommended"],
plugins: ["react", "react-hooks"],
rules: {
// Define your custom ESLint rules here, if needed.
},
};

In this configuration, we enable JSX support and define environments for browser and ECMAScript 2021. We also extend the recommended ESLint and React configurations and integrate Prettier with ESLint using the plugin:prettier/recommended extension.

Step 4: Configuring Prettier

Create a .prettierrc file in the root directory of your project with your desired Prettier configuration:

{
"singleQuote": true,
"trailingComma": "es5",
"tabWidth": 2
}

In this example, we configure Prettier to use single quotes, add trailing commas in arrays and objects (except functions), and set the tab width to 2 spaces. Feel free to adjust these settings to match your preferred code style.

Step 5: Setting Up Husky Git Hooks

Husky allows you to set up Git hooks to run ESLint and Prettier before commits. This ensures that your code meets the defined standards before it is committed to the repository. Add the following configuration in your package.json:

jsonCopy code{
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.js": [
"eslint --fix",
"prettier --write"
]
}
}

This configuration tells Husky to run lint-staged before each commit, which in turn runs ESLint and Prettier on the staged files (the files about to be committed).

Step 6: Testing the Configuration

At this point, your ESLint, Prettier, and Husky setup is complete. Restart your development server if it was running, and ESLint and Prettier will now enforce code quality and formatting rules.

To see the tools in action, try introducing some code violations, such as unused variables or inconsistent formatting, in your project. When you attempt to commit changes, Husky will prevent the commit if any issues are detected, asking you to fix them first.

Conclusion

By following this comprehensive guide, you have successfully set up ESLint, Prettier, and Husky in your React 18 project. This powerful combination of tools will help you maintain a consistent and error-free codebase, leading to better collaboration, improved code quality, and more productive development workflows. Happy coding!

--

--

Leandro A. Siqueira

Well, I woke up this morning and I got myself a beer!

Recommended from Medium

Lists

See more recommendations