preface


Using husky 7 (.husky), the hook script does not support vscode Git control

The environment

  • mac
  • Node Management Tool (NVM)
  • husky 7

The problem

Take a real world 🌰



Husky 7 is initialized to generate a.husky directory in the project root directory,

They recommend scripting different hook intercepts, so we add the most common (pre-commit)



The script logic is as simple as the executing shell using sh (the most generic shell),

Both Unix and Linux have this built in

If you specify ZSH, other environments may not have ZSH ~

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

# NPX is a temporary binary execution tool for the nod belt (it will pull by default)
npx lint-staged
Copy the code

The logic is simply lint-staged before committing



If you use the command line (in your terminal), because you can recognize node,

The hook content in this is certainly executable ~ ~



This one is thrown in GUI execution



The absence of NPX may surprise you at first, but the analysis makes it clear;

The shell we specified is sh, and the default configuration in it definitely doesn’t know where NVM is.

To solve

To solve this problem is to complete the identification NVM, which requires some Linux knowledge, which is not very complicated, just some combination of shells

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

export NVM_DIR="$HOME/.nvm/nvm.sh"
. "$(dirname $NVM_DIR)/nvm.sh"

export NVM_DIR="$HOME/.nvm"
matchNvmUseNodeVersion=$(nvm ls | grep 'node'| grep -oE "v([0-9]+\.?) +")
export PATH="$NVM_DIR/versions/node/$matchNvmUseNodeVersion/bin:$PATH"

npx lint-staged

Copy the code

rendering



conclusion

If there is any mistake, please leave a message and it will be corrected in time. Thank you for reading