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)
npx husky-initFor 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:
So I had to make
.husky/commit-msgmanually: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-devnpx husky-initnpm run preparethen i can do, for example:
npx husky set .husky/pre-commit "pretty-quick"inside my pre-commit file in the .husky folder i get:
so this works … but if i pass an argument e.g.:
npx husky set .husky/pre-commit "pretty-quick --staged"it does not work:
i tried it with eslint, same story
also can’t bind anything with
npmas prefix, not evennpm test, which is the default.still not working for husky@7.0.0 with
pnpx/npxbut if useit will work
As a temporary workaround, could you try:
Then manually add
npx commitlint --edit $1to.husky/commit-msgSo, I tried one more time and everything went OK. Strange.
I still have error.