Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint-staged command is not found #618

Closed
fayfang opened this issue Dec 4, 2019 · 13 comments
Closed

lint-staged command is not found #618

fayfang opened this issue Dec 4, 2019 · 13 comments

Comments

@fayfang
Copy link

fayfang commented Dec 4, 2019

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' 不是内部或外部命令,也不是可运行的程序
@Ryuno-Ki
Copy link

Ryuno-Ki commented Dec 6, 2019

Hi fayfang,

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

@fayfang
Copy link
Author

fayfang commented Dec 9, 2019

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.

@Ryuno-Ki
Copy link

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).

@typicode
Copy link
Owner

typicode commented Jan 7, 2020

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.

@barretron
Copy link

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.

@Manubi
Copy link

Manubi commented Dec 2, 2020

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

@typicode
Copy link
Owner

typicode commented Dec 3, 2020

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"
  }
}

@AbnerPS
Copy link

AbnerPS commented Feb 13, 2021

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

garyli2 added a commit to garyli2/Nightstand that referenced this issue Mar 9, 2021
Electron v12 introduced a breaking change, so we need to set contextIsolation to false for our app to continue to work.
Also update husky to fix husky-run command not found, as seen in typicode/husky/issues/854, and lint-staged not found, in typicode/husky/issues/618 and prettier config change https://github.com/prettier/eslint-config-prettier/blob/main/CHANGELOG.md#version-800-2021-02-21
@scoobster17
Copy link

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

beatrizsmerino added a commit to beatrizsmerino/vue-todolist that referenced this issue Aug 16, 2022
- Run linters against staged git files and don't let 💩 slip into your code base!
Linting makes more sense when run before committing your code. By doing so you can ensure no errors go into the repository and enforce code style. But running a lint process on a whole project is slow, and linting results can be irrelevant. Ultimately you only want to lint files that will be committed.

Steps:
- Install the npm package `lint-staged` with the command:
`npm install lint-staged --save-dev`
- Add the configuration to the `.lintstagedrc` file. This will run `npm run lint` on the files that have been modified and are being prepared for upload.
- Change the `npm run lint` to `npx lint-staged` on the `pre-commit` file. Husky instead of directly running `npm run lint` will run `lint-staged` so you don't have to check all the files in the repository at once.

References:
- https://www.youtube.com/watch?v=jNxDNoYEGVU
- https://dev.to/truemark/run-eslint-on-git-commit-with-husky-and-lint-staged-in-reactjs-4oeb
- typicode/husky#618
- https://www.npmjs.com/package/lint-staged#Configuration
@michelmany
Copy link

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

@officialkevinbrian
Copy link

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

Also this worked for me.

@SercanSercan
Copy link

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
Copy link

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

I should've read your comment earlier :D

torkjels added a commit to brako-consulting/brako-no that referenced this issue Mar 14, 2023
beatrizsmerino added a commit to beatrizsmerino/vue-editor that referenced this issue Mar 20, 2024
Steps:
- Install the `lint-staged` package using the command:
`npm install lint-staged -D`
- Make a clean installation:
1. Delete the `node_modules` folder and the `package-lock.json` file.
2. Reinstall the packages with the command:
`npm install --ignore-scripts`

Reason:
- Sometimes, when saving a file, errors are not corrected automatically.
That is why I add the `lint-staged` package, to make sure there are no formatting errors before uploading the changes to the remote repository.

References:
- https://www.youtube.com/watch?v=jNxDNoYEGVU
- https://dev.to/truemark/run-eslint-on-git-commit-with-husky-and-lint-staged-in-reactjs-4oeb
- typicode/husky#618
- https://www.npmjs.com/package/lint-staged#Configuration
beatrizsmerino added a commit to beatrizsmerino/vue-editor that referenced this issue Mar 20, 2024
Description:
- Run linters against staged git files and don't let 💩 slip into your code base!
Linting makes more sense when run before committing your code. By doing so you can ensure no errors go into the repository and enforce code style. But running a lint process on a whole project is slow, and linting results can be irrelevant. Ultimately you only want to lint files that will be committed.

Steps:
- Run the following command to create the `.husky/pre-commit` file:
`npx husky add .husky/pre-commit "npx lint-staged"`
Husky instead of directly running `npm run lint` will run `lint-staged` so you don't have to check all the files in the repository at once.
- Create the `.lintstagedrc` file and add the configuration to run the `npm run lint` command when any file changes.
This will run `npm run lint` on files that have been modified and are being prepared for upload.
- Create the `.huskyrc` file and add the configuration to use `lint-staged` when `husky` runs the `pre-commit` hook.
With this setting the `lint` command is executed before committing changes.
So the project files are always formatted with `prettier`, `stylelint` and `eslint`.

References:
- https://www.youtube.com/watch?v=jNxDNoYEGVU
- https://dev.to/truemark/run-eslint-on-git-commit-with-husky-and-lint-staged-in-reactjs-4oeb
- typicode/husky#618
- https://www.npmjs.com/package/lint-staged#Configuration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants