Lazy Loading NVM to Speed Up Terminal Startup

Installing Node Version Manager (NVM) can significantly slow down the startup speed of a zsh terminal on macOS.

Adding the below script to .zshrc or .bashrc can enable lazy loading NVM:

# >>> NVM configuration >>>
export NVM_DIR="$HOME/.nvm"
nvm() {
  unset -f nvm
  [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
  nvm $@ # This copies arguments after nvm
}
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
# <<< NVM configuration <<<

Now the NVM will only be loaded when manually typing nvm in the terminal, and this will not interfere with other NVM commands.

Reference: https://github.com/microsoft/WSL/issues/776#issuecomment-526622844

Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy