Created
January 30, 2023 08:21
-
-
Save shhider/fda204f598c367d48b5eaed0d14ad353 to your computer and use it in GitHub Desktop.
[NVM lazy Loading] #node #nvm
This file contains 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
# from: https://blog.yo1.dog/better-nvm-lazy-loading/ | |
export NVM_DIR="$HOME/.nvm" | |
# This lazy loads nvm | |
nvm() { | |
unset -f nvm | |
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" --no-use # This loads nvm | |
nvm $@ | |
} | |
# This loads nvm bash_completion | |
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" | |
# This resolves the default node version | |
DEFAULT_NODE_VER="$((cat "$NVM_DIR/alias/default" || cat ~/.nvmrc) 2> /dev/null)" | |
while [ -s "$NVM_DIR/alias/$DEFAULT_NODE_VER" ] && [ ! -z "$DEFAULT_NODE_VER" ]; do | |
DEFAULT_NODE_VER="$(cat "$NVM_DIR/alias/$DEFAULT_NODE_VER")" | |
done | |
# This resolves the path to the default node version | |
DEFAULT_NODE_VER_PATH="$(find $NVM_DIR/versions/node -maxdepth 1 -name "v${DEFAULT_NODE_VER#v}*" | sort -rV | head -n 1)" | |
# This adds the default node version path to PATH | |
if [ ! -z "$DEFAULT_NODE_VER_PATH" ]; then | |
export PATH="$DEFAULT_NODE_VER_PATH/bin:$PATH" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment