Skip to content

Instantly share code, notes, and snippets.

@joelkesler
Last active September 10, 2025 18:52
Show Gist options
  • Select an option

  • Save joelkesler/3c42f12bc0f77edb700d718d143a5bef to your computer and use it in GitHub Desktop.

Select an option

Save joelkesler/3c42f12bc0f77edb700d718d143a5bef to your computer and use it in GitHub Desktop.
Lazyload NVM (for bash and zsh)
#!/bin/bash
# I recomend adding this to your ~/.bash_profile and disabling the default NVM loading command
# --------------------------------------------------
# Lazyload NVM for Bash
# --------------------------------------------------
# Load NVM/Node/Ext when needed. This loads the terminal quicker
# based on https://old.reddit.com/r/node/comments/4tg5jg/lazy_load_nvm_for_faster_shell_start/d5ib9fs/
# --------------------------------------------------
# Create an array of all node executables, plus node and nvm
NODE_GLOBALS=($(find ~/.nvm/versions/node -maxdepth 3 -type l -wholename '*/bin/*' | xargs -n1 basename | sort | uniq));
NODE_GLOBALS+=('node'); NODE_GLOBALS+=('nvm');
export NVM_DIR="$HOME/.nvm"
load_nvm () {
[ -s "$NVM_DIR/nvm.sh" ] && source "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && source "$NVM_DIR/bash_completion"
}
# Create a new function for each installed node executable.
for cmd in "${NODE_GLOBALS[@]}"; do
eval "${cmd}(){ unset -f ${NODE_GLOBALS[@]}; load_nvm; ${cmd} \$@; }"
done
unset NODE_GLOBALS
# --------------------------------------------------
#!/bin/zsh
# I recomend adding this to your ~/.zprofile and disabling the default NVM loading commands
# --------------------------------------------------
# Lazyload NVM for Zsh
# --------------------------------------------------
# Load NVM/Node/Ext when needed to speed up creating a new terminal session
# By Joel Kesler - Based on https://old.reddit.com/r/node/comments/4tg5jg/lazy_load_nvm_for_faster_shell_start/d5ib9fs/
# --------------------------------------------------
# array of all node executables, plus node and nvm
declare -a NODE_GLOBALS=(`find ~/.nvm/versions/node -maxdepth 3 -type l -wholename '*/bin/*' | xargs -n1 basename | sort | uniq`);
NODE_GLOBALS+=("node"); NODE_GLOBALS+=("nvm")
export NVM_DIR="$HOME/.nvm"
load_nvm () {
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
}
for cmd in "${NODE_GLOBALS[@]}"; do
eval "${cmd}(){ unset -f ${NODE_GLOBALS}; load_nvm; ${cmd} \$@ }"
done
unset NODE_GLOBALS
# --------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment