“`html
Node Version Manager (nvm) is a powerful tool for managing multiple installed versions of Node.js on your development machine. This blog post covers everything from what nvm is, why you might want different versions of Node.js, to detailed guides on installing, using, and finally uninstalling nvm. By the end of this guide, you’ll be equipped with all the knowledge necessary to manage your Node.js environment effectively.
What is nvm?
Node Version Manager, commonly referred to as nvm, is a script-based tool that allows developers to manage and switch between multiple versions of Node.js effortlessly. Since different projects might depend on different versions of Node.js, having the ability to switch between these versions quickly is invaluable.
Nvm simplifies the process of switching Node.js versions without the need for complex, manual processes. It ensures that you can easily test your application against different versions of Node.js, which can be crucial for compatibility and performance assessments.
Now, why are different Node.js versions available?
Node.js has a vibrant and dynamic community, and like any robust technology, it evolves rapidly. Each new version brings performance improvements, new features, and sometimes, deprecations. Developers often need to test their code against multiple versions of Node.js, especially when preparing for a migration to a newer version.
In addition, some legacy projects may rely on older versions of Node.js, while cutting-edge projects might require the latest functionalities. Having multiple versions installed simultaneously allows for flexible and responsive development and debugging processes.
How to download nvm on Mac?
Downloading nvm on a Mac can be straightforward if you follow the steps correctly. You can use either the curl or wget command to get the nvm install script from the official nvm repository on GitHub.
To download nvm, open your terminal and execute the following command: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
. This command downloads and runs the script to install nvm on your Mac.
Install nvm
Installing nvm requires you to execute the downloaded script properly. Once you have downloaded the script using curl or wget, the next step is to run it using bash.
After running the installation script, you need to ensure that nvm is correctly added to your shell configuration file. If you are using bash, it would typically be ~/.bashrc
or ~/.bash_profile
. For zsh users, it would be ~/.zshrc
.
Installing nvm
If you’re on a Mac
If you are on a Mac, the process is relatively straightforward. First, download the nvm installation script using curl or wget as described earlier, then run the script using bash.
After running the script, add the following lines to your shell 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 loads nvm
If you’re on Windows
Installing nvm on Windows involves a slightly different process since it doesn’t natively support Unix-like scripts. Fortunately, there is a Windows version called nvm-windows that you can use.
First, download the nvm-setup.zip from the releases page of the nvm-windows repository on GitHub. Extract the contents and run the setup file. Follow the on-screen instructions, and once the installation is complete, open a new command prompt to start using nvm.
How to use nvm
To install the latest version of Node.js
Once nvm is installed, you can easily install the latest version of Node.js by running a simple command. Open your terminal and type:
nvm install node
This command will fetch and install the latest version of Node.js available.
To install a specific version of Node.js
Sometimes you may need a specific version of Node.js for your project. nvm makes this process straightforward. You can install a specific version by running:
nvm install [version]
Replace [version] with the actual version number you need, for example, nvm install 14.17.0
.
To use a specific version of Node.js
After installing multiple versions, you might want to switch between them. To use a specific version, you just need to execute:
nvm use [version]
Replace [version] with the desired version number, for example, nvm use 14.17.0
.
To see all the versions of Node.js that you have installed
To check all the versions of Node.js installed on your system, simply use the command:
nvm ls
This will display a list of all installed versions and indicate the currently active version.
To set the default version of Node.js
If you prefer to use a particular version of Node.js by default, you can set it using the alias command:
nvm alias default [version]
Replace [version] with your desired version number. This sets the specified version as the default one to be used in new terminal sessions.
To uninstall a specific version of Node.js
Should you need to free up space or remove projects that are no longer needed, you can easily uninstall a specific version of Node.js using nvm:
nvm uninstall [version]
Replace [version] with the version number you want to remove, for example, nvm uninstall 14.17.0
. This will delete the specified Node.js version from your system.
Lessons learned
Topic | Details |
---|---|
What is nvm? | A script-based tool for managing multiple Node.js versions |
Why different versions? | Diverse project dependencies and testing purposes |
Download nvm on Mac | Use curl or wget command to download the install script |
Install nvm | Run the install script and modify shell configuration file |
Installing nvm on Mac | Download script and update shell configuration |
Installing nvm on Windows | Download and run nvm-setup.zip |
Use nvm | Install, switch between, and manage Node.js versions |
Install latest Node.js | Command: nvm install node |
Install specific version | Command: nvm install [version] |
Use specific version | Command: nvm use [version] |
List installed versions | Command: nvm ls |
Set default version | Command: nvm alias default [version] |
Uninstall specific version | Command: nvm uninstall [version] |
“`