Lazy load nvm for faster shell start

NVM is a version manager for node that makes using specific versions of Node a breeze. I prefer to use it on my development machine instead of system wide node as it gives much more control with almost no added complexity.

Once you install it , it adds the following snippet to your .bashrc :

export NVM_DIR="/Users/zaro/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm

and everything just works 🙂

Except that on my laptop this adds 1-2 seconds of start up time to each new shell I open. It’s a bit of annoyance and I don’t need it in every terminal session I start, so I though maybe there will be a way to load it on demand.

After fiddling a bit with it I replaced the NVM snippet with the following:

nvm() {
    unset -f nvm
    export NVM_DIR=~/.nvm
    [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
    nvm "$@"
}

node() {
    unset -f node
    export NVM_DIR=~/.nvm
    [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
    node "$@"
}

npm() {
    unset -f npm
    export NVM_DIR=~/.nvm
    [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
    npm "$@"
}

Now nvm, node and npm are  loaded on their first invocation, posing no start up time penalty for the shells that aren’t going to use them at all.

Edit: Thanks to jonknapp’s suggestion, now the snippet is more copy paste friendly.

Edit: fl0w_io made a standalone script out of it to include in .bashrc

Edit: sscotth made a version that will register all your globally installed modules

9 thoughts on “Lazy load nvm for faster shell start”

  1. It’s work, thankyou.
    I modified a bit, so when I will use node, I run command fnode

    fnode() {
    export NVM_DIR=”$HOME/.nvm”
    [ -s “$NVM_DIR/nvm.sh” ] && \. “$NVM_DIR/nvm.sh”
    [ -s “$NVM_DIR/bash_completion” ] && \. “$NVM_DIR/bash_completion”
    }

    Sorry bad english

  2. I get

    -bash:        : command not found
    -bash:        : command not found
    -bash:        : command not found
    -bash:        : command not found

    every time I try and run nvm, node or npm.

    1. Weird. bash is trying to execute an command that is only white space, which I don’t see how it’s possible unless something messed up during copy paste.

    2. Did you try my code snippet as well? It’s slightly different and worked on my mac as the original code did not.

      1. Yeah, I did. You use brew to install nvm and I prefer to install nvm directly, so your version wouldn’t work for me 🙂

  3. Excellent job! I have some suggested changes to get it to work for other users automatically and also get it working on my machine (slightly different nvm command).

    “`
    nvm() {
    unset -f nvm
    export NVM_DIR=~/.nvm
    source $(brew –prefix nvm)/nvm.sh
    nvm “$@”
    }

    node() {
    unset -f node
    export NVM_DIR=~/.nvm
    source $(brew –prefix nvm)/nvm.sh
    node “$@”
    }

    npm() {
    unset -f npm
    export NVM_DIR=~/.nvm
    source $(brew –prefix nvm)/nvm.sh
    npm “$@”
    }
    “`

Leave a comment

%d