tonymontana
tonymontana

Reputation: 5823

NVM - How to install same Node version but different global versions of NPM using NVM

I have two projects:

Both require the same version of Node but different versions of NPM. I am wondering if it can be achieved with NVM.

I was able to do it (on MacOS) but it is rather hacky

nvm use v16.10.0
npm -v      # 7.24.2
which node  # /Users/useriko/.nvm/versions/node/v16.10.0/bin/node

# duplicate v16.10.0 folder
ditto ~/.nvm/versions/node/v16.10.0 ~/.nvm/versions/node/v16.10.0_npm8

nvm use v16.10.0_npm8
npm i -g [email protected]

nvm alias v16_npm7 v16.10.0
nvm alias v16_npm8 v16.10.0_npm8

now I can toggle between them

Is there a better/alternative solution?

Upvotes: 4

Views: 1300

Answers (1)

Ortomala Lokni
Ortomala Lokni

Reputation: 62466

A better solution would be to have the npm version defined in a per-project .nvmrc file.

There is a nvm feature request asking for this: https://github.com/nvm-sh/nvm/issues/964.

In the meantime, a proposed workaround is to define a preinstall script in your package.json , installing the npm version you need such as:

"preinstall": "npm i -g [email protected]"

Upvotes: 1

Related Questions