Back to Gatsby

Automatically run Prettier on commits

deprecated-packages/gatsby-recipes/recipes/prettier-git-hook.mdx

2.20.01.0 KB
Original Source

Automatically run Prettier on commits

Make sure all of your code is run through prettier when you commit it to git. We achieve this by configuring prettier to run on git hooks using husky and lint-staged.

This recipe:


Installs packages.

<NPMPackage name="husky" /> <NPMPackage name="prettier" /> <NPMPackage name="lint-staged" />

Implements git hooks for prettier.

<NPMPackageJson name="husky" value={{ "hooks": { "pre-commit": "lint-staged" } }} /> <NPMPackageJson name="lint-staged" value={{ "*.{js,md,mdx,json}": [ "prettier --write" ] }} />


Writes prettier config files.

<File path=".prettierrc" content={{ "semi": false, "singleQuote": true, "trailingComma": "none" }} /> <File path=".prettierignore" content={.cache public node_modules } />


Once you've installed this recipe, prettier, husky, and lint-staged will be installed! You can edit your .prettierrc if you'd like to change your prettier configuration.