husky icon indicating copy to clipboard operation
husky copied to clipboard

lint-staged command is not found

Open fayfang opened this issue 6 years ago • 13 comments

when I use package.json below, the output is 'lint-staged command is not found'

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

when I use the npm command like this, It work

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

How can I fix it up?

OS: windows terminal: git bash git version: 2.17

$ HUSKY_DEBUG=1 git commit
PS D:\Users\fangyy001\Desktop\project\veb-third-web\cmbwp> git commit
husky > pre-commit (node v12.13.0)
'lint-staged' 不是内部或外部命令,也不是可运行的程序

fayfang avatar Dec 04 '19 01:12 fayfang

Hi fayfang,

lint-staged is another package. Run npm install lint-staged --save-dev to install it.

Ryuno-Ki avatar Dec 06 '19 08:12 Ryuno-Ki

Hi fayfang,

lint-staged is another package. Run npm install lint-staged --save-dev to install it.

yeah, but I am confused that why the cmd in ./node_module/.bin can not be used when git commit. not only the lint-staged ,but also all the cmd in ./node_module/.bin can not be used.

fayfang avatar Dec 09 '19 01:12 fayfang

Can you show the error message? (In case you're using macOS or a GNU/Linux distribution, force an English message via LC_ALL=C ./node_modules/.bin/husky resp. with the git command).

Ryuno-Ki avatar Dec 18 '19 06:12 Ryuno-Ki

It may be worth testing with Husky v4.

As the reason why npm run lint works but not calling lint-staged directly, I suspect it's because npm run modifies PATH to include node_modules/.bin. On the other side, when hook commands are run PATH isn't modified.

typicode avatar Jan 07 '20 02:01 typicode

On the other side, when hook commands are run PATH isn't modified.

Is this an intentional change in behavior from v3? I'm running into the same issue.

barretron avatar Jan 10 '20 15:01 barretron

.husky/pre-commit: line 4: lint-staged: command not found I get now this error message with husky v5.0.4 and lint-staged v10.5.1.

Update: Adding yarn lint-staged to the pre-commit husky file fixed it.

Manubi avatar Dec 02 '20 10:12 Manubi

Update: Adding yarn lint-staged to the pre-commit husky file fixed it.

That's the right fix, for husky v5 and v4, locally installed commands need to be run via the package manager. So yarn lint-staged or npx --no-install lint-staged will work.

Alternatively, this will work too:

// .husky/pre-commit (v5) or .huskyrc (v4)
npm run pre-commit
// package.json
{
  "scripts": {
    "pre-commit": "lint-staged"
  }
}

typicode avatar Dec 03 '20 08:12 typicode

If the husky hook is not being called, try to install version 4: npm install husky@4 --save-dev

AbnerPS avatar Feb 13 '21 23:02 AbnerPS

To follow @typicode's message:

I suspect it's because npm run modifies PATH to include node_modules/.bin. On the other side, when hook commands are run PATH isn't modified.

If you change your .husky/pre-commit to include this path, it works with husky@latest:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

node_modules/.bin/lint-staged

scoobster17 avatar Sep 07 '21 15:09 scoobster17

Adding npx lint-staged to the pre-commit husky file fixed it for me.

michelmany avatar Oct 28 '22 13:10 michelmany

Adding npx lint-staged to the pre-commit husky file fixed it for me.

Also this worked for me.

officialkevinbrian avatar Jan 07 '23 09:01 officialkevinbrian

If you are also desperately trying to fix ".husky/pre-commit: line 4: lint-staged: command not found", add "npx lint-staged" to your pre-commit file. Read more about the solution here: https://storksnestblog.wordpress.com/2023/01/11/fixing-husky-lint-staged-command-not-found-error/

SercanSercan avatar Jan 11 '23 14:01 SercanSercan

Adding npx lint-staged to the pre-commit husky file fixed it for me.

I should've read your comment earlier :D

SercanSercan avatar Jan 11 '23 14:01 SercanSercan