YavarTechWorks
Published in

YavarTechWorks

ESLint and Prettier setup in typescript with a Husky Pre-commit Git hook

Installing ESLint

Install ESLint by using the following command in the project directory

npm i -D eslint

Configuring the ESLint

Initialize the ESLint by using the command

npx eslint --init

It will prompt the following question, You can select the option you want. I am selecting the following option

Then, it will ask you to install the required dependencies, Choose the option Yes

it will install the required dependencies and update the package.json. Also, it will create.eslintrc.json file.

Add the following scripts to run the lint in package.json file

"lint": "eslint ."

You can check the configuration by running the following commands

npm run lint

If you facing the following error,

Add node:true in env object in .eslintrc file like below

Installing Prettier

Install the prettier by the following command

npm i -D prettier

Configuring the Prettier

Adding a script to run prettier in package.json file. Add the following lines in the scripts

"prettier": "prettier --write ."

Add the .prettierignore file and include the following and any other files you need

You can check the prettier by running the following command

npm run prettier

Installing Husky and configuring

Install and initialize the Husky by using the below command

npx husky-init && npm install

It will setup husky, modify package.json and create a sample pre-commit hook that you can edit. By default, it will run npm test when you commit.

Now open the pre-commit file in the directory .husky/ add the following and remove the default

npm run lint
npm run prettier

Now commit your changes, it will run the lint and prettier

post-merge

Create the post-merge hook by running the below comment

npx husky add .husky/post-merge "npm install"

It will create the post-merge and update the file

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store