Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Last active May 31, 2023 09:34
Show Gist options
  • Select an option

  • Save primaryobjects/9ab8d2346aa58ef6959ad357bb835963 to your computer and use it in GitHub Desktop.

Select an option

Save primaryobjects/9ab8d2346aa58ef6959ad357bb835963 to your computer and use it in GitHub Desktop.
How to setup prettier as a pre-commit hook for Git commits.

Setting up prettier as a pre-commit hook for Git commits.

  • Create your .git repository.

  • Edit package.json to add a scripts, pre-commit, and lint-staged section, as shown below.

    {
      "name": "example",
      "main": "app.js",
      "dependencies": {
        "express": "*",
      },
      "scripts": {
        "pre-commit": "lint-staged"
      },
      "pre-commit": [
        "pre-commit"
      ],
      "lint-staged": {
        "*.js": [
          "prettier --write",
          "git add"
        ]
      }
    }
  • Run the following commands:

    npm install
    npm install prettier lint-staged pre-commit --save-dev

References

@andyfaizan
Copy link
Copy Markdown

Thank you! This got it working for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment