Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readDev Tool

Lint staged files with lefthook

Set up a pre-commit hook with lefthook that runs lint and format checks in parallel on the paths you staged.

You added a pre-commit hook that lints the whole project, and now every commit waits on files you never touched. You want the hook to check what is staged and run its checks side by side.

lefthook does that from one file at the repo root. It ships as an npm package, so the hook lives in your dev dependencies. I use Biome as the linter here, and any command that takes file paths works the same way.

Write the config

Add both tools with pnpm add -D lefthook @biomejs/biome, then create lefthook.yml.

pre-commit:
  parallel: true
  commands:
    lint:
      glob: ["*.{js,ts}", "**/*.{js,ts}"]
      run: pnpm exec biome lint {staged_files}
    format:
      glob: ["*.{js,ts}", "**/*.{js,ts}"]
      run: pnpm exec biome format {staged_files}

pre-commit is the Git hook name. parallel: true starts lint and format together instead of one after the other. The two glob patterns cover root-level and nested JavaScript and TypeScript files, and lefthook replaces {staged_files} with matching staged paths. Biome reads those paths from disk, so unstaged edits in a partially staged file can affect the check.

Install the hook and commit

Commit lefthook.yml so everyone gets the same commands. Register the Git hooks in each clone.

$ pnpm exec lefthook install
$ git commit -m "feat: add helpers"
┃  lint ❯
src/same.js:1:33 lint/suspicious/noDoubleEquals  FIXABLE
  × Using == may be unsafe if you are relying on type coercion.
Checked 2 files in 3ms. No fixes applied.
Found 1 error.
┃  format ❯
Checked 2 files in 3ms. No fixes applied.
summary: (done in 0.64 seconds)
✓ format (0.63 seconds)
✗ lint (0.61 seconds)

lefthook install prints a sync hooks line, and the commit then runs the hook. Biome flagged the == in src/same.js, the lint command exited non-zero, and Git created no commit.

The folder had a third file, later.js, with the same ==. I never staged it, so it never appears in the output, and both commands report two files.

Change == to === in src/same.js, stage it, and retry the commit. Both commands print a check mark and Git creates the commit.

Because the two commands run together, their output can arrive in either order. Across my runs lint came first in some and format in others.

A commit that stages notes.md alone shows lint (skip) no files for inspection and the same for format. The glob matched nothing, so lefthook started neither Biome process and the commit went through.

Git’s --no-verify flag skips the pre-commit and commit-msg hooks. Run the same checks in CI if every commit needs to pass them.