Thursday, March 21, 2024

Installing Multiple Versions of Node.js Using nvm — SitePoint

Installing Multiple Versions of Node.js Using nvm — SitePoint

There may be instances when using Node.js that require you to install different runtime versions.

For instance, it's possible that even though you have the most recent version of Node installed on your computer, you must downgrade because the project you're going to work on calls for an earlier version. It could also be that you are updating an outdated Node project to a more recent one, in which case it would be useful to have the ability to quickly switch between the two.

To install the necessary Node version, this would need a lot of time and work being spent manually uninstalling and reinstalling Node versions and their global packages without a decent tool. Thankfully, there's an improved method nvm!

The simplest way to manage numerous projects with varied version requirements is to use Node Version Manager (nvm), which makes it simple for developers to transition between different Node.js versions.

Cross-Platform Support: nvm is mostly focused on Linux and macOS, but it also supports Windows users using nvm-windows, a different but functionally equivalent utility for managing Node environments on Windows computers.

Installation and Usage Flexibility: nvm drastically reduces the complexity involved in version management by offering simple commands for installing, listing, and switching between Node versions. It also facilitates a seamless installation procedure across many operating systems.

The acronym for Node Version Manager is nvm. As the name implies, it makes it simple to maintain and switch between several Node versions. It has a command-line interface that lets you do a lot of things, such establish a default, switch between versions, and install several versions with a single command.

Although nvm is compatible with Linux and macOS, Windows users don't have to give up on it. Another project called nvm-windows gives Windows users the ability to manage Node setups with ease. Despite its name, nvm-windows is neither associated with or a clone of nvm. Nonetheless, both nvm and nvm-windows should be able to use the fundamental commands mentioned below for installing, listing, and switching between versions.

First, let's discuss Linux, macOS, and Windows installation.

In Linux and macOS, it's not mandatory to remove earlier versions of Node and npm, unlike Windows. If you're interested in doing this, there are a ton of helpful information on the internet. For instance, here's how to uninstall Node on Linux and macOS. And this is how to uninstall any earlier npm installations you may have had.

Nvm can be installed with Wget or cURL.

It is important to keep in mind that the version number (v0.35.2) may vary as the project progresses, thus it is advisable to visit the relevant area of the project's homepage to ascertain the most recent version.

This will modify your bash profile and clone the nvm repository to ~/.nvm, making nvm accessible from wherever in your terminal.

And that's it! When you reload or restart your terminal, VMware is now operational.

If installed properly, you can use the nvm command anywhere in your terminal. Let's examine how to manage Node.js versions with it.

Installing various Node.js versions is, of course, one of the most crucial aspects of nvm. Nvm offers the nvm install command for this purpose. By using this command and the desired version, you can install particular versions. As an illustration:

The aforementioned command will install Node.js version 12.14.1 in a terminal using nvm.

Advice: After installation, nvm-windows users must run nvm usage 12.14.1.

Since nvm adheres to SemVer, you can use nvm install 12.14 to install the most recent 12.14 patch, for example.

Next, Node.js version 12.14.X—where X is the most recent version—will be installed via nvm. You'll have the 12.14.1 version installed on your machine as of this writing, which is 1.

Use nvm ls-remote to get the complete list of versions that are currently available.

This is nvm ls available for nvm-windows.

It generates a lot of output to list every version of Node that is available. For Linux users, you can try grep the desired version or qpipe that to less. nvm ls-remote | less and nvm ls-remote | grep v12 are two examples.

Nvm will install an appropriate version of npm along with a Node.js instance. You may use npm -v to find out the npm install you're presently using. various Node versions may come with various npm installs. Different Node.js versions do not share globally installed npm packages because this could lead to incompatibility. Instead, they are installed in ~/.nvm/versions/node//lib/node_modules with the most recent version of Node. The extra benefit of this is that users can install global packages without requiring sudo capabilities.

Fortunately, you can reinstall the npm global packages from a specified version when installing a new version of Node.js. As an illustration:

By executing the aforementioned command, nvm will restore the global npm packages you had previously installed for the 10.18.1 version and install Node.js version 12.14.1, the precise version of npm needed.

We have shown how to install various Node versions thus far. Let's now discuss how to switch between them, or more specifically, how to upgrade or downgrade your Node version. First, let me say that a new version is automatically used after installation. You will thus see the most recent version output if you install the most recent version of Node.js and then execute node -v immediately after.

The nvm use command allows you to cycle through installed versions. This function is analogous to the install command. Therefore, you must use an alias or version number to follow this.

Update to version 13.6.0 of Node.js:

Use Node.js version 12.14.1 instead:

Update to the most recent version of Node.js:

Nvm will create a symlink between the node instance in your terminal and the correct Node.js instance when you transition to a new version.

In addition to the aliases included with nvm, you can also make your own. For instance, by sprinting:

You are configuring an alias for Node.js version 13.6.0 called awesome-version. Thus, in the event that you run now:

NVM will upgrade the node to 13.6.0. To get rid of an alias, execute:

This will show something similar to the picture down below.

The version that's currently in use is indicated by the green entry with the left arrow. Below the installed versions, an alias list is displayed. Try doing the following right now:

You can set a Ruby version for each project using version managers like rbenv (by writing that version to a.ruby-version file in your current directory). This is somewhat achievable with nvm since you can type nvm use by cding into the project directory after creating a.nvmrc file inside a project using a particular Node version. Next, nvm will utilize the Node version that you designate after reading the contents of the.nvmrc file.

There are a few snippets on the project home page that you can add to your.bashrc or.zshrc files to enable this to happen automatically if you find it important.

This is the ZSH sample. Put this beneath the NVM configuration:

Now, your shell will automatically change the version of Node when you navigate into a directory containing a.nvmrc file.

A few additional commands that are either less frequently used or more sophisticated are available in nvm.

The path to the Node.js executable for a certain installed version can be found here:

When setting up a text editor plugin that needs to know where your current Node version is stored, this could be helpful.

An excellent tool for every Node.js developer is nvm. It allows for worry-free installation and simple version switching, freeing you time for the important things.

A special thanks goes out to Tim Caswell, who created nvm, Corey Butler, who provided nvm for Windows support, and all the other contributors to these fantastic projects. The Node.js community really appreciates your work.

How about you? Are you an NVM user? What is your experience with it like? If you'd like to talk, follow James or Michael on Twitter.

Linux and macOS are both supported by nvm. WSL or nvm-windows should be used by Windows users.

By according to your operating system's installation instructions, you can install NVM. Usually, it entails executing a script via your command prompt or terminal.

Indeed, NVM may be set up to function with several shells, including Fish, Zsh, and Bash. You can usually find information about setting NVM for your favorite shell in the installation instructions.

NVMs is primarily focused on Node.js, but its philosophy has also influenced other community-maintained projects, such as Python version management software like Pyenv.

The command nvm update can be used to update NVM.

To avoid conflicts, it is generally advised to utilize only one Node.js version management at a time. When NVM is used in conjunction with other managers, surprising things can happen.

No, you can install and manage development, LTS (Long-Term Support), and stable versions of Node.js using NVM.

No, in addition to managing Node.js versions, NVM can also handle npm (Node Package Manager) installations. NVM will update to the matching npm version when you switch Node.js versions.

The command nvm install [version] can be used to install a particular version of Node.js. To install Node.js version 14.17.3, for instance, run nvm install 14.17.3.

Yes, you may use the command nvm uninstall [version] to remove a particular version of Node.js. To uninstall Node.js version 10.15.0, for instance, type nvm uninstall 10.15.0.

The commands nvm lscode and nvm list can be used to view the installed versions of Node.js.

Yes, you may use the command nvm alias default [version] to set a default version of Node.js. Every time a new terminal window is opened, this version will be utilized.

Use the command nvm use [version] to switch between versions of Node.js. For example, using nvm use 12.22.6 will make the move to version 12.22.6 of Node.js.

When working on projects that call for particular Node.js versions, NVM comes in handy. It guarantees compatibility and prevents version conflicts by enabling you to move between these versions with ease.

Using the command-line utility Node Version Manager (NVM), you may quickly switch between different versions of Node.js on the same workstation and manage numerous installations of the framework.

You certainly can. The command nvm install [version] can be used to install a prior version of Node.js.Use the command nvm use [version] to switch between versions of Node.js. For example, using nvm use 12.22.6 will make the move to version 12.22.6 of Node.js.

I write readable, modular, and tidy code. I enjoy picking up new skills that streamline my workflow and boost productivity.

At SitePoint, network administrator, independent web developer, and editor.

No comments:

Post a Comment