Before you begin, ensure you have NVM installed on your system. If you haven’t installed NVM yet, you can install it using the following commands:
- Open your terminal.
- Run the installation script:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
- Restart your terminal or run
source ~/.bashrc(or equivalent for your shell) to apply the changes.
Step 1: Installing Node.js Using NVM
To install a new Node.js version, open your terminal and use the command:
nvm install [version]
[version] with the Node.js version number you wish to install. For example:
nvm install 20.11.0
Step 2: Listing Installed Node.js Versions
To see all the Node.js versions installed on your system through NVM, use:
This command will display a list of all installed Node.js versions.
Step 3: Setting a Default Node.js Version
After installing the required Node.js version, you can set it as the default version using:
nvm alias default [version]
For example, to set version 20.11.0 as the default:
nvm alias default 20.11.0
This command sets the specified version as the default for new shells. This means whenever you open a new terminal session, it will automatically use the default Node.js version you’ve set.
Step 4: Verifying the Default Node.js Version
To ensure that the default version has been set correctly, open a new terminal window and check the Node.js version with:
This command should display the Node.js version number you set as default.
Managing multiple Node.js versions is a common requirement for developers. NVM provides a flexible and efficient way to handle different Node.js versions. By following these steps, you can easily set a default Node.js version, ensuring consistency across your development environment. Remember, you can always switch to a different version for specific projects using nvm use
For more advanced usage of NVM, such as setting version-specific global packages or automating version switching, you can refer to the NVM GitHub documentation. Happy