lukechilds / zsh-nvm

Zsh plugin for installing, updating and loading nvm
MIT License
2.22k stars 112 forks source link

NVM_AUTO_USE doesn't work with NVM_LAZY_LOAD until too late #72

Open HaleTom opened 3 years ago

HaleTom commented 3 years ago

_zsh_nvm_auto_use will return early if nvm_find_nvmrc is not found, which will be the case when lazy loading.

Would you consider including a copy of nvm_find_nvmrc inside your code if lazy loading, so that auto use can work as intended, before nvm, node, etc are run for the first time?

ht55ght55 commented 3 years ago

I was having the same issue, and I thought I would share my workaround in case someone might find it useful.

Basically, I declare a directory where, if I open a shell in it or it's sub-dirs, nvm is loaded.

# <-------- .zshrc -------->

# <------- export zsh-nvm variables **above** -----> 
# Declare the directory(s) where you code
export NVM_LAZY_AUTO_DIR=("$HOME/code")

activate_nvm() {
  # checking dir in path, and if node is a shell function (not loaded)
  if [[ $PWD =~ $NVM_LAZY_AUTO_DIR && "$(type node)" = *'a shell function'* ]]; then
    print 'Activating nvm...'

    # trigger loading
    node --version
    # cd into same dir to activate auto_use
    cd $PWD

  fi
}

# use this function if LAZY_LOAD is true
if [[ ( $NVM_LAZY_LOAD ) ]]; then
  precmd_functions+=(activate_nvm)
fi

# <-------- source zsh-nvm **below** --------->