Cactusman07
Cactusman07

Reputation: 1001

Husky (v8) - multiple issues with setting up pre-commit (error: cannot spawn and no pre-commit file created)

On a new project, I run:

npm install --save-dev husky

Then:

npx husky init

Expected outcome (as per docs @ https://typicode.github.io/husky/get-started.html)

"The init command simplifies setting up husky in a project. It creates a pre-commit script in .husky/ and updates the prepare script in package.json."

What happens: Console logs the below lines, and no folder is created:

Usage:
  husky install [dir] (default: .husky)
  husky uninstall
  husky set|add <file> [cmd]

Next, as per output from above, I run:

npx husky install

This installs a .husky folder in my project root directory, which has a .gitignore file and a "husky.sh" file in it. There is no pre-commit file created.

Try again to run:

npx husky init 

Afterwards, but this does nothing other than logging the same help text to the console.

In .git/hooks folder, I cannot see a pre-commit file (only pre-commit.sample).

I still have this in my package.json file:

"lint-staged": {
  "src/**/*.{js,jsx,ts,tsx}": [
      "npm run tidy",
      "npm run test"
  ]
}

Along with this under my scripts:

"pre-commit": "lint-staged"

But I am wondering whether or not I actually still need these? There is no mention of them in the current Husky docs. There isn't actually a lot in the Husky docs...

I manually add a pre-commit file in the .husky directory, with the following command in it:

npm run lint

Which runs my eslint script. But I then get the following error:

 error: cannot spawn .husky/pre-commit: No such file or directory

Any advice on what to do or how to actually use this package?

Upvotes: 0

Views: 123

Answers (1)

Cactusman07
Cactusman07

Reputation: 1001

For anyone else running into this issue, firstly check the version of Husky that was installed. All documentation pointed to v9, yet when installing Husky it installed v8.

You need to install using husky@latest as per changelog (https://github.com/typicode/husky/releases/tag/v9.0.1) which somehow (almost a year later) hasn't found it's way into the online documentation yet...

Upvotes: 1

Related Questions