1

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)?

João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109
  • 1
    `Set-Alias node C:\Users\JO52900\nodeJS\node-v14.18.0-win-x64\node.exe` – Mathias R. Jessen Oct 07 '21 at 15:58
  • @MathiasR.Jessen that's a simple solution, but it may not be comprehensive enough, because any non-PowerShell code that launches executable `node` would still invoke `C:\Program Files\nodejs\node.exe`, and I suspect that includes `npm`. – mklement0 Oct 07 '21 at 17:39

1 Answers1

2

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 $PROFILE file (but note that PowerShell instances launched with -NoProfile will not see this modification).

  • Unfortunately, modifying the registry-based user-level Path environment-variable definition for all processes (interactively via sysdm.cpl / programmatically via [System.Environment]::SetEnvironmentVariable('Path', $newVal, 'User')[1]) is not an option in this case, because the effective $env:Path value 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.

mklement0
  • 382,024
  • 64
  • 607
  • 775