0

I've installed NVM for windows, so I can switch between different versions of node when I have to. However, I have a need to be able to call one version of node / npm WITHOUT having to switch

For example, right now I have 2 versions of node installed: 10.17.0 and 8.9.1. I can easily switch between them using nvm use 10.17.0 or nvm use 8.9.4, and after either of those commands, when I run node ./app.js or npm install package, it uses the expected version of node / npm to do what I'm asking

But I want to create a situation where I can avoid switching, and instead I can just type node10 ./app.js or npm10 install package, and it uses the version I specify

Now, I've done a bit of research myself about what it would take to do this, so I'll lay that out and hope that someone has some advice because they've done something similar.

First of all: nvm installs both versions of node in a specific place, and that place should stay static and unchanging. I can find the 8.9.1 versions of node and npm here: C:\Users\SamuelR\AppData\Roaming\nvm\v8.9.1

I can find the 10.17.0 versions here: C:\Users\SamuelR\AppData\Roaming\nvm\v10.17.0

When I use nvm use 10.17.0, what it does is it changes a symlink at C:\Program Files\nodejs, and it points it to whichver of those two folders I specify

So my idea to make node8 / node10 and npm8 / npm10 commands work was this:

In some folder in my PATH, I was going to create 4 .bat files, node8.bat npm8.bat node10.bat npm10.bat. For node8.bat, I was going to follow the example here:

C:\Users\SamuelR\AppData\Roaming\nvm\v8.9.1\node.exe %*, so that it always forwards all my command line arguments to the correct installation.

For npm, the file npm8.bat would have C:\Users\SamuelR\AppData\Roaming\nvm\v8.9.1\npm.cmd %*.

Maybe I'd do the same for npx. And then I'd repeat those steps for the node10.bat npm10.bat files.

Is this the right way to go about this? Does it even make sense?

TKoL
  • 13,158
  • 3
  • 39
  • 73

1 Answers1

1

Welp, nobody's giving any answers, but I've tried it myself and as far as I can tell, it works!

I'll just lay it out for total clarity here: once you have nvm working as you like, find the install directory, in my case it was C:\Users\SamuelR\AppData\Roaming\nvm

For each version of node in there that you want to make these shortcuts to, make the following files,

node{v}.bat (eg node10.bat)
npm{v}.bat (eg npm10.vat)
npx{v}.bat (eg npx10.vat)

Put them all in a file that is accessible in your PATH environment variable (I made a new folder called C:\Program Files\nodelinks and put it in my path myself)

inside node10.bat:
C:\Users\SamuelR\AppData\Roaming\nvm\v10.17.0\node.exe %*

inside npm10.bat:
C:\Users\SamuelR\AppData\Roaming\nvm\v10.17.0\npm %*

inside npx10.bat:
C:\Users\SamuelR\AppData\Roaming\nvm\v10.17.0\npx %*

As far as I can tell, it passes all arguments through as expected, there's no apparent permissions issue, it works exactly as expected.

TKoL
  • 13,158
  • 3
  • 39
  • 73