Search code examples
gitnpmnvmgit-huskycommitizen

Can't commit when combining commitizen + husky: nvm is not compatible with the "npm_config_prefix" environment variable


I have a project that was using commitizen without problems. I've decided to add husky to run precommit hooks with prettier. The problem is that when I run the npm run cm command, I get this error...

.....................
? Are there any breaking changes? No
? Does this change affect any open issues? No
nvm is not compatible with the "npm_config_prefix" environment variable: currently set to "/Users/aiglesias/.nvm/versions/node/v8.9.4"
Run `unset npm_config_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)
/Users/aiglesias/Coding/react/react-basesupsub/node_modules/commitizen/dist/cli/strategies/git-cz.js:102
        throw error;
        ^

Error: git exited with error code 1
    at ChildProcess.<anonymous> (/Users/aiglesias/Coding/react/react-basesupsub/node_modules/commitizen/dist/git/commit.js:50:26)
    at emitTwo (events.js:126:13)
    at ChildProcess.emit (events.js:214:7)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] cm: `git-cz`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] cm script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/aiglesias/.npm/_logs/2018-02-03T08_22_42_663Z-debug.log

I've removed prettier and try just husky and a basic npm run test precommit hook, with the same error. Obviously, I see the message, but I don't know if running unset npm_config_prefix can cause me bigger problems in other places.

EDITED:

  • Neither $NPM_CONFIG_PREFIX nor $PREFIX are set in my environment.
  • I've tried unset npm_config_prefix, but with the same results.

Solution

  • After going crazy, is apparently a problem with current stable version of husky. Upgrading to the new husky 0.15.0-rc.3 version solved the issue.

    Just in case you face this problem in the future, to upgrade you just have to...

    1. npm i husky@next -D
    2. Go to package json and move your hooks from "script" to the new husky.hooks section, paying attention to the modified names (from precommit to pre-commit, for example). In my case...

      BEFORE:

      "scripts": {
          ...
          "precommit": "lint-staged"
      }
      

      AFTER:

      "scripts": {
          ...
      },
      "husky": {
          "hooks": {
              "pre-commit": "lint-staged"
          }
      }