question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

precommit runs twice when using husky

See original GitHub issue

Here’s my relevant package.json:

"devDependencies": {
    "commitizen": "^2.9.6",
    "cz-conventional-changelog": "^2.0.0",
    "husky": "^0.13.2",
    "lint-staged": "^3.4.0",
    "prettier": "^1.2.2"
},
"scripts": {
   "precommit": "lint-staged --verbose",
   "commit": "git-cz",
},
"lint-staged": {
    "*.js": [
      "prettier --single-quote --semi=false --write",
      "git add"
    ]
},
"config": {
    "commitizen": {
      "path": "./node_modules/cz-conventional-changelog"
    }
},

lint-staged is correctly called once before the commitizen command line form appears, and then incorrectly husky - npm run -s precommit is run after. Typically without commitizen only lint-staged is called. Somehow commitizen is triggering husky to be called. No where do I have this script: npm run -s precommit. It seems commitizen somehow triggers it after you complete the form.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jimthedevcommented, May 9, 2017

That is a bug in husky that happens due to a “feature” of npm-scripts that auto run scripts with the name prexxx where xxx is the name of another script. So, because you’re using commit and precommit it is running them twice.

Until husky fixes this you can simply change your commitizen task to cmt or something else (cz, co, etc).

We could probably update our docs to reflect that when using commitizen and husky you should pick another name than npm run commit. Want to put in a PR?

0reactions
faceyspaceycommented, May 10, 2017

nice find!

Here’s the PR as requested: https://github.com/commitizen/cz-cli/pull/433

Read more comments on GitHub >

github_iconTop Results From Across the Web

Commitizen and husky seem to be triggering the pre-commit ...
I am trying to configure commitizen with husky and when I commit using a custom script from package.json commit gets triggered twice.
Read more >
Developers - should husky be running precommit hooks twice
should husky be running precommit hooks twice: once before commitizen wizards and again after?
Read more >
pre-commit
To run individual hooks use pre-commit run <hook_id> . The first time pre-commit runs on a file it will automatically download, install, and...
Read more >
Getting started with Husky and Lint-staged for pre-commit hooks
Think of Prettier converting double quotes to single quotes for example. Lint-staged is also pre-configured to run the linting scripts on the staged...
Read more >
Husky + Lint-Staged on a React TypeScript Project
"pre-commit": "npm run test -- --watchAll=false" } }. Note: As you see, Husky Configuration file can use any script on “package.json” by ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found