Fixing ZSH slowdown caused by NVM

When I first started using oh-my-zsh on my MacBook, I immediately 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 culprit.

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 $@'
alias npm='unalias node ; unalias npm ; nvm use default ; npm $@'

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 with less downsides, you can just entirely replace nvm with fnm, a much faster Node.js version manager built in Rust. It’s compatible with nvm and works with .nvmrc and .node-version.

Installation

brew install fnm

And then add the following to your .zshrc profile

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

I also created an alias so I can stop typing nvm by habit

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

Made with ❤ and #Code