Improve shell start-up time when using nvm

I was finding terminal sessions took a long time to start up on my machine, and eventually discovered that nvm was taking the majority of the start-up time.

In order to mitigate this, I came up with a way to only load nvm when it was really needed, but still keep access to my default node installation.

I added the following to my .profile (yours might be .bashrc, .bash_profile, .zshrc, etc) instead of the usual nvm stuff:

# Wrap nvm up to stop it slowing login to a crawl
export NVM_DIR="$HOME/.nvm"

loadnvm() {
    source "$NVM_DIR/nvm.sh"
    return $?;
}

nvm() {
    unset -f nvm;
    loadnvm && nvm $*;
}

# Add default nvm bin to PATH
NVM_PATH=~/.nvm/versions/node/$(cat $NVM_DIR/alias/default)/bin
export PATH=$NVM_PATH:$PATH

This means that on startup of my shell, my path is updated to contain the bin from whichever version of node is currently set as default, but if I run any nvm commands, then my hack is removed and nvm is loaded and passed the arguments I typed.

Improve shell start-up time when using nvm
Mat Gadd