Installing Multiple Versions of Node.js Using nvm — SitePoint


When working with Node.js, you might encounter situations where you need to install multiple versions of the runtime.

For example, maybe you have the latest version of Node set up on your machine, yet the project you’re about to start working on requires an older version. Or maybe you’re upgrading an old Node project to a more modern version and it would be handy to be able to switch between the two while you make the transition.

Without a good tool, this would mean spending a lot of time and effort manually uninstalling and reinstalling Node versions and their global packages. Fortunately, there’s a better way!

Introducing nvm

nvm stands for Node Version Manager. As the name suggests, it helps you manage and switch between different Node versions with ease. It provides a command-line interface where you can install different versions with a single command, set a default, switch between them and much more.

OS Support

nvm supports both Linux and macOS, but that’s not to say that Windows users have to miss out. There’s a second project named nvm-windows that offers Windows users the option of easily managing Node environments. Despite the name, nvm-windows is not a clone of nvm, nor is it affiliated with it. However, the basic commands listed below (for installing, listing and switching between versions) should work for both nvm and nvm-windows.

Installation

Let’s first cover installation for Windows, macOS and Linux.

Windows

First, we need to do a little preparation:

  • uninstall any existing versions of Node.js
  • delete any existing Node.js installation directories (such as C:\Program Files\nodejs)
  • delete the existing npm install location (such as C:\Users\\AppData\Roaming\npm)

After this, download and run the latest stable installer and you should be good to go!

macOS/Linux

Unlike Windows, removing previous Node and npm installations in macOS and Linux is optional. If this is something you want to do, there are plenty of good resources available online. For example, here’s how to remove Node on macOS and on Linux. And here’s how you can remove any previous npm installation you might have.

You can install nvm using cURL or Wget.

For cURL, run the following on your terminal:

<code class="bash language-bash"><span class="token function">curl</span> -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh <span class="token operator">|</span> <span class="token function">bash</span>
</code>

Or with Wget:

<code class="bash language-bash"><span class="token function">wget</span> -qO- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh <span class="token operator">|</span> <span class="token function">bash</span>
</code>

Note that the version number (v0.35.2) will change as the project develops, so it’s worth checking the relevant section of project’s home page to find the most recent version.

This will clone the nvm repository to ~/.nvm and will make the required changes to your bash profile, so that nvm is available from anywhere in your terminal.

And that’s it! Reload (or restart) your terminal and nvm is ready to be used.

Using nvm

If installed correctly, the nvm command is available anywhere in you terminal. Let’s see how to use it to manage Node.js versions.

Install Multiple Versions of Node.js

One of the most important parts of nvm is, of course, installing different versions of Node.js. For this, nvm provides the nvm install command. You can install specific versions by running this command followed by the version you want. For example:

<code class="bash language-bash">nvm <span class="token function">install</span> <span class="token number">12.14</span>.1
</code>

By running the above in a terminal, nvm will install Node.js version 12.14.1.

Tip: nvm-windows users will have to run nvm use 12.14.1 after installing.

nvm follows SemVer, so if you want to install, for example, the latest 12.14 patch, you can do it by running nvm install 12.14.

nvm will then install Node.js version 12.14.X, where X is the highest available version. At the time of writing, this is 1, so you’ll have the 12.14.1 version installed on your system.

You can see the full list of available versions by running nvm ls-remote.

For nvm-windows, this is nvm ls available.

Tip: listing all available Node versions produces a lot of output. Linux users might like to qpipe that to less or grep the version they’re after. For example, nvm ls-remote | less, or nvm ls-remote | grep v12.

npm

When installing a Node.js instance, nvm will also install a compatible npm version. Each Node version might bring a different npm version, and you can run npm -v to check which one you’re currently using. Globally installed npm packages aren’t shared among different Node.js versions, as this could cause incompatibilities. Rather, they’re installed alongside the current Node version in ~/.nvm/versions/node//lib/node_modules. This has the added advantage that users won’t require sudo privileges to install global packages.

Fortunately, when installing a new Node.js version, you can reinstall the npm global packages from a specific version. For example:

<code class="bash language-bash">nvm <span class="token function">install</span> v12.14.1 --reinstall-packages-from<span class="token operator">=</span><span class="token number">10.18</span>.1
</code>

By running the above, nvm will install Node.js version 12.14.1, the corresponding npm version, and reinstall the global npm packages you had installed for the 10.18.1 version.

If you’re not sure what the latest version is, you can use the node alias:

<code class="bash language-bash">nvm <span class="token function">install</span> node
</code>

This will currently pull in version 13.6.0.

Or you can install the most recent LTS release, using:

<code class="bash language-bash">nvm <span class="token function">install</span> --lts
</code>

This will currently pull in version 12.14.1.

You can also uninstall any instance you no longer think is useful, by running:

<code class="bash language-bash">nvm uninstall <span class="token number">13.6</span>.0
</code>

Switching Between Versions

So far, we’ve seen how to install different Node versions. Now let’s go through how to switch between them. Let me first note that when a new version is installed, it’s automatically put to use. So if you install the latest Node.js version, and run node -v right after, you’ll see the latest version output.

To switch through installed versions, nvm provides the nvm use command. This works similarly to the install command. So, you need to follow this by a version number or an alias.

Switch to Node.js version 13.6.0:

<code class="bash language-bash">nvm use <span class="token number">13.6</span>.0
</code>

Switch to Node.js version 12.14.1:

<code class="bash language-bash">nvm use <span class="token number">12.14</span>.1
</code>

Switch to the latest Node.js version:

<code class="bash language-bash">nvm use node
</code>

Switch to the latest LTS version:

<code class="bash language-bash">nvm use --lts
</code>

When switching to a different version, nvm will make the node instance in your terminal symlink to the proper Node.js instance.

Custom Aliases

You can also create custom aliases beyond the ones that come with nvm. For example, by running:

<code class="bash language-bash">nvm <span class="token builtin class-name">alias</span> awesome-version <span class="token number">13.6</span>.0
</code>

You’re setting an alias with the name “awesome-version” for Node.js version 13.6.0. So, if you now run:

<code class="bash language-bash">nvm use awesome-version
</code>

nvm will switch node to version 13.6.0. You can delete an alias by running:

<code class="bash language-bash">nvm <span class="token builtin class-name">unalias</span> awesome-version
</code>

You can also set a default instance to be used in any shell, by targeting a version to the “default” alias, like so:

<code class="bash language-bash">nvm <span class="token builtin class-name">alias</span> default <span class="token number">12.14</span>.1
</code>

Listing Installed Instances

At any time you can check which versions you have installed by running:

<code class="bash language-bash">nvm <span class="token function">ls</span>
</code>

This will display something resembling the image below.

The entry in green, with an arrow on the left, is the current version in use. Below the installed versions, there’s a list of available aliases. Try executing the following now:

<code class="bash language-bash">nvm use node
nvm <span class="token function">ls</span>
</code>

It will display like so:

nvm use and versions list

You can also check what is the current version in use with the command:

<code class="bash language-bash">nvm current
</code>

Specify a Node Version on a Per-project Basis

Version managers such as rbenv allow you to specify a Ruby version on a per-project basis (by writing that version to a .ruby-version file in your current directory). This is kind of possible with nvm in that, if you create a .nvmrc file inside a project and specify a version number, you can cd into the project directory and type nvm use. nvm will then read the contents of the .nvmrc file and use whatever version of Node you specify.

If it’s important to you that this happens automatically, there are a couple of snippets on the project’s home page for you to add to your .bashrc or .zshrc files to make this happen.

Here’s the ZSH snippet. Place this below your nvm config:

<code class="bash language-bash">autoload -U add-zsh-hook
load-<span class="token function-name function">nvmrc</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token builtin class-name">local</span> <span class="token assign-left variable">node_version</span><span class="token operator">=</span><span class="token string">"<span class="token variable"><span class="token variable">$(</span>nvm version<span class="token variable">)</span></span>"</span>
  <span class="token builtin class-name">local</span> <span class="token assign-left variable">nvmrc_path</span><span class="token operator">=</span><span class="token string">"<span class="token variable"><span class="token variable">$(</span>nvm_find_nvmrc<span class="token variable">)</span></span>"</span>

  <span class="token keyword">if</span> <span class="token punctuation">[</span> -n <span class="token string">"<span class="token variable">$nvmrc_path</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
    <span class="token builtin class-name">local</span> <span class="token assign-left variable">nvmrc_node_version</span><span class="token operator">=</span><span class="token variable"><span class="token variable">$(</span>nvm version <span class="token string">"<span class="token variable"><span class="token variable">$(</span><span class="token function">cat</span> <span class="token string">"<span class="token variable">${nvmrc_path}</span>"</span><span class="token variable">)</span></span>"</span><span class="token variable">)</span></span>

    <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$nvmrc_node_version</span>"</span> <span class="token operator">=</span> <span class="token string">"N/A"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
      nvm <span class="token function">install</span>
    <span class="token keyword">elif</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$nvmrc_node_version</span>"</span> <span class="token operator">!=</span> <span class="token string">"<span class="token variable">$node_version</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
      nvm use
    <span class="token keyword">fi</span>
  <span class="token keyword">elif</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$node_version</span>"</span> <span class="token operator">!=</span> <span class="token string">"<span class="token variable"><span class="token variable">$(</span>nvm version default<span class="token variable">)</span></span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
    <span class="token builtin class-name">echo</span> <span class="token string">"Reverting to nvm default version"</span>
    nvm use default
  <span class="token keyword">fi</span>
<span class="token punctuation">}</span>
add-zsh-hook chpwd load-nvmrc
load-nvmrc
</code>

Now, when you change into a directory with a .nvmrc file, your shell will automatically change Node version.

Automatically apply nvm use

Other nvm Commands

nvm provides a couple of other commands that are more advanced or less commonly used.

You can run a command directly for an installed version without switching the node variable:

<code class="bash language-bash">nvm run <span class="token number">13.6</span>.0 --version
</code>

You can run a command on a sub-shell, targeting a specific version:

<code class="bash language-bash">nvm <span class="token builtin class-name">exec</span> <span class="token number">13.6</span>.0 node --version
</code>

You can get the path to the Node.js executable of a specific installed version:

<code class="bash language-bash">nvm <span class="token function">which</span> <span class="token number">13.6</span>.0
</code>

This might be useful when configuring a text editor plugin that needs to know where your current version of Node lives.

Conclusion

nvm is a great tool for any Node.js developer. It enables a concern-free installation and easy switching between different versions, saving time for what really matters.

A thank you note to Tim Caswell, the creator of nvm, and also to Corey Butler for the nvm for Windows support, and of course to those contributing to these great projects. Your work is greatly appreciated by the Node.js community.

What about you? Do you use nvm? How has your experience been with it? Hit up Michael or James on Twitter if you’d like to chat.

FAQs about Node Version Manager (NVM)

Which operating systems are supported by NVM?

NVM supports Linux, macOS, and Windows operating systems.

How do I install NVM?

You can install NVM by following the installation instructions provided for your operating system. Typically, it involves running a script in your terminal or command prompt.

Can NVM work with different shells?

Yes, NVM can be configured to work with various shells like Bash, Zsh, and Fish. The installation instructions often include details on configuring NVM for your preferred shell.

Is NVM limited to Node.js, or can it manage other runtime versions too?

While NVM’s primary focus is on Node.js, there are community-maintained projects like “pyenv” for Python version management that are inspired by NVM’s concept.

How do I update NVM itself?

You can update NVM using the command nvm update.

Can I use NVM with other Node.js version managers?

It’s generally recommended to use only one Node.js version manager at a time to prevent conflicts. Using NVM alongside other managers could lead to unexpected behavior.

Is NVM limited to stable releases of Node.js?

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

Does NVM only manage Node.js versions?

No, NVM can also manage installations of npm (Node Package Manager) alongside Node.js versions. When you switch Node.js versions, NVM will also switch to the corresponding npm version.

How do I install a specific version of Node.js using NVM?

You can install a specific version of Node.js using the command nvm install [version]. For example, nvm install 14.17.3 will install Node.js version 14.17.3.

Can I uninstall a specific Node.js version with NVM?

Yes, you can uninstall a specific Node.js version using the command nvm uninstall [version]. For example, nvm uninstall 10.15.0 will uninstall Node.js version 10.15.0.

How do I list the Node.js versions I have installed with NVM?

You can list the installed Node.js versions using the command nvm lscode or nvm list.

Can I set a default Node.js version to be used?

Yes, you can set a default Node.js version using the command nvm alias default [version]. This version will be used whenever you open a new terminal window.

How do I switch between Node.js versions using NVM?

To switch between Node.js versions, you can use the command nvm use [version]. For instance, nvm use 12.22.6 will switch to Node.js version 12.22.6.

Why would I need NVM?

NVM is useful when you work on projects that require specific versions of Node.js. It allows you to switch between these versions seamlessly, ensuring compatibility and avoiding version conflicts.

What is NVM?

Node Version Manager (NVM) is a command-line tool that allows you to easily manage multiple installations of Node.js and switch between different versions on the same machine.





Source link

Leave a Reply

Your email address will not be published. Required fields are marked *