Fixing ZSH slowdown caused by NVM

When I started using oh-my-zsh, the first things I noticed was how slow it was to startup.

To find the root cause, I turned on profiling by adding zmodload zsh/zprof at the start of .zshrc file and zprof at the end, this clearly showed that nvm was the main reason of the slowdown.

zprof

The best solution I found was removing the nvm plugin inside .zshrc and replacing the default nvm stuff with the following:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" --no-use # This loads nvm

alias node='unalias node ; unalias npm ; nvm use default ; node [email protected]'
alias npm='unalias node ; unalias npm ; nvm use default ; npm [email protected]'

This fixes the issue and moves the slowness to the first time you actually run node or nvm. More info on this Github issue comment by @parasyte.

Alternative: Replacing nvm with fnm

If you want a less “patchy” solution, you can just entirely replace nvm with fnm, a much faster Node.js version manager built in Rust with the following main features:

fnm

  • 🌍 Cross-platform support (macOS, Windows, Linux)

  • ✨ Single file, easy installation, instant startup

  • 🚀 Built with speed in mind

  • 📂 Works with .node-version and .nvmrc files

Installation

brew install fnm

And then add the following to your .zshrc profile

eval "$(fnm env --use-on-cd)"

I also created an alias to stop typing nvm by habit

alias nvm='echo "(╯°□°)╯︵ɯʌu, did you mean fnm?"'

Made with ❤ and #Code