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_DIR/" # 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.


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