Like any good developer, I use Homebrew to manage my softwares, compilers, and so on. I've also been using the brew
installation of node
and npm
for quite sometimes now.
But every time I tried to update all the packages installed globally with npm
, I had the same error saying that npm
could not update itself.
Some research on Google brought me to issue #22408. As you can see, this bug is quite common and even if there are different mildly working workarounds...
"...Package managers managing package managers rarely works out well..." - janv
I first tried installing node
--whithout-npm
, but I found out that I don't really like partial management as I often forget what I need to install when setting up a new machine, npm
in that case...
Until...
Node Version Manager
For those familiar with Ruby, it is highly recommended to use a Ruby Version Manager such as rbenv or rvm. It works like a charm when you need to manage different versions of Ruby and/or don't want to screw up the system version of Ruby.
It turns out that something similar exists for node
! It is called nvm
for Node Version Manager. The good news is that you can install nvm
using brew
which is exactly what I needed!
So let's get started !
First, pull the latest version of brew
by running:
$ brew update && brew upgrade
Then, we simply need to install nvm
:
$ brew install nvm
Finally, we need to add the following line to our .profile
, .bashrc
or .zshrc
, to be able to run nvm
from the command line:
$ echo "source $(brew --prefix nvm)/nvm.sh" >> ~/.zshrc
And that's it!
Installing node & npm
Installing nvm
is not enough, we now need to install node
& npm
.
To print out the list of all the available versions of node, simply run:
$ nvm ls-remote
We want to install the current stable version which is v0.10.26, so we need to run:
$ nvm install -s v0.10.26
The -s
flag means we want to compile node
from source.
When the installation process is finished, our brand new node
and npm
will be installed!
The great thing with nvm
is that you can use a different version of node
in each of your shell instances. But if you want to stick to the latest version, you can set it as default
by running:
$ nvm alias default 0.10.26
Make sure everything is up and running by typing node -v && npm -v
.
Conclusion
That's all folks! You can now manage different versions of node/npm
without even thinking about it. :)