3

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

mackosz
  • 139
  • 1
  • 8
  • Does this answer your question? ['npm' is not recognized as internal or external command, operable program or batch file](https://stackoverflow.com/questions/20992723/npm-is-not-recognized-as-internal-or-external-command-operable-program-or-bat) – Theo Mar 25 '20 at 11:29
  • @Theo yes, I did this, but for me it isn't work. The problem is not with npm but with powershell I think. When I run simple script like only change node by nvm and then run "npm -v" and i run this script in powershell it isn't work. When I do it manually and run it one by one, everything is ok. – mackosz Mar 25 '20 at 12:03

2 Answers2

1

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).
mklement0
  • 382,024
  • 64
  • 607
  • 775
0

I solved this problem using powershell Start-Sleep method after nvm use XXX. It helped and it works well currently.

mackosz
  • 139
  • 1
  • 8