Skip to content

Instantly share code, notes, and snippets.

@nateware
Last active September 1, 2026 00:12
Show Gist options
  • Select an option

  • Save nateware/7783571b9ccc7466deec7c425f827478 to your computer and use it in GitHub Desktop.

Select an option

Save nateware/7783571b9ccc7466deec7c425f827478 to your computer and use it in GitHub Desktop.
nvm lazy-loading trick - it is SO SLOW to load
# Put this in ~/.zshrc (or ~/.bashrc)
# nvm lazy-loading trick - it is SO SLOW to load
# This will delay loading nvm until the first nvm/node/npm command
export NVM_DIR="$HOME/.nvm"
_nvm_lazy_load() {
unset -f nvm node npm npx
echo "Lazy loading nvm...\c"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
echo "done."
"$@"
}
nvm() {
_nvm_lazy_load nvm "$@"
}
node() {
_nvm_lazy_load node "$@"
}
npm() {
_nvm_lazy_load npm "$@"
}
npx() {
_nvm_lazy_load npx "$@"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment