lint-staged: 'lint-staged' is not recognized as an internal or external command, operable program or batch file. (Windows)
Description
When using cmd.exe or Visual Studio Code in Windows I get the error “‘lint-staged’ is not recognized as an internal or external command, operable program or batch file.” when I attempt to commit my changes. Essentially the pre-commit fails because of this. This error does not occur in the Windows Subsystem for Linux command prompt.
Steps to reproduce
- Insure you are on a Windows machine with git installed.
- Create a project that uses lint-staged
- Make a change to your repo.
- Using Visual Studio Code or simply cmd.exe, attempt to commit your changes.
- You should see the error mentioned above in the console.
Debug Logs
expand to view
> git commit --quiet --allow-empty-message --file -
> git show :package.json
husky > npm run -s precommit (node v8.10.0)
'lint-staged' is not recognized as an internal or external command,
operable program or batch file.
husky > pre-commit hook failed (add --no-verify to bypass)
> git config --get-all user.name
> git config --get-all user.email
Environment
- OS: Windows 10
- Node.js: v8.10.0
lint-staged: v7.0.0
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 13
- Comments: 25
Commits related to this issue
- fix: Invoke commitlint through npx Visual studio somehow is unable to resolve the path for commitlint correctly without using npx. See https://github.com/okonet/lint-staged/issues/420#issuecomment-53... — committed to CrispyDrone by deleted user 4 years ago
- refactor: `npx lint-staged` Windows compatibility [1] 1: https://github.com/okonet/lint-staged/issues/420 — committed to conversionxl/lint-staged-config by lkraav 4 years ago
tl;dr: You might run into this problem on WSL if you’re using an IDE or other tool that is making your commit for you, and that tool uses a Windows version of node/npm.
I’m not sure if this solution will be relevant for some of the other folks in this thread, but I figured out what was going on for me. I noted in my previous comment that I’m using WSL, and that I’m able to commit from the command line, but that Webstorm gives the
'lint-staged' is not recognizederror.I realized it’s because my command line uses a version of node and npm installed on the Windows side of things, not installed in the WSL directories. I ran
npm install -g lint-stagedin my windows terminal (not in ConEmu, which I have set up to use Fish on WSL). Then I was able to commit normally from Webstorm.I’m still not completely sure why I needed to install
lint-stagedglobally - I thought that npm scripts would search the localnode_modulesdirectory when resolving commands. Anyway, it works fine now.Thanks @oturpe for the recap - especially workaround no.3. I added
npxto every command in husky config (exceptgitrelated of course). So instead ofi did:
Worked like a charm!
@sudo-suhas I deleted
node_modulesand did annpm install. I was then able to run the precommit command directly in cmd.exe and the precommit worked in VSCode when I committed through the interface. I believe this issue is solved. I recall this issue coming back up eventually, but lets hope it does not come up. If it keeps occurring, I will open a new bug with all the necessary information. Thanks!Greetings,
I do not think this issue is actually caused by lint-staged.
I ran into this problem, too. In my case, I have node_modules populated from inside WSL shell (using yarn, but I believe using npm would not be any different), but am commiting using Visual Studio Code running in Windows, and have husky set up like this in package.json:
Doing
git commitfrom WSL shell, everything works. Somehow husky knows how to find lint-staged from node_modules/.bin, which, since node_modules was populated from WSL shell, is a Unix symlink to …/lint-staged/bin/lint-staged, and runs it successfully.But Visual Studio Code Git tools (and PowerShell also) run in native Windows, and do not understand the symlink. The reason why populating node_modules again from Windows side works is that in that case node_modules/.bin/lint-staged is a different kind of file. Apparently that file understandable for both WSL shell and Windows (though I think success with Windows may depend on other factors too, like npm config script-shell setting).
Workarounds:
"pre-commit": "yarn lint-staged"(or npx or npm run if you prefer). I am not sure why this makes a difference, but it does - I guess the npm run finds the executable to run differently to what husky does. (Note that with yarn you need to use Node 11 at least, see yarn issue 6086 for details.I hope this helps.
@okonet I think you can close this issue as non-lint-staged related.
I just got the “‘lint-staged’ is not recognized as an internal or external command” after testing out building on WSL ubuntu. Deleting node_modules and doing npm install from windows side seems to have fixed everything.
I also have this. I tried to do what @Jikodis did but it didn’t help. Any other ideas?
Hey @Jikodis could you try running
npm run precommitdirectly? Because that’s the command which is failing in your pre-commit hook.pre-commit hook contents
Re-installing both
huskyandlint-stagedcould help but I can’t be sure.Is this bug specific to WSL? Deleting node_modules and then reinstalling doesn’t seem to work. It still keeps occurring.
messed around with this and strictly opening the IDE from within the path in a wsl terminal supresses this issue for me.
UPD/FYI. CI recognition issue fixed. Details: https://github.com/watson/ci-info/issues/108#issuecomment-1794448792
TL;DR; MAKE SURE YOU RESTART ALL INSTANCES of VS Code (especially if you use workspaces), and THEN HUSKY will “see” any executable from within NODEJS environment (local
./node_modules/.bin/or global or any custom NodeJS/npm executable)WHEN YOU re-install NodeJS packages (during git branches switch for example) where u have Husky v4 and then v6 or v7 or v8, ALSO MAKE SURE TO RESTART VS Code instances !!!
===
I can confirm that I also do have Windows + WSL (Ubuntu) setup and issue with
huskyandlint-stagedand here are my notes while I researched and fixed this issue.Yes, I do migrate Husky from v4 to v8.
lint-stagedas executable command from.husky/pre-commitdoesn’t work whatsoever, and it’s understandable, coz there is nonode_modules/.bin/there (in folder.husky) andlint-stagedis NOT installed global npm package (for me).npx lint-stagedALSO WORKS OK, and it’s KINDA better because doesn’t require additional npm adjacent script. But I have some bad feeling usingnpxwhen actuallylint-stagedas package is ALWAYS installed for my project. BUT IF YOU DO NOT INSTALLlint-stagedin your local codebase and you DO have Internet connection ALWAYS, thennpxapproach is 100% for you.npm run lint-stagedornpm run lintStagedor any custom NPM script name you create inpackage.jsonto targetlint-stagedWILL work, but…it will NOT work if your forget to clean up REMAINS after Husky v4 in
.git/hooks.So I did:
and RESTARTED VS Code. Then
npm run myCustomLintStageworks OK.Unfortunately such cleanup is NOT done neither by
npm install husky -DNOR bynpx husky installNOR bynpx husky-initYou can delete entire folder
.git/hooks, which will be REGENARETD after you do git commit/push activity. BUT ANYWAY I do NOT recommend to do so, coz you may have OTHER hooks your project needs, or another developer set it up and you don’t know. ANYWAY MAKE SURE YOU VERIFY ALL carefully before deletion. DO BACKUP ALWAYS !!!I do confirm also, that within
node@16.xandnpm@8.xyou don’t need to executenpm run preparemanually, because it after Husky v8 installed/configuredpreparelifecycle hook will be executed similar aspostinstall. I would personally removeprepare, but it seems to be reasonable to have, and I don’t want to break future Husky upgrades (if such happen)AFTER
npm install:AFTER
npm ci:More details:
PS. If you have codebase on real Windows and your Husky setup works, and you pushed code to GitHub repo, and then clone on WSL then GET ready to lose permission of file
.husky/pre-commitwhich should be EXECUTABLE.You will have such warning:
To fix that, if you continue using WSL ONLY do this:
chmod a+x .husky/pre-commitand then Husky setup will also work on WSL.