husky: Husky v5 add command fails in Windows using npx

When using the npx husky command in Windows (cmd or PowerShell, using single or double quotes), the arguments including the desired hook script are split on spaces, so the command fails with too may arguments. When running in Linux or in Windows by executing the husky bin command directly, the add command is properly processed.

An example adding console.log(process.argv) before the arguments are processed.

In Linux

/app # npx husky add .husky/pre-commit "npm test"
[
  '/usr/local/bin/node',
  '/app/node_modules/husky/lib/bin.js',
  'add',
  '.husky/pre-commit',
  'npm test'
]
add
created .husky/pre-commit

In Windows

PS C:\mymodule> npx husky add .husky/pre-commit "npm test"
[
  'C:\\Program Files\\nodejs\\node.exe',
  'C:\\mymodule\\node_modules\\husky\\lib\\bin.js',
  'add',
  '.husky/pre-commit',
  'npm',
  'test'
]
Usage

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

Examples

  husky install
  husky install .config/husky

  husky add .husky/pre-commit
  husky add .husky/pre-commit "npm test"
  husky add .config/husky/pre-commit "npm test"

PS C:\mymodule>> .\node_modules\.bin\husky add .husky/pre-commit "npm test"
[
  'C:\\Program Files\\nodejs\\node.exe',
  'C:\\mymodule\\node_modules\\husky\\lib\\bin.js',
  'add',
  '.husky/pre-commit',
  'npm test'
]
[ '.husky/pre-commit', 'npm test' ]
created .husky\pre-commit

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 15 (2 by maintainers)

Most upvoted comments

npx husky-init

For windows:node_modules/.bin/husky add .husky/commit-msg ‘npx --no-install commitlint --edit “$1”’ –> https://typicode.github.io/husky/#/?id=automatic-recommended

Doesn’t work on Windows:

PS G:\git\test-husky-changelog-gen> npx husky add .husky/commit-msg "npx --no-install commitlint --edit '$1'"
Usage:
  husky install [dir] (default: .husky)
  husky uninstall
  husky set|add <file> [cmd]

PS G:\git\test-husky-changelog-gen> npm -v
6.14.13

So I had to make .husky/commit-msg manually:

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

npx --no-install commitlint --edit "$1"

Note, no single quotes around the npx command, that caused it to not work.

Seems to be fixed in npm v7.5.4

something’s strange in the neighborhood!

if i install husky on my windows 11 machine with:

npm install husky --save-dev npx husky-init npm run prepare

then i can do, for example: npx husky set .husky/pre-commit "pretty-quick"

inside my pre-commit file in the .husky folder i get:

#!/bin/sh . “$(dirname “$0”)/_/husky.sh” pretty-quick

so this works … but if i pass an argument e.g.:

npx husky set .husky/pre-commit "pretty-quick --staged"

it does not work:

please review changes in package.json Usage: husky install [dir] (default: .husky) husky uninstall husky set|add <file> [cmd]

i tried it with eslint, same story

also can’t bind anything with npm as prefix, not even npm test, which is the default.

still not working for husky@7.0.0 with pnpx/npx but if use

./node_modules/.bin/husky add '.husky/commit-msg' 'npx --no -- commitlint --edit $1'

it will work

As a temporary workaround, could you try:

npx add .husky/commit-msg

Then manually add npx commitlint --edit $1 to .husky/commit-msg

So, I tried one more time and everything went OK. Strange.

I still have error.

image

"@commitlint/cli": "^12.0.1",
"@commitlint/config-conventional": "^12.0.1",
"husky": "^6.0.0",