Node.js is a popular JavaScript runtime environment that is widely used in web development. As developers work on different projects, they often need to switch between various Node.js versions. This is where Node Version Manager (NVM) becomes a game-changer. NVM allows you to install multiple versions of Node.js and easily switch between them. In this article, we will guide you through the process of setting a default Node.js version using NVM, ensuring a seamless development experience.
Prerequisites
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]
Replace [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:
nvm ls
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:
node -v
This command should display the Node.js version number you set as default.
Conclusion
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
Additional Resources:
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