Skip to content

Instantly share code, notes, and snippets.

@calle2010
Last active September 20, 2024 17:41
Show Gist options
  • Save calle2010/b3f0054c1d4b72394d0fda7f22d47b38 to your computer and use it in GitHub Desktop.
Save calle2010/b3f0054c1d4b72394d0fda7f22d47b38 to your computer and use it in GitHub Desktop.
Use NVM in fish
function load_nvm --on-variable PWD
set -l default_node_version $(nvm version default)
set -l node_version $(nvm version)
set -l nvmrc_path $(nvm_find_nvmrc)
if test -n "$nvmrc_path"
set -l nvmrc_node_version $(nvm version (cat $nvmrc_path))
if test "$nvmrc_node_version" = "N/A"
nvm install $(cat $nvmrc_path)
else if test "$nvmrc_node_version" != "$node_version"
nvm use $nvmrc_node_version
end
else if test "$node_version" != "$default_node_version"
echo "Reverting to default Node version"
nvm use default
end
end
function nvm
set -x current_path $(mktemp)
bash -c "source ~/.nvm/nvm.sh --no-use; nvm $argv; dirname \$(nvm which current) >$current_path"
fish_add_path -m $(cat $current_path)
rm $current_path
end
function nvm_find_nvmrc
bash -c "source ~/.nvm/nvm.sh --no-use; nvm_find_nvmrc"
end
# same as https://github.com/nvm-sh/nvm?tab=readme-ov-file#fish
# but without bass installed
# install nvm as usual, e. g. with "curl ... | bash"
# try it
nvm install 18
node -v # prints Node version 18
mkdir ~/test20; cat 20 >~/test20/.nvmrc
mkdir ~/test22; cat 22 >~/test22/.nvmrc
load_nvm
cd ~/test20
cd ..
cd ~/test22
cd ~/test20
cd ..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment