There are many ways for you to install and set up Node.js. This guide would you how you can best set up Node.js on your Mac as a developer.
There are three you can do the installation.
- Using the Node Version Manager (NVM)
- Using Homebrew
- Download the installer from the official website.
The recommended way I would suggest is to use the Node Version Manager (NVM). The reason is that it allows you to quickly and easily switch between versions of Node.js in your workflow. I have been working with Node.js very actively since v4.X around 2015. You might notice that there have been many major releases since then. From my experience, new major releases often come with breaking changes, particularly with NPM package compatibility.
New major versions are released approximately twice a year! If you are starting a project with a current version, chances are that it would be outdated by your next project. There are LTS (Long Term Support) versions of Node.js that have a longer shelf life. However, even those have compatibility issues between them such as between v14 and v16 (which are both currently in maintenance LTS and active LTS respectively as of this post).
As a developer who had to switch between versions between projects throughout the years, I would suggest installing it using NVM. You could say that it's best to just always keep your code up to date and compatible with the latest LTS versions. However, such task is not something that can often be prioritized in project development. Furthermore, there are often NPM package incompatibility that are very hard and inconvenient to work around.
Installing using Node Version Manager (NVM)
The easiest way to install NVM is to use Homebrew. Instead of installing Node.js using Homebrew directly. You'll use it to install NVM which then installs the selected Node.js version you need. Run in your terminal the command:
$ brew install nvm
Next, just follow the on-screen instructions from Homebrew. The instructions would typically be to:
1. Create the
$ mkdir ~/.nvm
2. Add to ~/.zshrc or your desired shell configuration file:
export NVM_DIR="$HOME/.nvm" [ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh" # This loads nvm [ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
Select a version
You can install and select any version you want. For this, we would just install the latest LTS version. It is often good to standardize to using the current LTS version instead of the latest version as they are active and maintained for longer. It is also good for stopping you or your team from using too new features from the latest version that often times break compatibility with previous versions (this happens).
$ nvm install --lts
Now, all you need to do is to check that it is installed and set to the correct version.
$ node -v v16.15.0
Installing using Homebrew
The other way to install Node.js to do it directly using Homebrew, instead of using Homebrew to install NVM just for it to install Node.js, as in the previous section. This could also be better if you are new to Node.js development. That is, if you find that there is too many versions and you just want something to get you started. In that case, you can just let Homebrew decide the version for you.
This method is extremely simple. Just run:
$ brew install node
Then, just check that you have it installed
$ node -v v18.2.0
If you do want to switch versions, then you can change in your Homebrew command
node to something like
node@14. However, do note this way of selecting
versions is reportedly buggy. If you are going to go though the hassle of selecting versions this
way, you are likely better off using NVM.
Installing using the Official Installer
This last way of installing Node.js is likely the most common. This is for if you like something
official and do not want to go through a package manager. It's very simple and I think
needs no explanation. Go to the official website, and download
the latest LTS version which would download the
.pkg file you required. Then, just
run it and just follow the instructions for the graphical user interface.
Once that is done, just check in your terminal that it is installed.
$ node -v v16.15.0