The place where random ideas get written down and lost in time.

2021-11-11 - “nvm” for Multiple Node Versions

Category DEV

Right now on the laptop for RPisicine I have:

$ node --version        ⇒ v14.11.0

$ npm --version        ⇒ 6.14.8

https://github.com/nvm-sh/nvm

⇒ install.sh

$ nvm list                # takes a while...

Install steps recommend to remove “-g” (global) modules that will likely break when changing version globally:

$ nvm use system

$ npm uninstall -g nwb

$ npm uninstall -g serve

$ nvm install v14.11.0

In rpiscine:

$ node --version > .nvmrc

$ nvm use        # in that directory, to switch to that version

How does changing the Node version with nvm affect the NPM version?

⇒ The NPM version depends (and varies with) the Node version being used. E.g.:

$ nvm use node

Now using node v14.11.0 (npm v6.14.8)

$ nvm install node

Now using node v17.1.0 (npm v8.1.2)

For Windows, there’s https://github.com/coreybutler/nvm-windows

To fix the NVM PATH in CMD.exe: Not really an option when installed using nvm-sh since the latter does not create an “nvm.exe” or “nvm.sh” -- instead all the nvm commands are bash functions loaded in the interpreter (meh). But we can work around it:

  • cygwin: $ cygpath -w $(dirname $(nvm which --silent))
    • ⇒ C:\Ralf\.nvm\versions\node\v18.8.0\bin
    • Copy path to a CMD.exe.
  • cmd:         $ refreshenv                ← optional, because I have chocolatey installed.
    • $ set PATH=C:\Ralf\.nvm\versions\node\v18.8.0\bin;%PATH%
    • $ echo %PATH%


 Generated on 2025-01-18 by Rig4j 0.1-Exp-f2c0035