Having trouble switching between different versions of Node.js as you build your project? Let Node Version Manager (NVM) save the day. Install NVM on Windows and save yourself the hassle of managing multiple versions .js Node.
Installing and using NVM can be a bit tricky, especially on Windows. But in this tutorial, you will learn how to install and use NVM on Windows to install different versions of Node.js.
Read on and switch between versions .js Node seamlessly!
This tutorial will be a hands-on demonstration. To follow, make sure you have the following
- A Windows 10 computer with administrative privileges
- : This tutorial uses Visual Studio Code (VS Code), but you can use any code editor you’re comfortable with.
. A code editor
Related: Visual Studio Code tutorial worth learning
how to download and install NVM on Windows
Switching between versions .js node shouldn’t be complicated, and NVM is up to the task. But before you can use NVM, you need to download and install it on your machine.
To download and install NVM:
1. Open your favorite web browser, navigate to the official download page on GitHub, and search for the latest version of the installer. At this time of writing, the latest version is 1.1.9.
2. Scroll down to the Assets section of the GitHub page and click nvm-setup.exe to download the NVM installer for Windows.
3. Once downloaded, double-click the installer to run it.
4. Now, choose the I Agree option in the Agreement and click Next to accept the license agreement.
5. Select a location to install NVM and click Next.
Consider the location, as you will need it later if NVM is not working properly.
6. On the next screen, keep the settings as they are and click Next. This configuration allows you to set the symlink directory, which is used to access NVM from the command line.
7. Then click Install to begin installing NVM.
The installation process starts and a status bar indicates progress
8. Once installed, click Finish to close the installer.
At this point, NVM is now installed on your Windows machine
9. Finally, open PowerShell as administrator and run the following command to check the NVM version installed.
Related:Find out how to run PowerShell as an administrator
is installed correctly, you will see the NVM version number printed on the screen, as shown
. Setting the PATH Environment Variable for NVM The nvm command should successfully return the installed version
. But what if you get an error instead, like the one below?
This error indicates that the PATH environment variable is not set correctly. The PATH environment variable is what tells your computer where to look for executable files like nvm.exe.
Environment Variables: The
To fix this error, add the location of the NVM executable file to the PATH: 1 environment variable
. Run the following command to set the PATH environment variable for NVM.
Replace INSTALL_PATH with the path of the NVM executable file that you noted in step five of the “Download and install NVM for Windows” section
The following output shows that setting the PATH environment variable for NVM is successful
A restart may be required for the new PATH variable to take effect
2. Now, run the following command to check the NVM version number again.
As you can see below, the nvm command now works to check your NVM version.
Using NVM to install multiple versions of
Now that you have NVM installed and configured, you can use it to install multiple versions of Node.js side by side on your machine
This feature is useful when working with projects that require different versions of Node.js. For example, you can test a module you are developing without uninstalling the stable version of Node.js.
Installing the latest version of Node.js
When installing tools or any package, you would typically choose to get
the latest version.
Run the following command to download and install the
latest version of Node.js.
You will see output similar to the following, showing the latest version of Node.js (19.0.0) installed on your machine.
Installing the latest LTS version of Node.js The latest
version of Node.js
is not always the best choice for production environments. Why? The latest version may introduce major changes or may be unstable.
What are your other options for installing Node.js? The Long-Term Support (LTS) version of Node.js is the most recommended for production environments. The LTS version is more stable and well-tested than the latest version.
Run the following command to install the LTS version (lts)
You can then see the latest LTS version of Node.js installed, which is 18.12.0 at this time of writing. Installing
a specific version of Node.js What if you need to install
a specific version of Node
.js? Don’t worry. NVM allows you to choose which version of Node.js install without overwriting those currently installed on your machine.
Installing a specific version of Node.js
is often necessary when working on an older project that is incompatible with the newer version of
Node.js. Run the following command to list all versions of Node.js that are available for installation via NVM. You will see a
list of all versions of Node.js that are available
for installation below. Choose a version that you want to install.
Now, run the following command to install Node.js v16.18.0 on your machine. But you can replace v16.18.0 with your preferred version.
Switching between multiple versions of
Perhaps you are working on a project that requires Node.js v16.x and another project that requires Node.js v18.x. If so, how do you switch between versions .js Node?
Fortunately, you can use NVM to switch between these two versions as needed quickly.
1. Run the following command to see which version of Node.js is currently (current) in use.
The output then shows the current version .js Node in use is v16.18.0. Now all you need is the version you want to switch to. But first, you need to check if that version is also installed on your machine.
Arabic numeral. Next, run the following command to list all versions of Node.js that are currently installed on your machine. NVM List
You will get output like the following, showing all installed Node.js versions
3. Finally, run the nvm command use below to switch to Node.js version 18.12.0. Replace 18.12.0 with the actual version number .js node you want to switch to.
Running a simple Hello World program Now that you
have Node.js installed on your machine, you can try running a simple Hello World program to verify that everything works as expected
In your preferred text editor, add the following code to the file and save and close the editor
The following code prints the Hello World string to the console.
Now, run the
node command below to run the *hello-app.js* script
If all goes well, you’ll see the Hello World message printed on the console, as shown below. This output confirms that Node.js has been successfully installed on your machine.
Running and managing multiple versions of Node.js on the same machine can be a hassle. But in this tutorial, you learned how to use NVM to install and manage different versions of Node.js on your machine.
Now that you’ve figured out how quickly you can switch between different versions of Node.js, would you consider NVM to level up your workflow?