How to Install and Use NVM: Unleashing the Power of Node.js Version Management ๐
Saturday, Jan 11, 2025 | 7 minute read
Effortlessly manage multiple versions of Node.js with a command-line tool that simplifies installation, switching, and environment setup. Enjoy enhanced flexibility, productivity, and compatibility across various projects! ๐ง๐
1. Discovering NVM’s Potential: The Must-Know Node Version Management Tool ๐ง
In todayโs fast-paced tech landscape, especially with the rise of JavaScript and Node.js, managing different versions of Node.js has become an essential skill for developers. So, how can we efficiently manage these versions? This has become an urgent problem we need to solve.
Many developers today juggle multiple projects in their daily work, and each project may rely on a different version of Node.js. That’s where NVM (Node Version Manager) comes into play as a versatile tool! It helps developers quickly install, switch, and manage different Node.js versions, significantly boosting productivity and easily handling project requirement changes โกโจ.
2. The Unique Charm of NVM: Making Node.js Version Management Effortless โจ
NVM operates through a simple yet efficient command-line interface, allowing users to manage Node.js versions with straightforward commands ๐ฅ๏ธ. For example, you can easily install the latest version of Node.js, list all installed versions, and switch between them seamlessly, greatly enhancing development efficiencyโจ๏ธ.
Additionally, NVM is compatible with various POSIX shells, meaning you can choose your preferred terminal environment, boosting the flexibility of the toolโจ. Whether youโre working on a local machine or using a cloud development environment, NVMโs practicality will always have your back ๐.
3. Developerโs Top Choice: Why NVM Is the Best Option ๐
Installing NVM is extremely straightforward. Users can quickly download it using curl
or wget
, with setup usually taking just a few minutesโณ. During use, it can also effectively address common issues such as permission problems and version conflicts, allowing developers to focus solely on their code๐.
Moreover, NVM provides convenient support for cross-environment development, enabling developers to switch easily between different projects and environments, optimizing workflows and reducing time wasteโฒ๏ธ. All of this makes NVM the ideal companion for solving Node.js version management issues, offering developers robust and flexible tool choices ๐ ๏ธ.
Thanks to its unique advantages and comprehensive features, NVM has become an indispensable management tool for many developers dealing with Node.js versions ๐ช, allowing them to concentrate more on coding rather than wrestling with cumbersome environment setups.
4. Installing NVM ๐ ๏ธ
To start using NVM, the first step is to install it on your system! You can use two widely-recognized tools, cURL or Wget, to complete this step quickly. Now let’s take a look at the commands to download and install NVM using these tools!
Installing with cURL
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
- What does this command do? It downloads the latest NVM installation script from the specified GitHub address and executes it using
bash
. You can think of this as an automated installation process โ just input one command to get it done.
Installing with Wget
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
- The operation here is almost identical to the command above, except it uses another downloading tool, Wget. It will also download and execute the NVM installation script.
Once the installation is complete, NVM will be cloned into your home directory at ~/.nvm
and will automatically attempt to update your shell configuration file (like .bashrc
, .bash_profile
, or .zshrc
), ensuring that NVM can be auto-loaded next time you open your terminal.
To ensure NVM works correctly, remember to add the following to your configuration file:
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This line loads nvm
- Note: The two lines added inform your system where NVM is located and load NVM so that you can use it.
5. Verify Installation โ
After installation, itโs essential to verify if NVM was successfully installed! Run the following command in your terminal:
command -v nvm
- If this command outputs
nvm
, congratulations! You’ve successfully installed NVM, and you’re ready to start using it! ๐
6. Installing NVM Via Git ๐
If you already have git
installed on your system (version greater than 1.7.10), you can also choose to install NVM via Git. Hereโs a quick rundown of the steps:
-
Clone the NVM repository
cd ~/ && git clone https://github.com/nvm-sh/nvm.git .nvm
- This command will clone the NVM code into a hidden directory
.nvm
in your home directory. Ensure you’re in your home directory when executing the command to place NVM correctly.
- This command will clone the NVM code into a hidden directory
-
Check out the latest version
cd ~/.nvm && git checkout v0.40.1
- This command takes you into the
.nvm
directory and switches to the specified version (e.g., v0.40.1). This ensures youโre using the latest version of NVM or a version you know works properly.
- This command takes you into the
-
Activate NVM
. ./nvm.sh
- This command immediately loads NVM so that you can use it in your current terminal session.
To ensure NVM auto-loads every time you open a terminal, add the following to your configuration file:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This line loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This line loads nvm's bash completion
7. Using NVM ๐
Once you master NVM, youโll find installing and managing Node.js a breeze! Here are some basic commands:
Install the Latest Version of Node.js
nvm install node # "node" is an alias for the latest version
- Running this command will install the latest stable version of Node.js. Now you can start developing with the latest version!
Install a Specific Version of Node.js
nvm install 14.7.0 # Or 16.3.0, 12.22.1, etc.
- If you need a specific version of Node.js, this command allows you to easily accomplish that by just replacing the version number.
List Installed Versions
nvm ls
- This command shows you all the currently installed Node.js versions, which is immensely useful for managing various versions.
Check Available Versions
nvm ls-remote
- This command lists all available Node.js versions, assisting you in understanding your installation choices.
Set the Default Node Version
nvm alias default node # Default to the most recently installed Node version
- Knowing this command will help you easily specify which Node.js version to use by default. The default version will be used whenever you open a new terminal.
Create a .nvmrc
File to Specify a Project’s Node Version
Create a .nvmrc
file in the root of your project, containing just the desired version number. For example:
echo "lts/*" > .nvmrc # Defaults to the latest LTS version
- The advantage of this practice is that when you switch to that project, NVM will automatically read this file and help you switch to the correct Node.js version.
8. Running Tests ๐งช
Want to run some tests? First, ensure all project dependencies are installed:
npm install
- This command will install all the required packages based on the dependency info in the
package.json
file.
Then, you can run the tests:
npm run test/fast # Fast tests
npm run test/slow # Slow tests
npm test # All tests
- The above commands will help you quickly verify the reliability of your code in different ways.
9. Enable Bash Completion ๐
To make your terminal smarter, you can also enable Bash completion. Add the following to your configuration file:
[[ -r $NVM_DIR/bash_completion ]] && \. $NVM_DIR/bash_completion
- Doing this will give you auto-completion when using NVM, enhancing your productivity.
10. Uninstalling NVM โ
When you no longer need NVM, you can uninstall it using the following commands!
nvm_dir="${NVM_DIR:-~/.nvm}"
nvm unload # Unload nvm
rm -rf "$nvm_dir" # Remove the nvm folder
- Make sure to back up any important projects before running these commands! Once you execute the uninstall command, NVM and the associated Node.js versions will be removed.
Also, donโt forget to remove the lines loading NVM from your configuration file so that it doesnโt try to load NVM again:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This line loads nvm
[[ -r $NVM_DIR/bash_completion ]] && \. $NVM_DIR/bash_completion # Loads nvm's bash completion
11. Updating NVM ๐
To update your installed NVM, simply run the following command:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
- Note that this will download and install the latest version of NVM, ensuring you have the latest features and security fixes.
Install the Latest Node Version
nvm install node
- If you need to install the latest version of Node.js again, this command is very handy.
Install the LTS Version
nvm install --lts
- If youโre looking for stability, opt for the latest LTS (Long-Term Support) version!
12. Automated Update Script โก
To ease the process, you can create a small script for automatically updating NVM.
#!/bin/bash
nvm_string="nvm-sh/nvm";
git_latest_version() {
basename $(curl -fs -o/dev/null -w %{redirect_url} https://github.com/$1/releases/latest)
}
latest_version_number=$(git_latest_version "${nvm_string}");
wget -qO- https://raw.githubusercontent.com/${nvm_string}/${latest_version_number}/install.sh | bash
Usage (Linux or Mac)
-
Create a script file
vim install-nvm.sh
-
Paste the code and save it.
-
Run the script
sh install-nvm.sh
- By following these steps, you will be able to directly install the latest version of NVM.
That’s it! These are the complete steps and methods to use NVM! Hope this helps you smoothly embark on your Node.js development journey! โจ