[Node.js] Run Lint Before git commit with husky + lint-staged

Tadashi Shigeoka ·  Tue, March 24, 2020

I’ll introduce sample code for running Lint during git pre-commit using husky + lint-staged.

npm

Background: Want to Prevent Forgetting to Run Lint

Sometimes I forget to run lint or prettier before git commit, but introducing husky and lint-staged can prevent this.

husky

husky is an npm package that allows you to define settings you want to add to git pre-commit or pre-push in package.json.

lint-staged

lint-staged is an npm package that runs linters on files in the git staging area.

husky, lint-staged Installation and Initial Setup

Running the following command will handle installation and initial setup of husky and lint-staged all at once.

npx mrm lint-staged

Sample code is published in the following GitHub Pull Request, so please take a look.

👉 Install the husky and lint-staged packages and setup them. · Pull Request #29 · codenote-net/expressjs-sandbox

That’s all from the Gemba.