husky icon indicating copy to clipboard operation
husky copied to clipboard

husky init overwrites existing prepare script without warning

Open IllegalCreed opened this issue 10 months ago • 2 comments

Description

When running pnpm dlx husky init (or npx husky init) in a project with an existing prepare script in package.json, Husky overwrites it without any warning or prompt. This behavior can lead to the unintended loss of existing setup logic, which is particularly problematic in projects with multiple tools relying on the prepare script.

Steps to Reproduce

Create a package.json with an existing prepare script:

{
  "scripts": {
    "prepare": "cypress install"
  }
}

Install Husky:

pnpm add -D husky

Run the init command:

pnpm dlx husky init

Check package.json after execution.

Expected Behavior

If a prepare script already exists, Husky should either: Skip modifying package.json and log a message (e.g., "Existing prepare script detected, please manually add husky if needed"). Prompt the user to confirm whether to overwrite or merge the script.

Actual Behavior

The existing prepare script ("cypress install") is silently overwritten with "husky". After running pnpm dlx husky init, package.json becomes:

{
  "scripts": {
    "prepare": "husky"
  }
}

This removes the original cypress install command, breaking the project's setup workflow.

IllegalCreed avatar Mar 11 '25 02:03 IllegalCreed

This is not a good design,i used husky@8 instead。

moonlitusun avatar Oct 26 '25 02:10 moonlitusun