Installing nvm for n users

I use n to manage my system/default NodeJS, but if I’m in a legacy repo (hint: pretty much every repo where I work) I’ll need to drop into an old version of NodeJS just for that terminal.

Here’s how I install and use nvm. Based on the original instructions at: github.com/creationix/nvm

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh
cd ~/.nvm
git remote rm origin
git remote add origin git@github.com:creationix/nvm.git

Keep nvm from making you use a weird version of Node:

nvm alias default system

Don’t use nvm’s init script, use this instead:

if [ -d ~/.nvm ]; then
  export NVM_DIR="$HOME/.nvm"
  [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
  [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
else
  alias nvm='[ -f .nvmrc ] && n $(cat .nvmrc) || echo "MISSING: .nvmrc"'
fi

Tools like Husky will look for NVM environment variables. So for my non-work machines, I make sure none of them get set.

So what do I normally do if I need to do something in an old version of Node? This only comes up for me if a TravisCI builds for an old version. If the fix isn’t obvious, I’ll do n 4 to get into Node 4, do my fix, then switch back to LTS.

Leave a Reply

Your email address will not be published. Required fields are marked *