Hello folks. I hope you all are doing well.
In this post, we will talk about how you can setup Husky pre-commit hook with ESLint, Prettier and lint-staged to avoid bad commits and properly format code before committing. So let's get into it.
Husky lets us run commands or script before committing or pushing our code to git. There are a lot of other use cases of Husky too but we will only be using pre-commit hook for this article.
ESLint is a tool that can analyze our code and find errors in that code using ESLint rules. It can also fix some errors on its own.
Prettier is an opinionated code formatter that can format our code with the help of rules that you set or defaults are used.
lint-staged can run multiple linters against staged git files which in our case are ESLint and Pretttier.
Setup new React or React Native project.
For React use
npx create-react-app demo
For React Native use
npx react-native init demo
npm i -D husky lint-staged eslint-config-airbnb prettier
Here we are saving these modules as devDependencies to specify that these are only required in the development and not in runtime. I am using Airbnb's pre config file for ESLint. You can also go plain if you want to setup all ESLint rules by your self or you can also extend multiple pre configs like this.
Replace or create .eslintrc.js file with the following code. If you have created a React project then remove "eslintConfig" from the package.json file as we have created a separate configuration file for the ESLint.
It is advisable to extend "react-app" for React projects and "@react-native-community" for React Native projects. These files are automatically installed so you don't need to worry about them. Just append this to the start of the array in the extend option in the file.
Replace or create .prettierrc.js file with the following code.
In the latest versions of Husky we need to enable Git Hooks and then create and add the pre-commit hook. To do that run the following commands in the terminal.
npx husky install
npx husky add .husky/pre-commit "npx --no-install lint-staged"
Make sure to commit the auto-generated husky folder to your Git repo.
The above code will run lint-staged command against the staged files before committing. Make sure to run
npx husky install if you clone your Husky configured git repo.
So now let's add the lint-staged in our package.json file.
Open your package.json file and add the code that I have specified at the same level as dependencies.
The above code will run Prettier and ESLint rules against all js,jsx,ts,tsx staged files. You can also specify more file extensions or you can write a script for a specific folder you want.
So yeah that's it. Now when you will try to commit any changes, ESLint and Prettier rules will execute and it will format your code and give you errors if there are any and stop you from causing any bad commits.
Note: If you get dependency errors for
eslint-plugin-import while commiting, then also install them as devDependencies.
At BoTree Technologies, we build web and mobile applications to add value to our client’s business. We align ourselves to ensure that our client benefits the most out of our engagement.
Drop us a line to discuss how can we help take your business to the next level. Reach out to learn more about the software development companies in NYC for the various ways to improve or build the quality of projects and across your company.