Nick
Nick

Reputation: 560

Husky 4.x+ not working with Visual Studio Git

Husky changed it's path handling with 4.0.0. After this change, it throws the following error on commit from Visual Studio:

husky > pre-commit (node v12.12.0)/c/path/to/repo/node_modules/.bin/lint-staged: 
line 5: cygpath: command not foundinternal/modules/cjs/loader.js:797 throw err;

^Error: Cannot find module 'C:\lint-staged\bin\lint-staged.js' 
 at Function.Module._resolveFilename (internal/modules/cjs/loader.js:794:15) 
 at Function.Module._load (internal/modules/cjs/loader.js:687:27)
 at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10) 
 at internal/main /run_main_module.js:17:11 { code: 'MODULE_NOT_FOUND', requireStack: []}

husky > pre-commit hook failed 
(add --no-verify to bypass)

However, when committing from CLI, it works as expected. Given that the error message has 'C:\lint-staged\bin\lint-staged.js' as the file path, I'm assuming that Visual Studio is handling the pathing differently.

I'm trying to find a way to make this work from within Visual Studio. I'm in an enterprise environment, so I'm hoping for a way I can include this configuration within the repo (rather than requiring manual local setup).

I have the husky config included within my package.json as

...
"husky":{
    "hooks":{ "pre-commit": "lint-staged"}
},
"lint-staged":{
    "!(*.min.*)js": "eslint --fix"
},
...

I'm currently using:
nvm 1.1.7 with Node 12.16.1
husky 4.2.5
lint-staged 10.1.3
visual studio 2019

Upvotes: 18

Views: 18155

Answers (5)

Master
Master

Reputation: 510

In Visual Studio 2022 I hit the same issue. The solution in my case was to execute in root project folder (where is .git folder) next commands:

npx husky init
npx husky install

Husky documentation

Upvotes: 0

Nick
Nick

Reputation: 560

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.

Update: since lint-staged v10, "git-add" should no longer be added to the lint-staged command. source

Upvotes: 27

Jorge Rozo
Jorge Rozo

Reputation: 1

From VS console, run:

npm install husky --save-dev

…and commit again.

Upvotes: -4

Michael Kriese
Michael Kriese

Reputation: 141

The issue with vs 2019 is, that the integrated git is missing the cygpath.exe file in C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\usr\bin

https://developercommunity.visualstudio.com/t/Missing-cygpathexe-in-git/1393876

Upvotes: 2

Denis Chernov
Denis Chernov

Reputation: 31

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.

Upvotes: 1

Related Questions