Pre-commit Hooks Setup (Using Husky)

Installation Steps

  1. Install Husky and lint-staged:
    npm install --save-dev husky lint-staged
    npx husky install
  2. Add prepare script to package.json:
    • Location: package.json (scripts section)
    {
      "scripts": {
        "prepare": "husky install"
      }
    }

Configuration Steps

  1. Create pre-commit hook:
    npx husky add .husky/pre-commit "npx lint-staged"
  2. Add lint-staged configuration to package.json:
    • Location: package.json (root level)
    {
      "lint-staged": {
        "*.{js,jsx,ts,tsx}": [
          "eslint --fix",
          "prettier --write"
        ],
        "*.{json,md}": [
          "prettier --write"
        ]
      }
    }
  3. Verify .husky directory is committed to version control
    • Check: .husky/pre-commit should be in repository

Did you find this article useful?