Last active
September 20, 2024 17:41
-
-
Save calle2010/b3f0054c1d4b72394d0fda7f22d47b38 to your computer and use it in GitHub Desktop.
Use NVM in fish
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function nvm_find_nvmrc | |
bash -c "source ~/.nvm/nvm.sh --no-use; nvm_find_nvmrc" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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