Auto-run nvm use on directory change in bash.
cdnvm() {
command cd "$@" || return $?
nvm_path="$(nvm_find_up .nvmrc | command tr -d '
')"
if [[ ! $nvm_path = *[^[:space:]]* ]]; then
default_version="$(nvm version default)"
if [ $default_version = 'N/A' ]; then
nvm alias default node
default_version=$(nvm version default)
fi
if [ "$(nvm current)" != "${default_version}" ]; then
nvm use default
fi
elif [[ -s "${nvm_path}/.nvmrc" && -r "${nvm_path}/.nvmrc" ]]; then
nvm_version=$(<"${nvm_path}"/.nvmrc)
locally_resolved_nvm_version=$(nvm ls --no-colors "${nvm_version}" | command tail -1 | command tr -d '\->*' | command tr -d '[:space:]')
if [ "${locally_resolved_nvm_version}" = 'N/A' ]; then
nvm install "${nvm_version}";
elif [ "$(nvm current)" != "${locally_resolved_nvm_version}" ]; then
nvm use "${nvm_version}";
fi
fi
}
alias cd='cdnvm'
cdnvm "$PWD" || exit
Reference: https://github.com/nvm-sh/nvm/blob/master/README.md#bash