DEV Community

Cover image for Fix slow ZSH startup due to NVM
Aron Schüler
Aron Schüler

Posted on • Updated on • Originally published at aronschueler.de

Fix slow ZSH startup due to NVM

NVM can really be helpful when dealing with older projects. But there’s a problem, still. The nvm installation adds a portion of code to your .zshrc to enable autocompletion. This, in turn, slows the startup of your shell quite a lot.

But we can easily fix this slow zsh startup! Instead of always loading your nvm autocompletion, we can command oh-my-zsh to just load nvm ressources on-demand, also known as lazy-loading.

How do I check if just NVM slows my ZSH startup?

That’s easy. In your ~/.zshrc you can simply add zmodload zsh/zprof as first line and zprof as last line. This will prove you a nice output on which plugin caused what delay.

So go ahead, add these both lines to your shell startup script and time your shell startup with time zsh -i -c exit. This will start your ZSH and execute the exit command.
Scroll a bit up and your zsh output should blame nvm:

Slow startup on ZSH after installing NVM

Bad, right? That’s almost 90% startup time caused by a single tool I
don’t use that much. So let’s fix this!

How do I fix the slow zsh startup?

As the average ZSH user, you probably already have different plugins provided by oh-my-zsh. If not, really, go ahead and get yourself some nice plugins! It’s a community driven aggregator of many many great plugins.

The plugin we need now is the nvm plugin. It’s already included in oh-my-zsh and should be activated in your plugins array somewhere in your .zshrc.

But you also want to add the line zstyle ':omz:plugins:nvm' lazy yes, which enables the
lazy-loading mode. This mode defers the load of the nvm plugin and activates the script when using it.
To further optimize your startup you can replace that line with zstyle ':omz:plugins:nvm' autoload yes.
This will only source the plugin in directories that contain a .nvmrc file. So, in conclusion,
add/modify the following in your zsh startup script:



zstyle ':omz:plugins:nvm' lazy yes
# Alternative:
# zstyle ':omz:plugins:nvm' autoload yes
plugins=(
  nvm
  // [...]
)
// [...]


Enter fullscreen mode Exit fullscreen mode

Also, be sure to remove the lines added by nvm, they should be somewhere at the bottom of your script, mine looked like:



export NVM_DIR="$HOME/.nvm"
[ -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

Enter fullscreen mode Exit fullscreen mode




Checking fixed startup times

Now, go to any directory that shouldn’t autostart nvm and check your timings again. Mine looked like this:

Improved startup times after nvm lazy-loading

As you can see above, nvm support takes now much less time to start, while still being available “on-call”.

Done!

Now you can safely remove the debugging lines we introduced to our .zshrc earlier.

Hopefully I could help you a bit with this short post, see you around for the
next one 🙂

Top comments (2)

Collapse
 
wwayne profile image
wwayne

Solved my issue, thanks : )
Now we need to change export NVM_LAZY=1 to zstyle ':omz:plugins:nvm' lazy yes

Collapse
 
thraizz profile image
Aron Schüler

Yes, the new zstyle-based configuration replaced the environment-variable-based configuration of the plugin. Thank you for the comment!