Skip to content

[Feat] Add Husky and lint-staged for Prettier pre-commit formatting #3

@ahammadabdullah

Description

@ahammadabdullah

Description

I've noticed a pull requests where files show multiple line changes even though nothing was actually modified. This usually happens when someone saves a file and their local Prettier setup reformats it differently. It clutters the diff and makes PRs harder to review.

To fix this, we should add Husky and lint-staged so that Prettier runs automatically on staged files before commits. That way, everyone’s code is formatted the same way and we avoid unnecessary changes showing up in reviews.


Proposed Implementation

  1. Add Husky and lint-staged as dev dependencies.

  2. Add a prepare script in package.json to enable Husky after install.

  3. Create a .husky/pre-commit file that runs:

    npx lint-staged
  4. Add the following lint-staged config to package.json:

    {
      "*.{js,jsx,ts,tsx,md,json,css}": "prettier --write"
    }
  5. (Optional) Include ESLint with eslint --fix if we want linting checks too.


Example Configuration

"scripts": {
  "prepare": "husky"
},
"devDependencies": {
  "husky": "^9.1.7",
  "lint-staged": "^16.2.3",
  "prettier": "^3.6.2"
},
"lint-staged": {
  "*.{js,jsx,ts,tsx,md,json,css}": "prettier --write"
}

And in .husky/pre-commit:

npx lint-staged

Expected Result

  • Prettier runs automatically before every commit.
  • No more random formatting diffs in pull requests.
  • Consistent and clean code style across the project.

Additional Notes

I'd like to take this issue and set up the configuration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions