NVM for macOS in 2 minutes
When you have an application based on the Node.js architecture, the ability to switch between different versions is essential to ensure compatibility with existing projects and to take advantage of the latest platform features. However, this task can become complicated on operating systems such as macOS, where version management is not natively integrated.
What is NVM?
NVM is a command-line tool that makes it easy to install and manage multiple versions of Node.js on a single system. However, its implementation on macOS presents particular challenges due to differences in environment and dependency management compared to other operating systems because it is not included in our operating system; so we will have to install Homebrew instead.
How do we install it?
Open a terminal and install Homebrew with the following command:
/bin/bash -c "$(curl -fsSL <https://raw.githubusercontent.com/Homebrew/install/master/install.sh>)"
Once Homebrew is installed, we will proceed with NVM:
brew install nvm
For NVM to be used we always have to include it in our ~/.bash_profile
or ~/.zshrc
file:
source $(brew --prefix nvm)/nvm.sh
If we don’t know where to find these files, we can easily find them by accessing them as superuser with the sudo su command. If they don’t exist, we will create them.
Once we have NVM installed, we will proceed to install NodeJS in its latest version:
nvm install node
When the installation is finished we can install the versions of NodeJS we want. To do this we will list all the existing versions on our computer with this command:
nvm ls-remote
If we don’t know the version we want to install we can consult the official NodeJS website. Knowing the version we want to install we will type the following command:
nvm install [NUMBER OF VERSION]
## Example:
nvm install 18.10.0
Once installed, it can be used as follows:
nvm use [NUMBER OF VERSION]
## Example:
nvm use 18.10.0
If we wanted to switch back to another version we would first have to list all the versions we have installed with nvm list
. Once we locate the version we want to switch to we will use nvm use [DESIRED VERSION NUMBER]
to use it.
Conclusion
Installing NVM on macOS via Homebrew greatly simplifies Node.js version management. With NVM, you can easily install, switch and manage multiple versions of Node.js on your system, giving you the flexibility to adapt to different projects and take advantage of the latest platform features. With just a few terminal commands, you can install new versions, switch between them, and ensure that your development environment is always compatible and up-to-date. Definetly, NVM gives you full control over your Node.js development environment on macOS, allowing you to focus on creating amazing applications without worrying about compatibility issues or outdated versions.