Skip to content

Instantly share code, notes, and snippets.

@jbutko
Last active August 2, 2022 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbutko/956a30b250560596c9bbf1eb945b8d10 to your computer and use it in GitHub Desktop.
Save jbutko/956a30b250560596c9bbf1eb945b8d10 to your computer and use it in GitHub Desktop.
Husky and Lint-staged pre-commit hook to lint code with ESLint and format code with Prettier before commit
  1. install deps: yarn add -D husky lint-staged is-ci
  2. configure husky pre-commit hook:
npm set-script prepare "husky install"
npm run prepare
npx husky add .husky/pre-commit "npm run pre-commit"
  1. edit npm prepare script: "prepare": "is-ci || husky install"
  2. add pre-commit script to package.json scripts section: "pre-commit": "npx lint-staged"
  3. add lint-staged section to package.json:
  "lint-staged": {
    "*.{ts,tsx}": [
      "prettier --write",
      "eslint --fix",
      "git add"
    ],
    "*.{html,css,less,ejs}": [
      "prettier --write",
      "git add"
    ],
    "*.js": "eslint --cache --fix"
  }
  1. commit and be happy that you push clean and well formatted code!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment