Manage node versions with brew

TLDR #

brew install node node@10 node@8 node@6 # latest, and all LTS versions at time of writing

brew info node@10 # gets the $PATH variable you'll need to use node v10. You will only need the following line:

export PATH="/usr/local/opt/node@10/bin:$PATH" # Tada! In this shell session only, node v10 is the executable that will be used first!

If you’re like me, you like to keep your system’s package versions up to date with the latest features and security patches as vigilantly as possible. So if you’ve had to work with multiple node versions on one development machine, you may have had a hard time keeping both these worlds in sync. Today I learned/pieced together a way to keep myself sane after dealing with node/npm permission issues for the last damn time. This also has only been possible relatively recently (at time of writing) since node@X versions have only been recently published as keg-only formulae on brew (keg-only is important).

I have worked with nvm and n and I don’t want to disparage either because they do work, and even work well at scale, but both have their flaws IMHO. Since brew manages all my packages for me, why not keep using it?

What is a keg-only formula? #

From brew.sh:

It means the formula is installed only into the Cellar; it is not linked into /usr/local. This means most tools will not find it. We don’t do this for stupid reasons. You can still link in the formula if you need to with brew link.

Essentially this means that you can have as many versions of node installed on your computer without them conflicting with each other, and as a bonus, your npm installation! Tangentially (from my experience), this is another problem that has been solved relatively recently, since npm@latest has become much more stable across versions of node.

Now, run the commands at the top of this article and you should be good to go! Remember you will need to export your $PATH variable in every shell session you want to use a different version of node from the default version. #

If you want to set a different version of node as your default system version: #

brew info node@6 # for example

echo 'export PATH="/usr/local/opt/node@6/bin:$PATH"' >> ~/.zshrc # modify your bashrc or zshrc with this line to have that version be the default!
 
2
Kudos
 
2
Kudos

Now read this

Writing Development Kit (WDK)

If you know about grunt and you’re like me and do a lot of your writing in Markdown, I’ve put together a “Writing Development Kit”. As far as I know no-one has beaten me to it, so I’m coining that phrase here and now! I’m using Brian... Continue →