Introduction
Before starting with Node Version Manager aka NVM, a BIG THANKS to Corey Butler and Team, who made this cool product possible for windows users. This tool is intially can only be used on Mac and Linux machines. This tool is extended for Windows machines with lot of efforts and contribution from community. Kudos to them.
Now lets get into the basics :). NVM is the tool, that allows you to download and install multiple node versions and isolates the projects and packages without affecting their functionality. This way installing or uninstalling a particular node version will not disrupt the packages and runtime. This article contains installation instructions for windows 10. Same steps can be performed on windows 11 as well.
Why NVM?
When working with SPFx, you may get into situation where the solution needs to be tested in Node LTS version (Long Term Support). Imagine you have developed an SPFx solution using node 6 and deployed the package in production. Now you are on node 10. You need to implement an enhancement for the solution developed in Node 6. There are 2 ways to achieve it, the harder way and smarter way.
The harder way is to uninstall the current node version (which is 10 in this case) and install the node version 6 and develop the enhancement build and package it. In this case if you want to move to latest version of node, you need to again uninstall the legacy and install the latest version. Switching between node version is bit cumbersome and it is required to uninstall / Install manually to switch back to required node version.
The smarter way is to install the NVM (Node version Manager). This tool when installed on your machine, it will switch to required node version with simple one liner command. How cool is it? 😊
This tool manages all legacy and newer node versions independently and manages the runtime for you on the fly.
As you seen in the above picture, NVM can maintain multiple versions of nodes in the same environment/machine. Each version of node can have their node runtime, tools, and generators.
Steps to install NVM
Installing NVM on windows 10 is straightforward. All you need to do is download the MSI package and install it. Before installing NVM, validate that there is no previous version of NVM installed.
In the screenshot, it shows NVM is not recognized as an internal or external command.
Step 1
Go to the below link and get the latest version of NVM (Node Version Manager)
https://github.com/coreybutler/nvm-windows/
Step 2
In the resulting folder, go to the assets section and click on the nvm-setup.zip file.
Step 3
Download the file and unzip it and right click on the installation file and select ‘Run as Administrator’. Please note that it is required to have local admin rights or privileged admin rights to have the proper setup complete.
Step 4
Select ‘I accept’ and click ‘Next’.
Step 5
You can either change the location of the folder or change it to your required one.
In some cases the node installers may not have elevated rights to default location which is c:\users\username\AppData\Roaming\nvm. In this case i have changed to c:\install\nvm
Step 6
In next window, it will ask for location of symlink that needs to be created for node.js. Symlink stands for symbolic link which is used to load the required node modules for your project during the runtime.
In some cases the nvm may not have access to default folder path to create packages in this case c:\Program Files \nodejs.
Step 7
Finally click on ‘Install’ and that finishes your installation of NVM on windows 10.
Validation
After the installation of NVM, simply open the command prompt and type nvm
Now you should see it will say the version it is running, and also gives some helpful commands to get started.
Validation
To validate the successful installation of nvm i have tried to run the following steps.
Step 1
Run the version command to get the version
nvm version
Step 2
Try installing specific node version. In this case i tred to install the node version v14.15.0 as this is the version supported by SharePoint framework (at the time of writing this blog).
nvm install 14.15.0
When you see the message 'Installation complete' this confims that the instllation of node version is completed successfully.
Step 3
Validate the node that is installed. After the installation complete, if you entered 'node', you might end up getting the following error
'node' is not recognized as an internal or external command. Before testing the node version, enter the below command, telling nvm to use the particular node version, in this case 14.15.0
nvm use 14.15.0
now validate the node version by entering the below command
node -v
Basic commands
Command |
Description |
nvm version |
It gives the version of NVM installed |
nvm list |
Lists the different versions of node installed |
nvm current |
Displays the current active version |
nvm install latest |
Installs the latest version of the node available in NodeJS repository |
nvm install 14.15.0 |
Installs the node version of 14.15.0 available in NodeJS repository. You can install the available node versions by specifying the node version. |
nvm install lts |
To install long term support version of node |
Check the github documentation in references for more commands.
Switching to different node version
By following above install steps, I have managed to have different node versions.
Step 1
Getting the list of available node versions, use the following command in the command prompt.
nvm list
The star denotes the active version currently being run.
Step 2
To switch to different node version for instance 14.15.0 run the following command
nvm use 14.15.0
Step 3
Validate the node package isolation. Since I am running the node 14.15.0, the generators yeoman, pnp/sp and gulp is specific to this node version only.
If I switched to different node version, observe that yo is no longer available in that version, unless manually installed.
Issues
Issue 1: could not be found or does not exist. Exiting.
When trying to install particular node version i am getting following error
c:\Users\username\AppData\Roaming\nvm could not be found or does not exist. Exiting.
Issue 2: The system cannot find the file specified
Users are getting below message when trying to install the latest version / any particular version.
Error open C:\insall\nvm\settings.txt: The system cannot find the file specified.
Issue 3: Error while creating node.zip. Access is denied. Could not download node.js
Users are getting below message when trying to install the latest version / any particular version.
Error while creating c:\users\username\vayina1\AppData\Roaming\nvm\v17.8.0\node.zip - open c:\Users\UserName\Appdata\Roaming\nvm\v17.8.0\node.zip: Access is denied.
Fix
If you end up having any of the above issue, please validate the below steps. Observe the instructions carefully as this step is very important in fixing the majority of issues being faced while installing node versions.
Step 1
Go to the folder path where the nvm is installed. The default location is c:\Users\<username>\Appdata\Roaming\nvm. In this scenario i have installed under c:\install\nvm (refer to step 5 in installation section). Check if 'settings' file whcih of type text (.txt) is existing or not. If not try manually creating settings.txt file with following details. Please note that you may have to update the values based on setting the installation path for nvm and symlink.
#replace the root value with nvm install folder path
root: C:\install\nvm
#replace the path value with symlink install folder path
path: C:\instsall\nodejs
finally save the file.
Issue 4: Unable to switch to different node version:
When trying to switch to different node, I am getting the following error.
exit status 1: You do not have sufficient privilege to perform the operation.
Fix: This is due to the normal command prompt did not have privileges to switch the runtime. Please open the command prompt in administrative mode.
Conclusion
Thus in this article, we have seen what NVM is and why it is required, and how to install nvm and how to manage different node versions using NVM.
References