When using NVM (Node Version Manager) to manage multiple Node.js versions, setting the default Node version is a common requirement to ensure that the specified Node version is automatically used when opening a new terminal session. The following are the steps to set the default Node version:
- Install NVM: First, ensure that NVM is installed on your system. You can check if it is installed by entering the following command in the terminal:
bashnvm --version
If NVM is not installed, you can visit the NVM GitHub page to view the installation guide.
- List Installed Node Versions: Use NVM to list all Node versions installed on your system:
bashnvm list
This will display all installed Node versions.
- Install a New Node Version (if needed): If the Node version you need is not installed, you can use NVM to install it:
bashnvm install <version>
For example, to install Node.js version 12.18.3, use:
bashnvm install 12.18.3
- Set Default Node Version: Once you have decided which version to set as the default, use the following command to set it:
bashnvm alias default <version>
For example, to set the default version to 12.18.3, use:
bashnvm alias default 12.18.3
- Verify the Setup: Close and reopen the terminal, or enter
nvm use defaultin the current terminal to activate the default version. Then, you can use the following command to verify the current Node version:
bashnode -v
This should display the default Node version you set.
These steps ensure that every time you open a new terminal session, the default Node.js version you set is automatically used. This is particularly useful in multi-project development environments, as it prevents different projects' Node version requirements from conflicting.