Node.js and NPM versioning with NVM

Our previous translation about the new functions of the 15th version of Node.js was very well received by the readers of Habr, so today we decided to continue the topic and tell you how to set up NVM with version 15 of Node.js and NPM 7.

Node.js version 15 was released on October 20, 2020. It ships with npm 7 and many new features. Have you already tried out the new version?

But wait a minute! Node.js 15 and npm 7 contain breaking changes. Wouldn’t the upgrade hurt existing projects then?

In theory, it can hurt!

Fortunately, we have NVM (Node Version Manager), which will save us from this danger. Let’s take a closer look at this tool in order to update versions of node.js and npm without any problems.

Install NVM

nvm manages the node.js and npm versions. It is installed for a specific user and can be called separately for each shell. nvm works with any POSIX-compatible shell (sh, dash, ksh, zsh, bash), including platforms: unix, macOS and windows WSL.

nvm can be installed using curl or wget commands:

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
$ wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

Script install.sh clones the nvm repository to ~/.nvm and tries to add the original lines from the below snippet to the desired profile file (~/.bash_profile, ~/.zshrc, ~/.profile or ~/.bashrc).

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm

IN ~/.bash_profile we see that the lines are added:

export NVM_DIR="/Users/fuje/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Using NVM

So, we have installed nvm. Now we use this command to install the latest version of node.js:

$ nvm install node
Downloading and installing node v15.4.0...
Downloading https://nodejs.org/dist/v15.4.0/node-v15.4.0-darwin-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with shasum -a 256
Checksums matched!
Now using node v15.4.0 (npm v7.0.15)

The output from the example above states that npm 7.0.15 used together with node.js 15.4.0… Let’s check:

$ node -v
v15.4.0
$ npm -v
7.0.15

We can also specify the required version for installation. The semantic format of the version is defined SemVer:

$ nvm install 10.14.0
Downloading and installing node v10.14.0...
Downloading https://nodejs.org/dist/v10.14.0/node-v10.14.0-darwin-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with shasum -a 256
Checksums matched!
Now using node v10.14.0 (npm v6.4.1)

If the specified version was already installed, it is not reinstalled:

$ nvm install 10.14.0
v10.14.0 is already installed.
Now using node v10.14.0 (npm v6.4.1)

We can display all installed versions:

$ nvm ls
->     v10.14.0
       v10.15.0
       v10.16.0
       v12.16.0
        v13.9.0
        v15.4.0
         system
default -> 12.16.0 (-> v12.16.0)
node -> stable (-> v15.4.0) (default)
stable -> 15.4 (-> v15.4.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/fermium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.23.0 (-> N/A)
lts/erbium -> v12.20.0 (-> N/A)
lts/fermium -> v14.15.1 (-> N/A)

In the above output examples, the character -> indicates that the current version of node.js is 10.14.0… The arrow also represents values ​​for default (12.16.0), node (15.4.0) and stable (15.4.0).

nvm use replaces the current version:

$ nvm use 12.16.0
Now using node v12.16.0 (npm v6.14.8)
$ nvm use 10.16.0
Now using node v10.16.0 (npm v6.14.5)
$ nvm use 13.9.0
Now using node v13.9.0 (npm v6.13.7)
$ nvm use default
Now using node v12.16.0 (npm v6.14.8)
$ nvm use node
Now using node v15.4.0 (npm v7.0.15)
$ nvm use stable
Now using node v15.4.0 (npm v7.0.15)

You might ask how it happened that v10.16.0 uses a later version of npm than v13.9.0… This task can be solved using the following commands:

$ nvm use 10.16.0
$ npm install -g npm@6.14.5

This command allows you to get the latest supported npm version for the current version of Node.js:

$ nvm install-latest-npm

nvm use installs the correct version for the current shell only. If you change the shell, the newly updated version of node.js will be lost.

How do I make a specific version of Node.js persistent?

The default version is one that is distributed to all shells.

Team nvm alias allows you to set the default version.

$ nvm alias default 10.16.0

For convenience, you can create a file .nvmrcwhich accepts the SemVer format, node or default… Thereafter nvm use, nvm install, nvm exec, nvm run and nvm which will use the version specified in the file .nvmrcunless otherwise specified on the command line.

$ cat .nvmrc
15.4.0
$ nvm use
Found '/Users/fuje/.nvmrc' with version <15.4.0>
Now using node v15.4.0 (npm v7.0.15)

We can check the current version with the following command:

$ nvm current
v15.4.0

ls-remote displays all available versions, but be prepared for a very long list.

$ nvm ls-remote

Note that the abbreviated version name significantly shortens the entire list.

$ nvm ls-remote 15
        v15.0.0
        v15.0.1
        v15.1.0
        v15.2.0
        v15.2.1
        v15.3.0
->      v15.4.0

nvm which specifies the path to the executable file, where nvm Was installed. We have installed versions of node.js like 10.14.0, 10.15.0 and 10.16.0… Here are the results nvm which:

$ nvm which 10.14.0
/Users/fuje/.nvm/versions/node/v10.14.0/bin/node
$ nvm which 10.15.0
/Users/fuje/.nvm/versions/node/v10.15.0/bin/node
$ nvm which 10.16.0
/Users/fuje/.nvm/versions/node/v10.16.0/bin/node
$ nvm which 10.15
/Users/fuje/.nvm/versions/node/v10.15.0/bin/node
$ nvm which 10.12
N/A: version "v10.12" is not yet installed.
You need to run "nvm install 10.12" to install it before using it.
$ nvm which 10
/Users/fuje/.nvm/versions/node/v10.16.0/bin/node

The specified version of Node.js can be used directly to launch applications:

$ nvm run 10.15.0 app.js

Alternatively, this command runs node app.js with a PATH variable pointing to the version 10.15.0

$ nvm exec 10.15.0 node app.js

If you need more nvm commands, run the command help:

$ nvm --help

NVM upgrade

We can use nvm to update node.js and npm. But how to update myself nvm?

Let’s try!

Before updating, we have installed nvm 0.34.0

We update to version 0.37.2.

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 13527  100 13527    0     0  23046      0 --:--:-- --:--:-- --:--:-- 23083
=> nvm is already installed in /Users/fuje/.nvm, trying to update using git
=> => Compressing and cleaning up git repository
=> nvm source string already in /Users/fuje/.bash_profile
=> bash_completion source string already in /Users/fuje/.bash_profile
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

As stated in the output, we need to close and reopen the terminal in order to use the new version:

$ nvm --version
0.37.2

Compared to version 0.34.0, in version 0.37.2 added function nvm set-colors for console output.

Default nvm ls shows the following colors:

Let’s set new colors:

$ nvm set-colors cgYmW

nvm ls displays output with new colors:

Conclusion

nvm simplifies node.js and npm version control. We’re now definitely ready to move to node.js 15 and npm 7. Hope you found this helpful. Other publications of the author can be found here

Similar Posts

Leave a Reply

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