Up and running Husky 🐶

Posted on:
January 14, 2025 | 07:50 AM
: 1 min read

Setting up and running husky

A quick guide on how to install, setup and run husky. What is husky ? It is a pre-commit git hook command that runs linting, test and building on your project upon committing and pushing. The following example is done using bun but it can be done using npm or any other javascript package manager.

  1. Install husky

    bun add --dev husky
  2. Init husky

    bunx husky init
  3. Update scripts in package.json

    {
    ...,
    "scripts":{
        ...,
        "lint-staged": "lint-staged"
        }
    }
  4. Add the following script code to package.json file

    "scripts": {
        ...,
    },
    "dependencies": {
        ...,
    },
    "devDependencies": {
        ...,
    },
    "lint-staged": {
        "**/*.{js,jsx,ts,tsx,json}": [
        "eslint --ext .js,.ts.,tsx,.astro src",
        "prettier --plugin-search-dir=. --write",
        "astro build"
        ]
    },
  5. Update the pre-commit file located in .husky folder

    bunx lint-staged

Once everything is setup, you can test it once you have git add and git commit. The following lint-staged script will check your files, prettify and build your project before readying your project for push.