Manage multiple node js version
Managing various Node.js version for different project can be cumbersome and time consuming. That’s why the Node Version Manage(NVM) comes in handy. NVM simplifies the process by enabling you to effortlessly install and switch different Node.js versions, streamlining your development workflow.
What is NVM?
Node Version Manager, commonly know as nvm, is a versatile command-line utility that facilitates the seamless management of multiple installations of Node.js on a single system. This tool empowers developers to effortlessly switch between various Node.js versions, enabling precise control and flexibility for projects with specific version requirements. Through nvm’s intuitive interface, users can install, uninstall, and effortlessly transition between different Node.js versions, enhancing productivity and facilitating compatibility across diverse development environments.
Homebrew
Homebrew is a package manager for macOS and Linux operating systems. It simplifies the process of installing software on these platforms by providing a command-line interface to install, update, and manage software packages. Homebrew installs software into its own directory and then symlinks their files into /usr/local. It’s widely used by developers and system administrators to easily manage software dependencies and keep their systems up to date with the latest versions of various tools and applications.
Install Homebrew, simply follow the steps outlined on the official website.
In this article, you’ll discover a simple guide on installing multiple versions of Node.js on your macOS system using Homebrew. Additionally, you’ll learn effortless methods for seamlessly switching between different Node.js versions.
After successfully installing Homebrew, you can proceed to install NVM.
Using Homebrew
To install the Node Version Manager using Homebrew, simply run the following command in your terminal:
brew install nvm
Once the installation is complete, open your profile file (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc) and add the following lines to the end of the file:
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
Adding these lines to your profile file enables you to use the nvm command in your terminal. Failure to add these lines will result in an error when attempting to run nvm commands. After making these changes, restart your terminal to begin using the nvm commands seamlessly.
NVM commands
Now that NVM is installed, let’s explore some of its most useful commands.
To check the NVM version installed, use:
nvm --version
After installing NVM, you can use the following commands:
This command lists all the Node.js versions available for download. Be cautious, as it might take a while to finish due to the extensive list of versions.
nvm ls-remote
After running nvm ls-remote, you can install the desired Node.js version using the following syntax:
nvm install <version>
For example, to install Node.js version 14, you would run:
nvm install 20
If you wish to install a specific version, you can specify it, such as 20.9.0.
Note: To install the latest LTS (Long Term Support) version of Node.js, simply run:
nvm install --lts
This command lists all the Node versions installed on your machine. It’s important to note the distinction between nvm ls and nvm ls-remote. The former lists the installed
nvm ls
After installing the desired Node version, you can activate it using:
nvm use
For example, to switch to Node.js version 14.17.6, you would run:
nvm use 14.17.6
Note: To use the latest LTS version, you can run nvm use — lts.
Setting a default version:
nvm alias default <version>
For instance, to set Node.js version 16 as the default, you would use:
nvm alias default 16
Uninstalling a specific Node version:
nvm uninstall 14.17.6
Setting Node version in the .nvmrc file:
You can specify the desired Node version for a particular project by creating a .nvmrc file in the project’s root directory. For example, to set Node version 16, you would add the following line to the .nvmrc file:
echo "16" > .nvmrc
Uninstalling NVM:
If you want to uninstall NVM entirely, you can use the command:
nvm unload
Conclusion
In conclusion, the Node Version Manager (NVM) offers a convenient solution for managing multiple Node.js versions on your system. By following the steps outlined, you can seamlessly install, switch between, and uninstall Node.js versions according to your project requirements. From listing available versions to setting defaults and even specifying versions for individual projects using the .nvmrc file, NVM provides flexibility and control over your Node.js development environment. Whether you’re exploring different Node.js releases or ensuring compatibility with specific project dependencies, NVM streamlines the process, enhancing your development workflow.