Search code examples
powershellnpmnvm

Powershell doesn't recognize npm after nvm change version of node


I have problem with my powershell script. I want to build one project which is based on node 10.17.0 and copy the result into other project which is based on node 8.11.4 and run the project.

   cd $PathToWebLibs

   Write-Host "..........Switching to node v10.17.0.........." -ForegroundColor Magenta
   nvm use 10.17.0

   Write-Host "..........Building WebLibs.........." -ForegroundColor Magenta
   npm run build_lib

   Write-Host "..........Copying files from ($PathToWebLibs\dist\rsp\core-ui) to ($PathToSFP\node_modules\@rsp) .........." -ForegroundColor Magenta

   cp -Recurse -Force ($PathToWebLibs + "\dist\rsp\core-ui") ($PathToSFP + "\node_modules\@rsp")

   cd $PathToSFP

   Write-Host "..........Switching to node v8.11.4.........." -ForegroundColor Magenta
   nvm use 8.11.4


   Write-Host "..........Starting SFP.........." -ForegroundColor Magenta
   npm run start

The problem is that when nvm changes version of node npm is not recognized. When I did it manually simply type in commands one by one it works.

I can just add that system environmental path is set correctly. I checked it. enter image description here


Solution

  • nvm is designed to be run in-process by your shell, which is only supported for POSIX-compatible shells such as bash, and not for PowerShell:

    nvm works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash), in particular on these platforms: unix, macOS, and windows WSL.

    On Unix-like platforms and possibly WSL, consider Node.js version manager n as an alternative, which doesn't rely on modifying the current shell's environment.

    • n-install allows installation of n directly from GitHub; for instance:
      curl -L https://git.io/n-install | bash
    • However, this particular installation method, which includes installation of Node.js itself, currently requires additional configuration in PowerShell (in your PowerShell $PROFILE file or, alternatively via persistent environment-variable definitions in the registry on Windows): $HOME/n/bin must be added to $env:Path, and $env:PREFIX must be set to $HOME/n (adjust the paths for WSL accordingly, if you're running PowerShell from outside WSL).