Taming Node — Part 1: Managing node.js versions effectively with NVM
An introduction to node version manager
In this series of articles, we are going to take a deep dive into NVM and node version and also package management. We will look at using nvm to manage the versions of Node.js on a local development environment. Advanced techniques to easily switch versions, closely followed by some techniques that will help keep packages up-to-date and a project that’s cared for.
Introduction
As a modern JS developer, managing different Node versions can become a daunting task. This is especially difficult when working across multiple projects. This article will guide you through using Node Version Manager (NVM) to help deal with this more effectively.
I will follow up article that will cover other essential tips for managing package dependencies using npm and yarn. I’ll link to that when it’s complete so this is a warning that those topics won’t be covered in this article.
Why Use NVM?
If you have ever struggled when jumping between projects and having to remove or change versions of Node.js then NVM is the tool you have been looking for. NVM is a powerful tool that allows you to easily install, manage, and switch between different versions of Node.js in your development environment. This is particularly useful when working on multiple projects that require different Node versions or when testing your applications against the latest releases of Node.js.
Key Benefits of Using NVM:
Version Flexibility: Seamlessly switch between different versions of Node.js without having to manually uninstall and reinstall.
Project Isolation: Each project can use its specific Node.js version, reducing conflicts and ensuring compatibility. This is helpful when considering modern deployment stacks as well. Often a CI/CD pipeline will require a specific version of Node during its build. If there is variation between local and pipeline there can sometimes be issues.
Ease of Use: Simple commands to install, remove, and switch Node.js versions.
Dealing with previous Node installs
I would advise removing any prior node installs before starting. It doesn’t always cause issues but it has done more often than not in my experience. Removing existing versions will depend on how Node.js was initially installed. Some tips are:
Removing from macOS/Linux:
If you have installed Node.js via homebrew then it is a simple case of uninstalling it with the following command.
brew uninstall node
If you have used other means to install it then run the following to help clean it up
sudo rm -rf /usr/local/bin/node /usr/local/bin/npm
sudo rm -rf /usr/local/lib/node_modules
Removing from Windows:
If Node.js was installed using the official Node.js installer:
Go to Control Panel > Programs and Features, find Node.js in the list, and click Uninstall.
If Node.js was installed using Chocolatey:
choco uninstall nodejs
Cleaning Up Environment Variables:
After uninstalling Node.js, it’s important to remove any remaining paths related to Node.js from the system’s environment variables.
On macOS/Linux:
Open a shell profile in a text editor (e.g., ~/.bashrc, ~/.zshrc) and remove any lines that reference Node.js or npm.
Reload the shell profile:
source ~/.bashrc # or source ~/.zshrc for Zsh
On Windows:
Open System Properties > Environment Variables, locate Path, and remove any entries related to Node.js (e.g., C:\Program Files\nodejs\).
Ok, so we should be all clean and in a good place to start.
Setting Up NVM
Firstly we can check to see if nvm is already installed by using the following command.
nvm version
Installation on macOS/Linux
It’s worth checking the website for the most recent version https://github.com/nvm-sh/nvm/releases then replace the <MOST_RECENT_VERSION> part of the following code with the version you want. For example v0.40.0
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/<MOST_RECENT_VERSION>/install.sh | bash
# or we can use wget
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/<MOST_RECENT_VERSION>/install.sh | bash
If we are using homebrew:
brew install nvm
Installation on Windows
Download and run the Installer:
To install on a Windows machine we need to visit nvm-windows GitHub releases page download the latest nvm-setup.exe file and run it locally. Following the installation steps.
Post-Installation Verification
After installing NVM on our system, it’s important to verify that the installation was successful. This step ensures that NVM is correctly configured and ready for use. Verifying the installation will also help confirm that the nvm command is accessible from your terminal, which is pretty important for managing Node.js versions effectively.
macOS/Linux:
Open a new terminal window to load the NVM environment variables and settings. If we’re using a shell like Bash, Zsh, or Fish, we need to make sure that the shell profile (~/.bashrc, ~/.zshrc, or ~/.config/fish/config.fish) has been reloaded with the necessary NVM configurations.
We can also manually reload our shell profile by running:
source ~/.bashrc # or source ~/.zshrc for Zsh
Windows:
Open a new Command Prompt or PowerShell window. This ensures that the PATH and environment variables set by the NVM installer are loaded into the session.
Verify NVM Installation
To confirm that NVM is installed correctly, we run the following command in the terminal:
nvm version
This command should output the current version of NVM installed. For example:
0.39.7
Installing and using node versions
After verifying NVM is set up and working correctly, the next step is to install a version of Node.js to ensure that everything is working as expected:
nvm install --lts
nvm use --lts
This command installs the latest Long Term Support (LTS) version of Node.js and switches the environment to use this version. I’ll let ChatGPT explain why using LTS is a good thing.
LTS Support — Using LTS (Long Term Support) versions of Node.js is important because these versions are maintained with security updates and critical bug fixes for an extended period, ensuring greater stability and reliability for production applications.
We can now run npm -v
and node -v
to check that the correct version is being used. Which should result in something similar to the following.
❯ npm -v
10.5.0
❯ node -v
v20.11.0
~ ❯
Next, we can install a specific version so we can switch between the two:
nvm install 18.17.1
Now using node v18.17.1 (npm v9.6.7)
Once the package has successfully downloaded, if it hasn’t already switched, we will be able to switch to it by using the following command:
nvm use 18.17.1
Now using node v18.17.1 (npm v9.6.7)
We can now switch back to the LTS version.
nvm use --lts
To list which versions are installed we can use the following:
nvm list
v14.16.1
v16.13.0
v16.14.0
v16.14.2
v16.15.0
v16.19.0
v16.20.1
v18.14.0
v18.15.0
v18.17.0
v18.17.1
v18.20.2
v18.20.3
v18.20.4
v20.10.0
v20.11.0
v20.13.1
v20.14.0
v20.15.1
-> v20.16.0
v21.6.1
default -> 20.11.0 (-> v20.11.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v21.6.1) (default)
stable -> 21.6 (-> v21.6.1) (default)
lts/* -> lts/iron (-> v20.16.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.2 (-> N/A)
lts/hydrogen -> v18.20.4
lts/iron -> v20.16.0
This shows the versions available as well as identifying our currently used version with an arrow.
Conclusion
Managing Node.js versions effectively is crucial for any JavaScript developer, especially when working across multiple projects or teams. Using NVM simplifies the process, offering flexibility, project isolation, and ease of use. By mastering NVM, we not only streamline our local development environment but also ensure consistency across different stages of the project lifecycle. NVM is a great tool for any Node.js developer looking to manage their development environment with greater efficiency
In future articles, we will discuss how to automate the switching of the Node.js version to increase productivity and reduce project friction even further. Stay tuned.
References
This article was helpfully checked and corrected into something resembling non-Northern English by Grammarly and ChatGPT