Husky 4.x+ not working with Visual Studio Git

I have a similar error which is caused by the same pre-commit hook, but it can't find yarn.js:

/c/Users/xxx/AppData/Roaming/npm/yarn: line 5: cygpath: command not found
internal/modules/cjs/loader.js:968
  throw err;
  ^

Error: Cannot find module 'C:\program files (x86)\microsoft visual studio\2019\enterprise\common7\ide\commonextensions\microsoft\teamfoundation\team explorer\Git\node_modules\yarn\bin\yarn.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)
    at Function.Module._load (internal/modules/cjs/loader.js:841:27)
    at Function.executeUserEntryPoint as runMain
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Unfortunately, this doesn't help:

  "husky": {
    "hooks": {
      "pre-commit": "npx lint-staged"
    }
  },

I can resolve the issue by removing of pre-commit hook, but I do want to avoid this.


I found a solution, albeit it not a full explanation. The easy work around is to modify your husky command like so:

...
"husky":{
    "hooks":{ "pre-commit": "npx lint-staged"}
},
...

Specifying the NPM command corrects the issue with pathing. I found the suggestion in this response to an issue from 2018 in the lint-staged github, source here.

Edit: I just wanted to draw attention to TetraDev's comment. They are correct, you must include git add . in the lint staged tasks after the any code altering tasks (linting, prettier, etc) in order for the changes made to be applied to your commit.