Skip to content

Instantly share code, notes, and snippets.

@nicholasf
Last active September 14, 2017 07:29
Show Gist options
  • Save nicholasf/7988a210ec4a7aa2be16377db6b99038 to your computer and use it in GitHub Desktop.
Save nicholasf/7988a210ec4a7aa2be16377db6b99038 to your computer and use it in GitHub Desktop.
Alternative to loading nvm for each prompt

Background

It feels too slow to load .nvm for each prompt. Instead I indicate the default version of Node I want to use. Works well everywhere unless I need to use older code.

This function scans for a .nvmrc in the dir you've switched to, if it's present it will:

  • identify the node version
  • load the nvm environment
  • switch to the node version.

Using it in .zshrc

The bottom of my .zshrc looks like:

# dont load nvm (too slow), just put the version you want on your path.
export PATH=$PATH:/Users/faizn1/.nvm/versions/node/v8.4.0/bin

function chpwd {
  if [[ -a .nvmrc ]]; then
        CURRENT_NVER=`node -v`
        NVER=`cat .nvmrc`
        if [ "$CURRENT_NVER" != "$NVER" ]; then
                export NVM_DIR="$HOME/.nvm"
                [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
                eval "nvm use $NVER"
        fi
  fi
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment