Skip to content

Instantly share code, notes, and snippets.

@kritish-dhaubanjar
Last active January 30, 2026 18:43
Show Gist options
  • Select an option

  • Save kritish-dhaubanjar/571e66c60ee89faf779fca3493bf49c0 to your computer and use it in GitHub Desktop.

Select an option

Save kritish-dhaubanjar/571e66c60ee89faf779fca3493bf49c0 to your computer and use it in GitHub Desktop.
source nvm_selector.sh
#!/bin/bash
function get_nvmrc_search_path(){
local DIR=$1
local PATH="$DIR/.nvmrc"
echo $PATH
}
function nvmrc_selector(){
builtin cd "$@"
local DIR=$(pwd)
local DEPTH=$(( $(grep -o '/' <<< $DIR | wc -l) + 1 ))
while true; do
local NVMRC_PATH=$(get_nvmrc_search_path "$DIR")
if [[ -f $NVMRC_PATH ]]; then
local CURRENT_NODE_VERSION=$(nvm current)
local TARGET_NODE_VERSION="$(cat $NVMRC_PATH)"
if [[ $CURRENT_NODE_VERSION != $TARGET_NODE_VERSION ]]; then
nvm use
fi
break
fi
if [[ -z $DIR ]]; then
break
fi
DIR="${DIR%/*}"
done
}
alias cd='nvmrc_selector'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment