Search code examples
visual-studio-codegithub-desktophuskylint-staged

VSCODE & GitHub Desktop pre-commit hook: npx: command not found


I am starting a new repo, thinking I should use the most recent Huksy v6 which is installed from LintStaged using their setup guide:

npx mrm lint-staged

// package.json updated with:
"husky": ">=6",
"lint-staged": ">=10",

This adds necessary packages and adds the husky files including the precommit files:

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

npx lint-staged

When i run my commit in the terminal it works fine. However, if I try to run my commit in GitHub Desktop or VSCode (which I know some teammates do), it results in an error for both:

npx: command not found. husky - pre-commit hook exited with code 127 (error)

I have npx installed:

npx -v
// 6.14.10

If I try to install in globall, such as described in other StackOverflow suggestions, it returns a warning about existing location (with & with out sudo):

ERR! EEXIST: file already exists, symlink '../lib/node_modules/npx/index.js' -> '/Users/plucks/.nvm/versions/node/v14.15.4/bin/npx' npm ERR! File exists: /Users/plucks/.nvm/versions/node/v14.15.4/bin/npx npm ERR! Remove the existing file and try again, or run npm npm ERR! with --force to overwrite files recklessly.

Is there anything I can do so the programs like VSCode & GitHub Desktop can run?


Solution

  • I had to combine the answers of Cathal and Misol.

    I did not want to edit the .husky/pre-commit like Cathal for two reasons:

    1. I would need to that for every project I use husky in
    2. It would actually break husky for my fellow developers on Windows

    So I added a global ~/.huskyrc file like Misol did with the following contents:

    export NVM_DIR="$HOME/.nvm/nvm.sh"
    . "$(dirname $NVM_DIR)/nvm.sh"
    
    export NVM_DIR="$HOME/.nvm"
    a=$(nvm ls | grep 'node')
    b=${a#*(-> }
    v=${b%%[)| ]*}
    
    export PATH="$NVM_DIR/versions/node/$v/bin:$PATH"