Configuring Husky in a monorepo using Lerna is a practical approach for managing Git hooks and code quality checks across multiple packages [1]. Here are the steps to configure Husky in a Lerna monorepo: Prerequisites: * Ensure you have Lerna installed globally: ```bash npm install -g lerna ``` Steps: 1. Initialize Lerna Monorepo: * Create a new directory for your monorepo or navigate to your existing monorepo project. * Initialize a Lerna monorepo using the following command: ```bash lerna init ``` * This sets up the basic structure for your monorepo. 2. Install Husky and Lint-Staged: * In the root of your monorepo, install Husky and Lint-Staged as development dependencies: ```bash npm install husky lint-staged --save-dev ``` 3. Configure Git Hooks for all packages: * In the root of your monorepo, use Lerna to configure Git hooks for all packages. Run the following command: ```bash lerna add husky --dev --hoist ``` * This installs Husky as a development dependency for all packages in your monorepo and hoists them to the root. 4. Configure Husky Hooks for Each Package: * Inside each package in your monorepo, create a `.husky` directory if it doesn't exist: ```bash mkdir packages/<package-name>/.husky ``` * Within the `.husky` directory of each package, create a `pre-commit` file (or any other hook you want to configure) with the desired script. For example, you can create a `pre-commit` script that runs lint-staged: ```bash # packages/<package-name>/.husky/pre-commit #!/bin/sh . "$(dirname "$0")/_/husky.sh" npx lint-staged ``` * Make sure the script is executable: ```bash chmod +x packages/<package-name>/.husky/pre-commit ``` 5. Configure Lint-Staged: * In the root of your monorepo, create a `.lintstagedrc` file to configure lint-staged for all packages. Define the linting and formatting commands based on your project's needs: ```json { "packages/**/*.{js,ts}": [ "eslint --fix", "prettier --write" ] } ``` * Adjust the lint-staged configuration to match your project's linting and formatting requirements. 6. Set Up Pre-Commit Hooks: * In the root of your monorepo, create a `.husky` directory (if it doesn't exist) and add a `pre-commit` script: ```bash # .husky/pre-commit #!/bin/sh . "$(dirname "$0")/_/husky.sh" # Run pre-commit hooks for all packages lerna exec --parallel -- npx --no-install husky-run pre-commit ``` * Ensure the script is executable: ```bash chmod +x .husky/pre-commit ``` 7. Link Husky Hooks to the Main `.husky` Directory: * In the root of your monorepo, link the Husky hooks in each package to the main `.husky` directory: ```bash lerna exec -- npx --no-install husky install .husky ``` 8. Commit Your Changes: * Commit the configuration changes you've made: ```bash git add . git commit -m "Add Husky and lint-staged configuration" ``` Now, Husky is configured in your Lerna monorepo to enforce Git hooks and code quality checks for all packages. When you commit changes in any package, Husky will run lint-staged to ensure that code quality standards are met before allowing the commit to proceed. Here are some resources that provide more detailed instructions on configuring Husky in a monorepo: - [Linting, Formatting, and Type Checking Commits in an Nx Monorepo with Husky and lint-staged](https://www.thisdot.co/blog/linting-formatting-and-type-checking-commits-in-an-nx-monorepo-with-husky/) - [Enforce Git Hooks in Monorepos with Husky - But How?](https://dev.to/mimafogeus2/enforce-git-hooks-in-monorepos-with-husky-but-how-3fma) - [Smart monorepo · Issue 719 · typicode/husky](https://github.com/typicode/husky/issues/719)