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 $@"
@yo1dog
Copy link

yo1dog commented Feb 5, 2020

@yo1dog your solution (mac) seems to create an infine loop for me

You running with BASH or ZSH? I found that BASH does not allow for local outside of functions which causes an error and an infinite loop as DEFAULT_NODE_VER is empty and [ -s ... ] on a directory always returns 0.

I updated the script to remove local so it should work under BASH as well.

@LeonardoGentile
Copy link

@LeonardoGentile if you echo $PATH, is the path to node in there correctly? If you copied my example exactly, but then use the v in the version you make default, it probably is not correct. You can specify the version without the v, modify the script to not add it, or use the first example @yo1dog posted that takes care of it either way.

That was it 👍

@LeonardoGentile
Copy link

@yo1dog your solution (mac) seems to create an infine loop for me

You running with BASH or ZSH? I found that BASH does not allow for local outside of functions which causes an error and an infinite loop as DEFAULT_NODE_VER is empty and [ -s ... ] on a directory always returns 0.

I updated the script to remove local so it should work under BASH as well.

On bash, that why 👍 thanks

@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