Skip to content

Instantly share code, notes, and snippets.

@Drumstix42
Created December 19, 2024 02:31
Show Gist options
  • Select an option

  • Save Drumstix42/15759e798ff2a26c70acda0328358a0c to your computer and use it in GitHub Desktop.

Select an option

Save Drumstix42/15759e798ff2a26c70acda0328358a0c to your computer and use it in GitHub Desktop.
Load nvm version based on nvmrc for Windows
# ------------------------------------------------------
# Load proper nvm version if a nvmrc is given on WINDOWS
# ------------------------------------------------------
load-nvmrc() {
if [[ -f .nvmrc && -r .nvmrc ]]; then
declare version;
version="$(< .nvmrc)";
if [ -z "$(node -v | grep $version)" ]; then
if [ -z "$(nvm list | grep $version)" ]; then
echo "Node $version is not currently installed through nvm. Installing.";
nvm install $version;
fi
nvm use $version;
else
return; # same version we're running, no need for anything else.
fi
fi
}
load-nvmrc
# Override `cd` to auto-load correct version of Node on enterting directory.
cd() { builtin cd "$@"; 'load-nvmrc'; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment