SitePoint
  • Premium
  • Library
  • Community
  • Jobs
  • Blog
LoginStart Free Trial
Node.js: Tools & Skills, 2nd Edition
Node.js: Tools & Skills, 2nd Edition
Notice of Rights
Notice of Liability
Trademark Notice
About SitePoint
Who Should Read This Book?
Conventions Used
Introducing nvm
OS Support
Installation
Using nvm
Conclusion
Installing Node.js
Updating npm
Node Packaged Modules
Changing the Location of Global Packages
Installing Packages in Global Mode
Listing Global Packages
Installing Packages in Local Mode
Managing Dependencies with package.json
Uninstalling Local Packages
Installing a Specific Version of a Package
Updating a Package
Searching for Packages
Re-installing Project Dependencies
Managing the Cache
Audit
Aliases
npx
Conclusion
What is Express?
Getting Started
Application Structure
app.js
Bootstrapping a New Project
A Useful Tool
Basics
HTTP
Database
Views
Advanced
Adonis Pros
Adonis Cons
Verdict
What is an API?
1. Express
2. Fastify
Decorators
3. FeathersJS
4. KeystoneJS
5. Strapi
Summary
Quick Start: How to Use MySQL in Node
Installing the mysql Module
Getting Started
Connecting to the Database
Executing Queries
Advanced Use
Why Not Just USE an ORM?
Conclusion
Terminology and Basic Concepts
Installing MongoDB
Basic Database Operations
User Management
MongoDB CRUD Operations
Schema Validation
An Overview of MongoDB Drivers
Conclusion

Installing Multiple Versions of Node.js Using nvm

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\<user>\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. On your terminal, run the following:

With cURL:

Code snippet

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash

Or with Wget:

Code snippet

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash

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 snippet

nvm install 12.14.1

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

Running nvm use

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:

Code snippet

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:

Code snippet

nvm ls-remote

For nvm-windows, this is:

Code snippet

nvm ls available

Reducing Output

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/<version>/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 snippet

nvm install v12.14.1 --reinstall-packages-from=10.18.1

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 snippet

nvm install node

This will currently pull in version 13.6.0.

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

Code snippet

nvm install --lts

This will currently pull in version 12.14.1.

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

Code snippet

nvm uninstall 13.6.0

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 snippet

nvm use 13.6.0

Switch to Node.js version 12.14.1:

Code snippet

nvm use 12.14.1

Switch to the latest Node.js version:

Code snippet

nvm use node

Switch to the latest LTS version:

Code snippet

nvm use --lts

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 snippet

nvm alias awesome-version 13.6.0

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

Code snippet

nvm use awesome-version

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

Code snippet

nvm unalias awesome-version

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

Code snippet

nvm alias default 12.14.1

Listing Installed Instances

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

Code snippet

nvm ls

This will display something resembling the following:

nvm versions list

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 snippet

nvm use nodenvm ls

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 snippet

nvm current

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 snippet

autoload -U add-zsh-hookload-nvmrc() {  local node_version="$(nvm version)"  local nvmrc_path="$(nvm_find_nvmrc)"
  if [ -n "$nvmrc_path" ]; then    local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
    if [ "$nvmrc_node_version" = "N/A" ]; then      nvm install    elif [ "$nvmrc_node_version" != "$node_version" ]; then      nvm use    fi  elif [ "$node_version" != "$(nvm version default)" ]; then    echo "Reverting to nvm default version"    nvm use default  fi}add-zsh-hook chpwd load-nvmrcload-nvmrc

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 snippet

nvm run 13.6.0 --version

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

Code snippet

nvm exec 13.6.0 node --version

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

Code snippet

nvm which 13.6.0

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.

End of PreviewSign Up to unlock the rest of this title.

Community Questions