Skip to content

Instantly share code, notes, and snippets.

@jamsinclair
Created February 16, 2021 01:40
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jamsinclair/f6927c4e767e49545bf2a75bfc563d3a to your computer and use it in GitHub Desktop.
Lazy Load NVM (node npm, npx, yarn)
load-nvmrc() {
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$node_version" ]; then
nvm use
fi
elif [ "$node_version" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
lazy_load_nvm() {
unset -f nvm node npm npx yarn
export NVM_DIR=~/.nvm
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
load-nvmrc
if [ -f "$NVM_DIR/bash_completion" ]; then
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
fi
}
nvm() {
lazy_load_nvm
nvm $@
}
node() {
lazy_load_nvm
node $@
}
npm() {
lazy_load_nvm
npm $@
}
npx() {
lazy_load_nvm
npx $@
}
yarn() {
lazy_load_nvm
yarn $@
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment