Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

npm

nvm

I have two projects:

  • project A: Node 16.10.0 NPM 7.24.2
  • project B: Node 16.10.0 NPM 8.10.0

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

  • nvm use v16_npm7 for npm 7.24.2
  • nvm use v16_npm8 for npm 8.10.0

Is there a better/alternative solution?

like image 451
tonymontana Avatar asked Dec 12 '25 05:12

tonymontana


1 Answers

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]"
like image 119
Ortomala Lokni Avatar answered Dec 13 '25 23:12

Ortomala Lokni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!