When working with multiple Node.js projects or needing to switch between different versions of Node.js on your development machine, managing the Node.js version can become a challenge. However, with the help of Node Version Manager (nvm), you can easily switch between Node.js versions and set a default version for your projects. In this tutorial, we will walk you through the process of setting the default Node.js version using nvm.
Prerequisites
Before proceeding, make sure you have the following prerequisites:
- Node Version Manager (nvm) installed on your system. If you haven’t installed it yet, you can follow the instructions in the official nvm repository: nvm on GitHub.
Steps to Set the Default Node.js Version
Step 1: List Available Node.js Versions
To set the default Node.js version, you need to know the available versions installed on your system. Open your terminal and run the following command to list the installed Node.js versions:
nvm ls
This command will display a list of installed Node.js versions along with an indicator of the currently active version.
Step 2: Set the Default Node.js Version
Once you have identified the desired Node.js version, you can set it as the default version using the nvm alias
command. Replace <version>
with the Node.js version you want to set as the default.
nvm alias default <version>
For example, if you want to set Node.js version 14.17.0 as the default version, run the following command:
nvm alias default 14.17.0
Step 3: Verify the Default Node.js Version
To confirm that the default Node.js version is set correctly, run the following command:
node --version
This command will display the Node.js version currently set as the default.
Step 4: Testing
To test if the default Node.js version is working as expected, you can create a new directory and initialize a new Node.js project. Run the following commands:
mkdir my-project
cd my-project
npm init -y
This will create a new directory named “my-project,” navigate to it, and initialize a new Node.js project with default settings.
Step 5: Verify Node.js Version for the Project
To verify that the correct Node.js version is being used for the project, run the following command within the project directory:
node --version
The output should match the default Node.js version you set earlier.
Additional Tips
Switching Node.js Versions
If you need to switch to a different Node.js version for a specific project, you can use the nvm use
command. This command allows you to temporarily use a specific version without changing the default version. For example, to use Node.js version 12.22.0 for a project, run the following command within the project directory:
nvm use 12.22.0
Updating Node.js
You can update the installed Node.js versions using nvm. To check for available updates, run the following command:
nvm ls-remote
This command will list the remote Node.js versions. To update nvm and install the latest version of Node.js, run:
nvm install node --reinstall-packages-from=<version>
Replace <version>
with the Node.js version you want to install. For example, to install the latest version, run:
nvm install node --reinstall-packages-from=node
Removing Node.js Versions
If you want to remove an installed Node.js version, use the following command:
nvm uninstall <version>
Replace <version>
with the Node.js version you want to remove. For example, to uninstall Node.js version 10.15.3, run:
nvm uninstall 10.15.3
Default Node.js Version across Terminal Sessions
The default Node.js version set using nvm applies to the current terminal session. If you want to set the default version globally, meaning it remains the default across all terminal sessions, use the nvm alias default <version>
command without opening a specific terminal.
nvm alias default <version>
This will set the default Node.js version globally, and it will be applied to new terminal sessions.
Conclusion
With nvm, managing and switching Node.js versions becomes a breeze. By setting the default version, you ensure that the correct version is used for your projects, avoiding compatibility issues. Additionally, nvm provides flexibility in switching versions and updating Node.js installations. By leveraging these capabilities, you can work effectively with different Node.js projects and take advantage of the latest features and improvements in the Node.js ecosystem.