Skip to content

Instantly share code, notes, and snippets.

@vegaasen
Last active September 28, 2021 06:31
Show Gist options
  • Save vegaasen/715c6752047936fcd20361aec3d8aa69 to your computer and use it in GitHub Desktop.
Save vegaasen/715c6752047936fcd20361aec3d8aa69 to your computer and use it in GitHub Desktop.
Prettier + Husky + git-commit-hook + package.json on a separate folder

Problem 💥

I can't get Prettier, husky, git commit hooks working for my project, as I have my package.json in a separate folder!!!111!! WHAT TO DO?!?!!!

Example set-up:

  • package.json in a separate folder (say .frontend/package.json)
  • prettier installed
  • husky installed
  • pretty-quick installed
  • and it doesn't work anyway...

Solution 🎉

So, what if I told you there is a magic solution for this!

WOW THAT'S SO CRAZY SICK! U FOR REALZ?!?!

Do the following:

  1. npm install -D prettier pretty-quick husky
  2. Add script to package.json:
  • "scripts": {..., "prepare": "cd ../ && husky install ./frontend/.husky", ...}
  1. Add husky hook to package.json:
    "husky": {
        "hooks": {
          "pre-commit": "pretty-quick --staged"
        }
    }
  1. Run npm install
  • a .husky-folder will be created alongside your package.json-folder
  • e.g ./frontend/.husky
  1. Add new file under the .husky-folder named pre-commit
  • i.e ./frontend/.husky/pre-commit
  1. Add the following content to enable pre-commits:
  #!/bin/sh
  . "$(dirname "$0")/_/husky.sh"
  # Notice the folder here, important
  cd ./frontend && npx lint-staged
  1. Enable execution on the pre-commit-file
  • chmod +x pre-commit
  1. Add file .lintstagedrc on same level as your package.json
  2. Add the following content to .lintstagedrc:
    { "*.{js,ts,tsx,scss,css}": ["eslint", "prettier --write"] }
  1. Done ✨

You can now perform a change somewhere in e.g a tsx-file, and your changes will be prettified 😁

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