16

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

Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66
Nick
  • 529
  • 1
  • 4
  • 13

4 Answers4

26

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

Nick
  • 529
  • 1
  • 4
  • 13
  • Works - this should be the default – Aki Nov 21 '20 at 08:59
  • 2
    Weird. Only Visual Studio requires this. Committing via CLI or SourceTree does not require the prepending of `npx` for some reason. Also note that Visual Studio doesn't auto add any changes from the linter to the current set of staged changes, so add `git add .` to the end of your lint-staged command list – TetraDev Feb 17 '21 at 22:06
  • @TetraDev is correct. I got this error with GitHub Desktop. Everything worked as expected from the cmd line without adjusting the `package.json` – Porter Sep 01 '21 at 00:46
  • saved my monday. thanks! – JBoothUA Jan 03 '22 at 21:47
2

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

  • What a sad development in the developer community item. Have you found a decent workaround? This is driving me crazy every day :) – Borislav Ivanov Mar 10 '23 at 13:55
1

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 think you should ask this as its own question, you're probably not going to get much visibility on it here. however, i did find [this](https://github.com/yarnpkg/yarn/issues/1648) which seems related. I'm not currently using yarn, so I haven't experienced your issue firsthand – Nick Dec 20 '20 at 18:45
-3

From VS console, run:

npm install husky --save-dev

…and commit again.

Obsidian
  • 3,719
  • 8
  • 17
  • 30