Npm Install Nvm

When developing Node.js applications, you might face situations where you need to install multiple versions of Node. This can happen when you have multiple projects and they have different requirements, or you have a deployable application which must be compatible with different Node versions. Without a good tool, this would mean a lot of work and effort to install the different versions manually, and basing a project on a specific version. Fortunately, there’s a better way!

To install specific nodejs version, Visit our tutorial Install Specific Nodejs Version with NVM. Step 1 – Add Node.js PPA. Node.js package is available in LTS release and the current release. It’s your choice to select which version you want to install on the system as per your requirements. To solve both these issues, many developers opt to use a node version manager, or nvm, to install npm. The version manager will avoid permissions errors, and will solve the complexities of updating Node.js and npm. In addition, developers can use an nvm to test their applications on multiple versions of npm. Nov 24, 2016  Node Version Manager - Simple bash script to manage multiple active node.js versions. Git versions before v1.7 may face a problem of cloning nvm source from GitHub via https protocol. (and npm) when you run nvm install X. The Node project compiles, tests and hosts/provides pre-these compiled binaries which are built for.

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

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 which offers Windows users the possibility 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, make sure you uninstall any Node.js version you might have on your system, as they can collide with the installation. After this, download the latest stable installer. Run the executable installer, follow the steps provided and you’re good to go!

How To Install Npm Windows

macOS/Linux

Removing previous Node installations is optional, although it’s advised you do so. There are plenty of good online resources for how you might do this (macOS, Linux). It’s also good if you remove any npm installation you might have, since it might collide with nvm’s installation. You’ll also need to have a C++ compiler installed on your system. For macOS, you can install the Xcode command line tools. You can do this by running the following command:

On Linux, you can install the build-essential package by running the following (assumes apt):

Having the required C++ compiler, you can then install nvm using cURL or Wget. On your terminal, run the following:

With cURL:

Or with Wget:

Note that the version number (v0.33.8) 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.

That’s it, nvm is installed and 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:

By running the above in a terminal, nvm will install Node.js version 8.9.4. nvm follows SemVer, so if you want to install, for example, the latest 8.9 patch, you can do it by running:

nvm, will then install Node.js version 8.9.X, where X is the highest available version. At the time of writing, this is 4, so you’ll have the 8.9.4 version installed on your system. You can see the full list of available versions by running:

For nvm-windows, this is:

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 are not 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:

By running the above, nvm will install Node.js version 9.0.0, the corresponding npm version, and reinstall the npm packages you had installed on the 8.9 version.

Install Npm Through Nvm

To help even further, nvm has some aliases that you can use to target versions without writing a specific version:

  • node: this installs the latest Node.js version
  • unstable: this installs Node.js 0.11 version, which is the last unstable version.

So, to install the latest Node.js version, you can just run:

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

Switching between Versions

So far, we’ve seen how to install different Node versions. Let’s now 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 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 similar to the install command. So, you need to follow this by a version number or one of the aliases.

Following the install commands demonstrated above, you can:

  • switch to Node.js version 8.9.4:

  • switch to Node.js version 9.3.0:

  • switch to latest Node.js version:

  • switch to latest LTS Node.js version:

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

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

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

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

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

Install nvm from npm

Note on io.js

As from Node.js v4, all features of io.js were merged into the Node.js project. io.js is no longer under development.

Listing installed instances

Npm Install Nvm Use

At anytime you can check which versions you have installed by running:

This will display something resembling the following:

The entry in green, with an arrow on the left, is the current version in use. Below the green and blue list, there’s a list of available aliases. If you now execute:

It will display:

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

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 really important to you that this happens automatically, there’s a project (avn) which integrates with nvm, and is designed to address this need.

Other nvm Commands

There are other commands nvm provides, which are more of advanced or not common usage:

Npm Install Nvm On Ubuntu

  • running a command directly for an installed version without switching the node variable:

  • running a command on a sub-shell, targeting a specific version:

  • getting the path to the Node.js executable of a specific installed version:

Conclusion

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

A thank you note to Tim Caswell the creator of nvm, and Corey Butler for the nvm for Windows support, and of course to the contributors of 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? Would love to hear from you in the comments below.

Npm Install Nvm Windows

This article was peer reviewed by Tim Severien. Thanks to all of SitePoint’s peer reviewers for making SitePoint content the best it can be!