How to use Husky and Lint-staged

Formatting Code

  1. Prettier
  2. ES Lint
  3. Husky
  4. Lint-staged

Install and Configure Husky Step by Step:

1. Install Husky as dev dependency:

Installation:
npm install husky -D

2. Add 'prepare' script to package.json and run it once:

Update package.json scripts:
npm pkg set scripts.prepare="husky install"
npm run prepare

3. Add a hook:

  • This will generate a .husky folder with husky.sh file.
  • And also a pre-commit file with "npx lint-staged" command
Generate a husky folder with a shell script:
npx husky add .husky/pre-commit "npx lint-staged"

Install Husky Automatically:

Just run husky-init:

  • husky-init is a one-time command to quickly initialize a project with husky.
  • 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.
  • npx husky-init && npm install