node.jsangularnpmangular-cli

Use multiple Node versions in windows simultaneously


There are multiple question regarding this on SO but none was able to solve my problem. There is a project called NVM for Windows https://github.com/coreybutler/nvm-windows which solves this issue by switching between multiple node js. In order to use it we need to have an elevated command prompt (admin rights), but I have a corporate issued laptop and I don't have admin rights neither they will give me admin rights in cmd because then a user can do anything. I have to work on three different angular projects with different angular cli version and different nodes.


Solution

  • So I came up with my own solution to solve this problem because none of the existing blog post and SO questions helped me. I think this solution is even better then any existing third party package because you are not depending on any third party installer or script and it is extremely easy to implement.

    First go to https://nodejs.org/en/download/package-manager and download prebuilt binaries of the desired node version.enter image description here

    After that go to C:\Users<user-name>\Documents\ and create a folder nvm(any name). Extract the contents of the downloaded zip file to this folder, it will create a folder "node-v20.13.1-win-x64"(version name of downloaded node). In this folder you will have all the files you need to run node.

    After that create a powershell script file named "node-profile.ps1" and paste the following command in it

    #$env:PATH = $env:PATH.Replace($env:ProgramFiles + '\nodejs\',  $env:USERPROFILE + '\Documents\nvm\node-v22.1.0')
    $env:PATH = $env:PATH.Replace($env:ProgramFiles + '\nodejs\',  $env:USERPROFILE + '\Documents\nvm\node-v20.13.1')
    

    the path starting with a # sign is commented code. You can enter as many paths you want granted you have downloaded many node versions int the same location. It will look something like this.enter image description here

    After that open you project in visual studio code(or your preferred editor) and in the terminal run this node-profile.ps1 file like thisenter image description here

    C:\Users\<user-name>\Documents\nvm\node-profile.ps1
    

    Now for this terminal temporarily (as long as you don't close it) your node version will be the one you have set in node-profile.ps1 file i.e version 20.13.1 for this example. You can check the version by typing node -v command and it will give you the one you have just set.

    For a second project open it in another code editor and set the desired node version in that terminal and by uncommenting the desired path in node-profile.ps1 file and commenting the others, and for that terminal you will have a different node version.

    Now you can run any angular project that this version supports. You can even run legacy angular projects by this method.

    The beauty of this method is that with this method you can use multiple node versions simultaneously I mean truly simultaneously.

    In NVM for Windows when you switch between version it is set globally i.e in every terminal you will get the same node version so running multiple projects with different node version dependency is a pain.

    Hope this helps.