Lint-staged: 'lint-staged' is not recognized as an internal or external command, operable program or batch file. (Windows)

Created on 30 Mar 2018  Â·  18Comments  Â·  Source: okonet/lint-staged

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

  1. Insure you are on a Windows machine with git installed.
  2. Create a project that uses lint-staged
  3. Make a change to your repo.
  4. Using Visual Studio Code or simply cmd.exe, attempt to commit your changes.
  5. 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

Most helpful comment

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 recognized error.

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-staged in 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-staged globally - I thought that npm scripts would search the local node_modules directory when resolving commands. Anyway, it works fine now.

All 18 comments

Hey @Jikodis could you try running npm run precommit directly? Because that's the command which is failing in your pre-commit hook.

pre-commit hook contents

λ cat .\.git\hooks\pre-commit
#!/bin/sh
#husky 0.14.3

command_exists () {
  command -v "$1" >/dev/null 2>&1
}

has_hook_script () {
  [ -f package.json ] && cat package.json | grep -q "\"$1\"[[:space:]]*:"
}

cd "."

# Check if precommit script is defined, skip if not
has_hook_script precommit || exit 0

# Node standard installation
export PATH="$PATH:/c/Program Files/nodejs"

# Check that npm exists
command_exists npm || {
  echo >&2 "husky > can't find npm in PATH, skipping precommit script in package.json"
  exit 0
}

# Export Git hook params
export GIT_PARAMS="$*"

# Run npm script
echo "husky > npm run -s precommit (node `node -v`)"
echo

# The following command is the one which is failing for you.
npm run -s precommit || {
  echo
  echo "husky > pre-commit hook failed (add --no-verify to bypass)"
  exit 1
}



Re-installing both husky and lint-staged could help but I can't be sure.

@sudo-suhas I deleted node_modules and did an npm 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!

I also have this. I tried to do what @Jikodis did but it didn't help.
Any other ideas?

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.

@Enalmada same WSL to Windows bug

I also got this error. Can this issue be reopened please? There is a workaround for it but it's still a bug.

Doesn't work with Angular 7 for me.
Edit:
Had to install lint-staged as dev dependency manualy.

Is this bug specific to WSL? Deleting node_modules and then reinstalling doesn't seem to work. It still keeps occurring.

I'm having this issue as well, on WSL. Though my particular issue might just be a problem with Webstorm. I'm getting the error when I use Webstorm's commit GUI.

8:51 AM Commit failed with error
  0 files committed, 1 file failed to commit: docs: Add npm start startup step
  'lint-staged' is not recognized as an internal or external command,
  operable program or batch file.
  pre-commit:
  pre-commit: We've failed to pass the specified git pre-commit hooks as the `lint:staged`
  pre-commit: hook returned an exit code (1). If you're feeling adventurous you can
  pre-commit: skip the git pre-commit hooks by adding the following flags to your commit:
  pre-commit:
  pre-commit:   git commit -n (or --no-verify)
  pre-commit:
  pre-commit: This is ill-advised since the commit is broken.
  pre-commit:

When I commit directly from the command line, though, lint-staged runs fine:

 ↓ Running tasks for *.{js,jsx} [skipped]
   → No staged files match *.{js,jsx}
 ↓ Running tasks for *.json [skipped]
   → No staged files match *.json
 ↓ Running tasks for *.{ts,tsx} [skipped]
   → No staged files match *.{ts,tsx}
[master 6468a0f] docs: Add "npm start" step to startup steps
 1 file changed, 1 insertion(+)

Looks like it’s still an issue. I’m reopening it but I won’t be able to work on it.

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 recognized error.

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-staged in 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-staged globally - I thought that npm scripts would search the local node_modules directory when resolving commands. Anyway, it works fine now.

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_:

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

Doing git commit from 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:

  1. As previously suggested, populate _node_modules_ using Windows. Note that there are other tools (like Lerna) that will not work inside WSL any more if you do this.
  2. Put your source in the WSL file system and run also your IDE from inside WSL. I have not tried this myself, but I believe Visual Studio Code's remoting feature could do this.
  3. Configure husky like "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've noticed it's because even if I specify to use Node from WSL do commit from WebStorm, it uses Node.js installed in Windows instead of in WSL. I checked out, error says "node v10.16.0". It's a version from Windows. But there is node v8.16.2 in my WSL.

@nicogreenarry did you resolve this issue?

Thanks @oturpe for the recap - especially workaround no.3. I added npx to every command in husky config (except git related of course). So instead of

"husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "./src/**/*.{js,jsx}": [
      "prettier --write",
      "eslint",
      "git add"
    ]
  }

i did:

"husky": {
    "hooks": {
      "pre-commit": "npx lint-staged"
    }
  },
  "lint-staged": {
    "./src/**/*.{js,jsx}": [
      "npx prettier --write",
      "npx eslint",
      "git add"
    ]
  }

Worked like a charm!

@zorzysty That worked for us too. We upgraded from 8.2.1 to 9.x and 10.x and saw failures with our developers using Visual Studio. Explicitly defining npx worked.

I'm not sure what changed between these versions to cause this to break though.

I had issue using husky pre-commit hooks and from @oturpe suggestion I configured if as following:

  "scripts": {
    "lintStage": "node_modules/.bin/lint-staged"
  },
  "husky": {
    "hooks": {
      "pre-commit": "npm run lintStage",
    }
  },

and it started working fine. I use windows7 with node 10.15

Thanks @oturpe for the recap - especially workaround no.3. I added npx to every command in husky config (except git related of course). So instead of

"husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "./src/**/*.{js,jsx}": [
      "prettier --write",
      "eslint",
      "git add"
    ]
  }

i did:

"husky": {
    "hooks": {
      "pre-commit": "npx lint-staged"
    }
  },
  "lint-staged": {
    "./src/**/*.{js,jsx}": [
      "npx prettier --write",
      "npx eslint",
      "git add"
    ]
  }

Worked like a charm!

Thanks. Using npx lint-staged solved my problem in Atom

Thanks @oturpe for the recap - especially workaround no.3. I added npx to every command in husky config (except git related of course). So instead of

"husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "./src/**/*.{js,jsx}": [
      "prettier --write",
      "eslint",
      "git add"
    ]
  }

i did:

"husky": {
    "hooks": {
      "pre-commit": "npx lint-staged"
    }
  },
  "lint-staged": {
    "./src/**/*.{js,jsx}": [
      "npx prettier --write",
      "npx eslint",
      "git add"
    ]
  }

Worked like a charm!

thanks it works!!! after hoursss reading... in my case my package.json didn´t have the part of "lint-staged" i pasted the complete part that you show with npx and it worked!, thanks again!!!!

Was this page helpful?
0 / 5 - 0 ratings