Skip to content

Instantly share code, notes, and snippets.

@mjj2000
Last active August 8, 2016 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjj2000/f232b196a7299fe61be94cb3b094a1f3 to your computer and use it in GitHub Desktop.
Save mjj2000/f232b196a7299fe61be94cb3b094a1f3 to your computer and use it in GitHub Desktop.
Switch to target node version of project by nvm automatically(only when .nvmrc exist in current folder)
# put this snippet in your shell config(ex: ~/.bashrc, ~/.zshrc, ...)
# make sure `.nvmrc` exsits in your project folder
function init-node {
# switch node version by nvm after changing folder without AVN
if [[ -f ".nvmrc" ]]; then
# initialize nvm if necessary
if [[ -z $(nvm 2>/dev/null) ]]; then
# nvm is not initailized
echo 'initialize nvm ...'
source ~/.nvm/nvm.sh
fi
# switch to target version
if [ $(cat "./.nvmrc") != $(node --version | sed "s/v//") ]; then
nvm use
fi
fi
}
function cd {
builtin cd "$@"
init-node
}
init-node # check after entering shell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment