Installing and Using nvm

TUTORIAL

Once again struggling with environment versions? This time are you dealing with node versions? Good — it’s time to fix it. I want to clarify that the need to manage multiple versions of the development environment is not a prerogative of the JS world. I have already dealt in the past with a similar topic for python, and the solution principle is always very similar. Before going into details, I want to emphasize that the speed at which new versions are released should not be seen as a problem, but as an opportunity because continuous improvement is what provides developers with new features and stable releases. I am aware, however, that continuous change can become onerous and sometimes frustrating as well as complex because upgrading to the latest version can introduce backward-compatibility issues with old projects or ongoing projects often not solvable, but it is precisely for this reason that it becomes essential to know how to change and activate in real-time the version to use in a simple and quick way. In the case of node.js, to solve all our problems we can use nvm. Let’s see how it works and how to install it through the following simple guide.

Installation

Download and install nvm using the following installation script via cURL:

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

At the end of the installation make sure that nvm was installed correctly by running

 nvm --version

If everything went well the command should return the installed nvm version and at this point you will be able to install the version of Node.js you want in your virtual environment. For example you can install the latest version of node.js via nvm by running the following command:

nvm install node

while to use the installed version it will be enough to run the command:

nvm use node

How to manage multiple Node.js versions

As mentioned, the most important feature of nvm is that it allows us to install different versions of Node.js and switch from one to another easily and quickly. Let’s see how this is possible with examples. Suppose you want to install a specific version of node.js, for example 8.9.4 — in that case just run the command:

nvm install 8.9.4

while, if you want to install the latest version of 8.9 you only need to run:

nvm install 8.9

How to check installed and available Node.js versions

To see the list of installed node.js versions you can use the command:

nvm list

while to see the list of versions available for installation you can use:

nvm ls-remote

Uninstalling a Node.js version

You can uninstall any instance you no longer need by running:

nvm uninstall <version>

for example if you wanted to uninstall version 0.11

nvm uninstall 0.11

Switching from one version to another

So far we’ve seen how to install different versions of Node. Now let’s see how to switch between them. Remember that when you install a new version, it will be automatically set as the default. So, if you install the latest version of Node.js and run the command

node -v

you will see the output of the latest installed version.

To switch from one installed version to another, nvm provides the command

nvm use <version>

Example 1- switch to Node.js version 8.9.4:

nvm use 8.9.4

Example 2 – switch to Node.js version 9.3.0:

nvm use 9.3

Example 3 – switch to the latest Node version

nvm use node

Example 3 – switch to the latest Node LTS version

nvm use node --lts

When you switch to a different version, nvm will link, only for the active terminal session, the node instance of that session to the Node.js version to use.

Custom aliases

You can also create custom aliases for better organization. For example, by running:

nvm alias my-version 8.9.4

to activate the version you can then run the command

nvm use my-version

To delete the alias, just run

nvm unalias my-version

Configure default Node version for any shell

To set a default instance to use in any shell, you can use the following command:

nvm alias default node

 

Se vuoi farmi qualche richiesta o contattarmi per un aiuto riempi il seguente form

    Comments