I have no admin rights in my Windows PC, and the admin has already made a Node installation at C:\Program Files\nodejs, but this is version 12:
PS C:\Users\JO52900> node -v
v12.18.3
PS C:\Users\JO52900> Get-Command node
CommandType Name Version Source
----------- ---- ------- ------
Application node.exe 12.18.3.0 C:\Program Files\nodejs\node.exe
I want now to update Node without admin rights, so I manually installed version 14 in C:\Users\JO52900\nodeJS\node-v14.18.0-win-x64
C:\Users\JO52900\nodeJS\node-v14.18.0-win-x64> .\node.exe -v
v14.18.0
Now I tried adding this folder to path:
$env:Path = "; C:\Users\JO52900\nodeJS\node-v14.18.0-win-x64"
but it does't work, Get-Command node always returns C:\Program Files\nodejs\node.exe.
TLDR: how can I override the default Node path, without admin rights, such that NPM and Powershell 'node' command use only the the new NodeJS folder which I have full access to (C:\Users\JO52900\nodeJS\node-v14.18.0-win-x64)?
CodePudding user response:
When the system looks for executables specified by name only in the directories listed in the $env:PATH environment variable, the order in which they are listed matters: The first directory in which the executable is found is used.
Therefore, you must prepend rather than append your custom path:
$env:Path = "C:\Users\JO52900\nodeJS\node-v14.18.0-win-x64;$env:PATH"
To make this change for all future PowerShell sessions, add the above command to your
$PROFILEfile (but note that PowerShell instances launched with-NoProfilewill not see this modification).Unfortunately, modifying the registry-based user-level
Pathenvironment-variable definition for all processes (interactively viasysdm.cpl/ programmatically via[System.Environment]::SetEnvironmentVariable('Path', $newVal, 'User')[1]) is not an option in this case, because the effective$env:Pathvalue that processes see is a composite value, in which the machine-level value comes first.
[1] Actually, updating REG_EXPAND_SZ-based environment values such as Path with this method can have unexpected side effects; the proper solution, unfortunately, requires direct registry access - see this answer for details.
