husky: Cannot execute precommit hook if triggered inside `yarn run` or `npm run`
Do you want to request a feature or report a bug?
Husky cannot execute precommit hook in yarn run <script>
, because nvm cannot load correctly.
What is the current behavior?
Yarn cannot run this script:
yarn run release --prerelease
yarn run v1.3.2
$ standard-version --prerelease
✔ bumping version in package.json from 2.0.1 to 2.0.2-0
✔ outputting changes to CHANGELOG.md
✔ committing package.json and CHANGELOG.md
nvm is not compatible with the "PREFIX" environment variable: currently set to "/usr/local"
Run `unset PREFIX` to unset it.
.git/hooks/pre-commit: line 49: node: command not found
husky > npm run -s precommit (node )
env: node: No such file or directory
husky > pre-commit hook failed (add --no-verify to bypass)
Command failed: git commit CHANGELOG.md package.json -m "chore(release): 2.0.2-0"
nvm is not compatible with the "PREFIX" environment variable: currently set to "/usr/local"
Run `unset PREFIX` to unset it.
.git/hooks/pre-commit: line 49: node: command not found
husky > npm run -s precommit (node )
env: node: No such file or directory
husky > pre-commit hook failed (add --no-verify to bypass)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
But npm can:
npm run release -- --pre-release
> xxd-backend@2.0.0-alpha.1 release /Users/linkeo/Projects/service/xxd-backend
> standard-version "--pre-release"
✔ bumping version in package.json from 2.0.0-alpha.1 to 2.0.0
✔ outputting changes to CHANGELOG.md
✔ committing package.json and CHANGELOG.md
husky > npm run -s precommit (node v8.9.1)
yarn run v1.3.2
$ bash bin/_internal/lint.sh
- Lint using eslint (incremental scan)
- Code not changed, linting is skipped.
Done in 0.27s.
husky > npm run -s commitmsg (node v8.9.1)
⧗ input: chore(release): 2.0.0
✔ found 0 problems, 0 warnings
✔ tagging release 2.0.0
ℹ Run `git push --follow-tags origin master; npm publish` to publish
Because PREFIX
is mentioned above, I run these commands to check env:
yarn run debug # env
npm run debug # env
Comparing the results, I notice yarn add PREFIX=/local/bin
in yarn run <script>
command.
So nvm cannot be correctly loaded in this environment.
If the current behavior is a bug, please provide the steps to reproduce.
My project is using standard-version
to auto-generate CHANGELOG.md, and husky
to check code style(eslint
) or commit message(commitlint
) during commiting.
So you can quickly reproduce by:
Execute:
mkdir test && cd test
touch package.json index.js .gitignore
Edit package.json
{
"name": "test",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"commitlint": "^4.3.0",
"eslint": "^4.11.0",
"husky": "^0.14.3",
"standard-version": "^4.2.0"
},
"scripts": {
"release": "standard-version",
"precommit": "eslint .",
"commitmsg": "commitlint -e $GIT_PARAMS",
"debug": "env | grep PREFIX"
}
}
Edit index.js
This cannot be passed by eslint.
Edit .gitignore
node_modules
Execute:
git init
git add .
git commit -m 'init'
yarn
yarn run release
Then you will get the error above.
What is the expected behavior?
husky git hooks should correctly executed even triggered by yarn run <script>
commands.
Please mention your node.js, yarn and operating system version. OS: macOS Sierra 10.12.6 nvm: v0.33.6 yarn: v1.3.2 node: v8.9.1 (managed by nvm) npm: 5.5.1
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 16
- Comments: 16 (3 by maintainers)
@leohxj That don’t solve my problem.
And I’m facing a new problem… It seems if git commit is happened inside a npm script, husky hooks cannot work fine because ‘nvm is not compatible with the “npm_config_prefix” environment variable’, and
npm run <script-name>
will set “npm_config_*” environment itself…I can reproduce this problem inside docker:
Actually, I’m using
standard-version
to manage my package versions (runnpm run release
to release an new version). This script will commit changes to git repo, then husky’s precommit hook will be triggered with environment variables inside the npm script.I can’t see the solution to this problem by myself by now.
A possible solution with husky is: Check if the git hook is executed inside an npm script, if so, do not load nvm. Reason: Since
git commit
is happened inside thenpm run
, some version of node/npm is used already, so there is no need to load nvm again in git hook.yarn add husky@next --dev
gives this error:macOS 10.13.5, yarn 1.7.0, npm 6.1.0
I’d suggest trying with
husky@next
as the code has changed quite a lot since this issue.Let me know if you still doesn’t work.
That worked!
solve the problems
rm -rf node_modules/ yarn clean cache and yarn install won’t work
Hi @mrextreme,
I’m not reproducing, I would try running
yarn remove husky && yarn add husky@next --dev
, just to ensure that all husky’s dependencies get installed correctly.If it doesn’t work, I’d try removing
node_modules
and runningyarn
again.I tried to edit
.git/hooks/pre-commit
inside my project, change line 34 and 37:Then it works.
But I’m not sure which is the better solution:
yarn run
.