When multiple developers work on a React project, ensuring consistency in coding styles, standards, and practices becomes crucial. Without a unified approach, the codebase can quickly become messy, making it harder to maintain, debug, and extend. By adopting tools that automate code formatting, enforce linting rules, and promote best practices, teams can streamline their development process and improve code quality.
These tools serve the purpose of maintaining the quality of your code whether you’re working individually or as part of a team
Setting up pre-commit hooks involves a few simple steps. Here’s a quick guide to get you started
To begin, navigate to your React project’s root directory and install the necessary packages as development dependencies
npm install --save-dev husky lint-staged prettier eslint eslint-plugin-react@latest
Create an ESLint configuration file in the project’s root directory. You can do this by running the following command. follow the prompts to set up ESLint based on your project’s requirements. This will generate an .eslintrc.js file with your chosen configuration.
npm init @eslint/config
You can create one more file to ignore the files that we don't want to apply linting such as the node_modules, JSON and etc, so we can create a file .eslintignore and mention below and you can add more if u want to ignore any more files
build
node_modules
public
.env.*
.eslintignore
.eslintrc.json
.gitignore
*.json
package.json
package-lock.json
Create a Prettier configuration file in the project’s root directory by creating a file called .prettierrc . Define your desired formatting rules in this file to ensure code consistency across the team.
{
"semi": true,
"tabWidth": 2,
"printWidth": 80,
"singleQuote": true,
"trailingComma": "none",
"jsxBracketSameLine": false
}
You can create one more file to ignore the files that we usually don't prettify such as the node_modules, JSON and etc, so we can create a file .prettierignore and mention below
npx husky install
this will help you to initialize and create a folder called .husky
Add prepare script to package.json, this script will trigger to enable Git hooks after installation. This step also depends on our npm version.
npx husky add .husky/pre-commit "npx lint-staged"
The installation of Husky and lint-staged will generate a pre-commit hook named pre-commit within the .husky folder. This hook is triggered automatically when executing the git commit command.
Running the linting process before committing your code is a sensible approach as it allows you to prevent any errors from being included in the repository and enforce a consistent code style. However, conducting linting on an entire project can be time-consuming, and the results may not always be relevant. The ideal scenario is to exclusively lint the files that are being committed, focusing on the relevant code changes.
create a new file named .lintstagedrc.json and add the following code within it
{
"**/*": [
"prettier --write --config ./.prettierrc --ignore-unknown",
"eslint --fix --quiet"
]
}
Now to test this you can run the following git commands
git init
git add .
git commit -m "add lint-staged & husky"
This will run the pre-commit scripts and will look something like this
Hope this blog helps you in setup up Eslint, Prettier, and Husky in your React application
Let's collaborate to turn your business challenges into AI-powered success stories.
Get Started