Install a specific version of NPM
I use nvm to manage Node.js versions on my computer and I typically install the latest version of npm that is compatible with the Node.js version. This is as simple as:
$ nvm install --lts --latest-npm
Installing latest LTS version.
...
Now using node v18.15.0 (npm v9.5.0)
Attempting to upgrade to the latest working version of npm...
...
* npm upgraded to: v9.6.4
$ node -v
v18.15.0
$ npm -v
9.6.4
or to install a specific version:
$ nvm install 16.20.0 --latest-npm
Downloading and installing node v16.20.0...
...
Now using node v16.20.0 (npm v8.19.4)
$ node -v
v16.20.0
$ npm -v
8.19.4
But sometimes a project requires a specific version of npm. This can be done using:
npm install -g npm@version
Continuing from our earlier example:
$ nvm use 16.20.0
Now using node v16.20.0 (npm v8.19.4)
$ node -v
v16.20.0
$ npm -v
8.19.4
$ npm install -g npm@8.12.0
...
$ npm -v
8.12.0
Note that this doesn’t impact other node versions:
$ nvm use 18.15
Now using node v18.15.0 (npm v9.6.4)
$ nvm use 16.20
Now using node v16.20.0 (npm v8.12.0)