No matter if you are working on backend or are in the frontend camp, Node.js is getting more popular day by day and you may need to install it at some point. There is plenty of ways to install it on your operating system and I am about to show you the most popular methods and highlight pros and cons of each of them.
Pre-built installer for your platform #
Npm -v will show you the current version. Brew link -overwrite -dry-run node will force the link and overwrite all conflicting files. Brew install node do a fresh installation. Brew update; brew upgrade node; brew link -overwrite node. Node Upgrade Procedure are below. The last option installs all global npm packages over to your new version. This way packages like mocha and node-inspector keep working. N is an npm-based node version manager. Brew update brew upgrade node. MacPorts is the other package manager for Mac. The following will update the local ports tree to get access to updated.
The easiest and most obvious way is to download a pre-built installer for your platform from the Node.js downloads page. Installation itself is very straight forward — next, next, next, done.
Despite the fact it is the easiest way of installing it, it comes with many disadvantages. It locks you to a particular version — it may not be a big issue on day one but it can be in the long run if you are willing to jump between projects that depend on different versions. Another (worst) issue with this method is the fact that it requires admin permissions (sudo
) to install package globally. I published “Fix privileges and never again use sudo with npm” with some solutions for this issue, but you better check “Resolving EACCES permissions errors when installing packages globally” on official documentation or follow this article.
Node from Homebrew / Chocolatey #
“Homebrew — the best friend of the macOS user” explains why I love this tool so much. Windows users can find an alternative like Chocolatey. Both of these package managers allow you to install Node.js with ease.
CLI tools like that allow you to install / uninstall software in no time and never again be bothered by insufficient permissions. Although they allow you to manage versions too, in Node.js you should use…
NVM (Node Version Manager) #
Node Version Manager is a simple bash CLI that allows you to install multiple Node.js versions and switch between them using simple commands. Although it is available for macOS and Linux only, Node.js version manager for Windows exists (ironically it is written in Go). Installation is straightforward — you can use a simple bash script or brew
(my preferred way).
Thats the way to install the latest version of Node.js.
Be curious and explore more nvm
commands on an official nvm documentation. My most frequently used commands are: install
, uninstall
, list
and use
.
It is hard to remember the version of Node.js that a project depends on — smash the version into .nvmrc
file in the root of your project and run nvm use
inside this directory. If something can be automated, it should be automated — installing the right version of dependencies is not an exception. I made a simple bash script that runs nvm use
whenever it encounters a .nvmrc
file inside your project. There is a cool zsh-nvm plugin for Zsh users that does the same job.
Do you use nvm by @creationix to manage @node versions? Have you ever forgotten to `nvm use` in your project? I have a little bash script for y'all. Put it into your .bashrc / .bash_profile and you are all set :-*https://t.co/GhVwRKYnw7#node#nodejs#nvm#js#javascriptpic.twitter.com/jWGN89rQnA
— Paweł Grzybek (@pawelgrzybek) January 12, 2019I am glad it helped you out. Have a great day!
thank you! definitely more detailed and nicer explained than other top google ranked posts on this topic. What are the most important reasons to use nvm over brew?
Hi.
Thanks for nice words. The main reason why I use nvm is the fact that I work on multiple projects. Some of them are built with the latest node runtime, some of server-less lambda stuff that I work on is on 8.10. The ease of switching versions is extremely convenient. Other than that and if you always work with the latest version I would suggest being stick to `brew install node`.
Have a great day 🥑
Very helpful, thank you.
It should be noted that installing nvm with Homebrew is not officially supported:
Homebrew installation is not supported. If you have issues with homebrew-installed nvm, please brew uninstall it, and install it using the instructions below, before filing an issue.
Yes you are right.
The only reason why is the fact that nvm needs to be added to shell session. Official installation script does it for you. Brew installation requires a few lines of code to be added to .zshrc / .bashrc / .whareverrc...
Post-install message from brew formula gives a detailed instruction about this step.
Thanks for contribution. Have a fantastic day!
Thank you. Now, I can move on with my Node.js course. Best wishes.
My pleasure. Good luck in your programming career!
Thanks for share! Very useful information, just add the notes about the missing lines in the ~/.bash_profile that we should add when we install nvm through brew
Thanks a ton for suggestion. I will update this article at some point. Have a great day Enrique 🥑
What'ya think?
In this tutorial we will learn to install NodeJS and NPM on Mac using Homebrew.
Step 1: Install Homebrew
Homebrew is 'The missing package manager for macOS'.
Open terminal and type the following command.
This will install Homebrew on your Mac. To check the version type the following command.
Output
Why use Homebrew to install NodeJS?
If you are installing NodeJS via the installer from https://nodejs.org/ then you have to use sudo
to make sure that it installs correctly. After that you have to make changes in your system $PATH
by adding the path of the node executable. And if you want to uninstall node then you have track all the files that were created and get rid of them. In short its a long process.
That's why Homebrew is used. It makes the job easy. It will install/uninstall Node easily.
Step 2: Install Node via Homebrew
In the terminal type the following command to install Node.
If everything installed successfully then you can type in the following command in the terminal to check the Node and NPM version.
Possible issues/errors that may occur
If you look above you will see that we have some issues.
So, in terminal type the following command.
We also got an error 'Error: The `brew link` step did not complete successfully'.
So, type the following command.
Output
The above output says:
You may want to remove it:
rm '/usr/local/share/man/man1/node.1'
So, use the following command.
Now, to list all files that would be deleted use the following command.
Output
Now we got the Error: Could not symlink share/systemtap/tapset/node.stp
/usr/local/share/systemtap/tapset is not writable.
To fix this we have to change the username and group of the systemtap directory.
Type the following command in the terminal.
Brew Npm Update
Output
Now, run the --overwrite --dry-run command again.
Output
Now, we got the Error: Could not symlink lib/dtrace/node.d
/usr/local/lib/dtrace is not writable.
To fix this we have to change the username and group of the dtrace directory.
In the terminal type in the following command.
Output
Now, run the --overwrite --dry-run command again. Hopefully you will get no error. So, its time to run the --overwrite command.
Brew Npm Not Found
Output
Brew Uninstall Npm
Congratulation! You have successfully installed NodeJS and NPM on your Mac via Homebrew.