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:

curl -o-
cd ~/.nvm
git remote rm origin
git remote add origin

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_DIR/"  # This loads nvm
  [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
  alias nvm='[ -f .nvmrc ] && n $(cat .nvmrc) || echo "MISSING: .nvmrc"'

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 *