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 configure 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) to 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 repository nvm in ~/.nvm



and tries to add the source lines from the following fragment 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 can 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 data of the above example indicates that npm 7.0.15



used 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 determined by the 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 output examples above, the symbol ->



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 be asking how is it 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.



The command nvm alias



allows you to set the default version.



$ nvm alias default 10.16.0
      
      







For convenience, you can create a file .nvmrc



that accepts the SemVer format, node



or default



. After that nvm use



, nvm install



, nvm exec



, nvm run



and nvm which



will use the version specified in the file .nvmrc



, unless another 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



indicates the path to the executable where it 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 is run node app.js



with the PATH variable indicating 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



node.js and npm to update. But how do I 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 the version 0.34.0



, the version has 0.37.2



added a function nvm set-colors



for output to the console. Shows the following colors by







default nvm ls



:







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 by the author can be found here .



All Articles