Table of Contents
Introduction
Setting the default Node version using nvm (Node Version Manager) allows you to easily switch between different versions of Node.js on your system. This can be useful when working on projects that require different Node versions or when you want to test your code against different Node versions. In this guide, we will walk through the steps to set the default Node version using nvm.
Related Article: Advanced Node.js: Event Loop, Async, Buffer, Stream & More
Step 1: Install nvm
To set the default Node version using nvm, you first need to have nvm installed on your system. If you haven't installed nvm yet, follow the official installation instructions for your operating system:
- For Linux/OS X: https://github.com/nvm-sh/nvm#installation-and-update
- For Windows: https://github.com/coreybutler/nvm-windows#installation--upgrades
Make sure to choose the installation method that is appropriate for your operating system.
Step 2: List Available Node Versions
Once nvm is installed, you can list the available Node versions on your system by running the following command in your terminal:
nvm ls-remote
This will display a list of all the Node versions that are available for installation using nvm. Take note of the version number that you want to set as the default.
Step 3: Install the Desired Node Version
To install a specific Node version using nvm, you can run the following command in your terminal, replacing <version>
with the actual version number you want to install:
nvm install <version>
For example, to install Node.js version 14.17.1, you would run:
nvm install 14.17.1
Wait for the installation process to complete. Once the installation is finished, you can verify that the Node version has been installed by running:
node --version
This should display the version number of the Node installation.
Related Article: Fix The Engine Node Is Incompatible With This Module Nodejs
Step 4: Set the Default Node Version
To set the default Node version to be used by nvm, you can run the following command in your terminal, replacing <version>
with the version number you want to set as the default:
nvm alias default <version>
For example, to set Node.js version 14.17.1 as the default, you would run:
nvm alias default 14.17.1
Now, whenever you open a new terminal session or restart your computer, the default Node version will be automatically set to the version you specified.
Step 5: Verify the Default Node Version
To verify that the default Node version has been set correctly, you can run the following command in your terminal:
node --version
This should display the version number of the default Node installation that you set.
Alternative Method: Using .nvmrc
File
Another way to set the default Node version for a specific project is by using an .nvmrc
file. This file should be placed in the root directory of your project and contain the desired Node version number.
To set the default Node version for a project using an .nvmrc
file, follow these steps:
1. Create a file named .nvmrc
in the root directory of your project.
2. Open the .nvmrc
file in a text editor and enter the desired Node version number (e.g., 14.17.1
).
3. Save the file.
Now, whenever you navigate to the project directory using the terminal, nvm will automatically switch to the Node version specified in the .nvmrc
file.