Skip to content

Instantly share code, notes, and snippets.

@gfguthrie
Created September 27, 2019 22:02
Show Gist options
  • Save gfguthrie/9f9e3908745694c81330c01111a9d642 to your computer and use it in GitHub Desktop.
Save gfguthrie/9f9e3908745694c81330c01111a9d642 to your computer and use it in GitHub Desktop.
Lazy Load Homebrew NVM but still have default aliased Node in PATH
# normal brew nvm shell config lines minus the 2nd one
# lazy loading the bash completions does not save us meaningful shell startup time, so we won't do it
export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/etc/bash_completion" ] && . "/usr/local/opt/nvm/etc/bash_completion" # This loads nvm bash_completion
# add our default nvm node (`nvm alias default 10.16.0`) to path without loading nvm
export PATH="$NVM_DIR/versions/node/v$(<$NVM_DIR/alias/default)/bin:$PATH"
# alias `nvm` to this one liner lazy load of the normal nvm script
alias nvm="unalias nvm; [ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"; nvm $@"
@olestole
Copy link

Works great, thanks! It would make it even clearer if you made sure to add comments about:

  • Running nvm alias default [preffered node-version]
  • Changing "/usr/local/opt/nvm/nvm.sh" to your local nvm.sh location

@sescotti
Copy link

sescotti commented Apr 12, 2020

Worked just fine on MacOS and zsh 👍 . One small caveat I noticed by using this is that you cannot run which nvm anymore, it will echo the executable output instead of returning the bin location.

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