Skip to content

Instantly share code, notes, and snippets.

@jmickey
Created August 26, 2020 19:22
Show Gist options
  • Select an option

  • Save jmickey/616100c88b9fd4e37fa3189e26ce64e1 to your computer and use it in GitHub Desktop.

Select an option

Save jmickey/616100c88b9fd4e37fa3189e26ce64e1 to your computer and use it in GitHub Desktop.
Lazy load nvm to avoid slow shell start-up times
# Lazy load NVM!
# Add every binary that requires nvm, npm or node to run to an array of node globals
NODE_GLOBALS=(`find ~/.nvm/versions/node -maxdepth 3 -type l -wholename '*/bin/*' | xargs -n1 basename | sort | uniq`)
NODE_GLOBALS+=("node")
NODE_GLOBALS+=("nvm")
NODE_GLOBALS+=("npx")
# Lazy-loading nvm + npm on node globals call
load_nvm () {
export NVM_DIR=~/.nvm
[ -s "$(brew --prefix nvm)/nvm.sh" ] && . "$(brew --prefix nvm)/nvm.sh"
if [ -f "$(brew --prefix nvm)/bash_completion" ]; then
[ -s "$(brew --prefix nvm)/bash_completion" ] && \. "$(brew --prefix nvm)/bash_completion"
fi
}
# Making node global trigger the lazy loading
for cmd in "${NODE_GLOBALS[@]}"; do
eval "${cmd}(){ unset -f ${cmd} >/dev/null 2>&1; load_nvm; ${cmd} \$@; }"
done
unset cmd NODE_GLOBALS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment