Setting Up Node.js on MacOS Using NVM
How to Install Node.js on MacOS Using NVM?
Starting your JavaScript journey requires setting up your development environment. One of the essential tools is Node.js. In this tutorial, we’ll install Node.js on MacOS using Node Version Manager (NVM), which offers more flexibility and ease in managing multiple Node.js versions.
$ nvm use 16
Now using node v16.9.1 (npm v7.21.1)
$ node -v
v16.9.1
$ nvm use 14
Now using node v14.18.0 (npm v6.14.15)
$ node -v
v14.18.0
$ nvm install 12
Now using node v12.22.6 (npm v6.14.5)
$ node -v
v12.22.6
Step 1: Install Homebrew
Homebrew is a free and open-source software package management system that simplifies the installation of software on MacOS. Open your terminal and run the following command:
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Step 2: Install NVM using Homebrew
With Homebrew installed, now proceed to install NVM by running:
$ brew install nvm
Step 3: Configure NVM
If the ~/.nvm
directory was not created during installation, create a directory for NVM to store Node.js versions:
$ mkdir ~/.nvm
Next, configure the shell to include the necessary NVM directories. Add the following lines to your shell profile (~/.zshrc
, ~/.bash_profile
, or ~/.profile
depending on your shell):
export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && \. "/usr/local/opt/nvm/nvm.sh" # This loads nvm
[ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/usr/local/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
Step 4: List Available Node.js Versions
To list the available stable versions of Node.js for installation on your local machine, run the following command:
$ nvm ls-remote --lts
You will see something similar to the list below:
...
v16.18.0 (LTS: Gallium)
v16.18.1 (LTS: Gallium)
v16.19.0 (LTS: Gallium)
v16.19.1 (LTS: Gallium)
v16.20.0 (LTS: Gallium)
v16.20.1 (LTS: Gallium)
v16.20.2 (Latest LTS: Gallium)
Step 5: Install Node.js using NVM
Now you’re ready to install Node.js. In your terminal, for example run:
$ nvm install 16.20.2
This command installs the latest Long Term Support (LTS) version of Node.js.
Step 6: Reload Your Shell Configuration
After configuring NVM, it’s essential to reload your shell configuration to reflect the changes. The command to reload the configuration depends on the shell you are using. Below are the commands for different shells:
# for zsh
$ source ~/.zshrc
# for bash
$ source ~/.bash_profile # or source ~/.bashrc
# for fish
$ source ~/.config/fish/config.fish
Alternatively, you can open a new terminal window, and the changes will take effect.
Note: Ensure you use the command corresponding to the shell scripting
environment installed on your machine.
Step 7: Verify Installation
Verify the installation by checking the Node.js and npm versions:
# for example
$ node --version
v16.20.2
You should see the installed Node.js version displayed in your terminal.
With this setup, you are now ready to start your JavaScript development journey on MacOS using NVM to manage your Node.js versions. Stay tuned for the next tutorial, where we will dive into JavaScript variables and their usage.
Feel free to revisit this tutorial whenever you need to set up Node.js on a new machine or want to switch Node.js versions using NVM.
Happy coding! 🔥🚀