Adding an alternative Node.js version can be helpful for managing multiple projects with different Node.js dependencies. Here are the steps to add an alternative Node.js version:
-
Install Node Version Manager (NVM):
NVM is a popular tool that allows you to manage multiple Node.js versions on a single machine. Follow these steps to install NVM:
bash
-
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
After installation, you may need to restart your terminal or run
source ~/.bashrc
(orsource ~/.zshrc
if you're using Zsh). -
Install the Desired Node.js Version:
Once NVM is installed, you can use it to install the Node.js version you want. For example, if you want to install Node.js version 14, you can do:
bash -
nvm install 14
-
Use the Alternative Node.js Version:
After installation, you can switch to the newly installed version by running:
bash
nvm use 14
You can verify the active version with:
-
node -v
-
Set Default Node.js Version (Optional):
If you want to make this version the default for new terminal sessions, you can set it as the default:
bash -
nvm alias default 14
This will ensure that Node.js version 14 is used by default when you open a new terminal window.
-
Managing Multiple Versions:
You can install and switch between different Node.js versions using NVM. For example, if you need to switch back to a different version, you can use:
bash
nvm use <version>
You can list all installed versions with:
-
nvm ls
-
Switching Node Versions Automatically per Project (Optional):
You can create a
.nvmrc
file in your project's directory with the desired Node.js version. For example, if you want to use Node.js version 14, create a.nvmrc
file with the content:
-
14
Then, when you navigate to that directory, you can simply run
nvm use
and it will switch to the specified version.
Remember that you should always install the versions you need from trusted sources. NVM is a widely used tool, but always be cautious about executing commands from the internet without understanding what they do.