Husky npm package Pre-Commit hook not working in sub directory repository

Issue

I am trying to use Husky with Eslint. I’m trying to create a pre-commit (lint and husky) script that’ll prevent commits with added files if they have lint errors.
However, my project isn’t in a mono-repo format, but rather a custom format. In other words, my .husky folder and the package.json file are both in a sub-directory called “react-client” (seen below). This is problematic because husky always assumes that the .husky folder is in the root folder with the .git folder, but in this case it’s in the “react-client” folder.

enter image description here

Everything that I read on this matter led me to these steps for the husky docs (below), but that didn’t work either. https://typicode.github.io/husky/#/?id=custom-directory

I keep receiving this error message

enter image description here

I’m thinking that I need more than “cd react client” and “npm test” in my .husky pre-commit file. Or that I might need to make changes to my .git folder files. Also, I believe that my package.json scripts (below) could be incorrect/need more configurations

enter image description here

enter image description here

Solution

I wasn’t adding a file in the correct directory. I had to add a pre-commit file with no file extension under the .husky folder with the following contents:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
cd ./ClientApp && npx lint-staged 

IMPORTANT!! – MAKE SURE THAT YOU ADD THE PRE-COMMIT FILE UNDER THE .HUSKY FOLDER. For me, vscode was adding the pre-commit file to the "_" folder, (which is inside of the husky folder). Instead, Vscode should’ve just added the pre-commit file directly inside of the .husky folder.

Answered By – Chris Rose

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published