How to Use NVM to Manage Node.js 19 and NPM 9 | by Jennifer Fu | October, 2022

A Practical Guide to NVM

Unsplash. Photo by Per Chong Deng Jiang

Node.js 19 was released on October 18, 2022. It comes with npm 9 and many new features. Are you ready to try it?

As we said in the previous article, NVM (Node Version Manager) is the best way to upgrade node.js and npm versions. It can help us reduce the risk.

This is a rewrite with new versions of nvmnode.js, and npm.

nvm Manages node.js and npm versions. It is designed to be installed per user and implemented per shell. nvm Works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash), especially on Unix, macOS and windows WSL.

, latest version of nvm Is 0.39.2and curl it or . can be installed by wget command:

Script, install.shnvm clones the repository ~/.nvmand tries to add the source lines to the correct profile file from the snippet below (~/.bash_profile, ~/.zshrc, ~/.profileeither ~/.bashrc,

in profile file, e.g. ~/.zshrcWe see that the above lines have been added.

After nvm is installed, we can use the following command to install the latest version of node.js:

The above output (line 7) tells that npm 8.19.2 node.js . is used with 19.0.0, This can be verified by the following:

We can also specify the exact version to be installed. The semantic versioning format is defined by SemVer:

If the specific version has already been installed, it will not be installed again:

What to do to upgrade npm to version 9?

We can list all installed versions:

The arrow in the above output shows that the current version of node.js is 19.0.0 (line 11). It also shows values ​​for default ,14.17.6line 13), node ,19.0.0line 16), and stable ,19.0.0line 17).

nvm use Current version changes:

you might be surprised how v10.14.0 Uses a later version of npm (line 2) v14.20.1 (line 6). The following commands can achieve this:

As we know, npm 8 dropped support for node.js 10. Edition 10.14.0 npm. can’t install 8.0.0,

The following command will get the latest supported npm version on the current node version:

for node.js 19.0.0The latest working version of npm is 8.19.2,

nvm use Sets a specific version for the current shell. If you start a new shell the newly set node.js version will be lost.

How can we make a specific node version persistent?

The default version is the one carried over all shells. nvm alias Can set default version.

a .nvmrc The file can be created for convenience, which takes the SemVer format, or nodeeither default, Later, nvm use, nvm install, nvm exec, nvm runAnd nvm which will use the version specified in .nvmrc file if no version is provided on the command line.

We can check the current version with the following command:

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

% nvm ls-remote

More specifically, providing a partial version can reduce the available list.

nvm which Shows the path to the executable where it was installed. We have installed node.js of 10.14.0, 12.22.7, 14.17.6, 17.1.0, 18.0.0 And 19.0.0, are here nvm which Result:

A specific node version can be used to run the app directly:

% nvm run 14.17.6 app.js

Alternatively, the following command runs node app.js pointing to node along path 14.17.6,

% nvm exec 14.17.6 node app.js

If you want to find more nvm command, run the help command:

we can use nvm To upgrade node.js and npm. how can we upgrade nvm on one’s own?

let’s try.

Before the upgrade, we have nvm 0.39.0,

% nvm --version

We upgrade it to the version 0.39.2,

As stated in the output (line 10), we have to close and reopen the terminal to use the new version:

% nvm --version

nvm Makes it easy to manage node.js and npm versions. We can upgrade to node.js 19 and npm 9 as well as any previous version, listed as follows:

Thanks for reading.

Want to Connect?If you are interested, check out my directory of web development articles.

Leave a Comment