Skip to content

Husky + lint-staged: reference setup

This is one of the four enforcement mechanisms you can implement in Workshop 7. Below is a working configuration. Copy and adapt.

Prereq: install Prettier

OrbitTasks does not ship Prettier yet (no prettier dep, no .prettierrc). The prettier --write step below needs both, so add them first:

bash
npm install --save-dev prettier
echo '{ "singleQuote": true, "semi": true }' > .prettierrc

Install

bash
npm install --save-dev husky lint-staged
npx husky init

husky init creates .husky/pre-commit with a default npm test invocation. Replace its contents with the version below.

.husky/pre-commit

sh
#!/usr/bin/env sh
npx lint-staged

In your root package.json

json
{
  "lint-staged": {
    "*.{ts,tsx}": [
      "eslint --fix",
      "prettier --write"
    ],
    "*.{json,md,yml}": [
      "prettier --write"
    ]
  }
}

Test it

  1. Stage a file with deliberately bad formatting.
  2. Run git commit.
  3. Because eslint --fix and prettier --write auto-fix the file, lint-staged re-stages the fixed version and the commit succeeds, with the formatted file. (A rule violation that can't be auto-fixed is what blocks a commit.)
  4. Show the diff: the staged file got cleaned up before it landed.

What this gives you

  • Formatting consistency across the team without arguing about it in PR review.
  • A failure mode developers can fix in 10 seconds (just rerun the commit), not in 10 minutes (waiting for CI).
  • A clear policy boundary: "Did the pre-commit pass?" replaces "Is this style okay?"

What this doesn't give you

  • It doesn't catch bypasses. A teammate with git commit --no-verify skips Husky entirely. CI is your backstop.
  • It doesn't catch issues in files the dev didn't stage. CI handles that too.

Treat Husky as the fast feedback loop, not the source of truth. CI is the source of truth.