nvm: Powershell : nvm : The term 'nvm' is not recognized as the name of a cmdlet

  • Operating system and version: Ubuntu 18.4
  • How did you install nvm? (e.g. install script in readme, Homebrew):

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

  • What steps did you perform?

Install nvm

nvm install 8.11.3 nvm install 12.3.1

ran nvm / npm and noted help text

pwsh

ran npm noted help text

ran nvm nvm : The term ‘nvm’ is not recognized as the name of a cmdlet

  • What happened?

error displays nvm : The term ‘nvm’ is not recognized as the name of a cmdlet

  • What did you expect to happen?

I thought i could execute nvm via Powershell and access its functionality as part of our build scripts.

Path: /opt/microsoft/powershell/6:/home/jonvaughan/.nvm/versions/node/v12.3.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

@darthwalsh Thanks for sharing!

This solved not only the issue of being able to run the nvm command in pwsh, but for me it also allowed my PowerShell session to recognize the node and npm commands in my WSL2.

My abbreviated ~/.config/powershell/profile.ps1:

# Based on: https://github.com/nvm-sh/nvm/issues/2058#issuecomment-735551849
function Initialize-NvmPath {
    $ENV:NVM_DIR = "$HOME/.nvm"
    $bashPathWithNvm = bash -c 'source $NVM_DIR/nvm.sh && echo $PATH'
    $env:PATH = $bashPathWithNvm
}

function nvm {
    $quotedArgs = ($args | ForEach-Object { "'$_'" }) -join ' '
    $command = 'source $NVM_DIR/nvm.sh && nvm {0}' -f $quotedArgs
    bash -c $command
}

# ....

Initialize-NvmPath

You can use nvm from powershell, you just need to proxy nvm commands to a posix subshell. You don’t get any variable manipulation from the subshell, but you can take the new path from the subshell and set powershell’s variable, by putting this in your profile.ps1:

$ENV:NVM_DIR = "$HOME/.nvm"
function nvm() {
  $quotedArgs = ($args | ForEach-Object { "'$_'" }) -join ' '
  
  zsh -c "source /usr/local/opt/nvm/nvm.sh && nvm $quotedArgs && echo __PATH_AFTER__`$PATH" | Tee-Object -Variable zsh_output | Where-Object { -not ($_ -match "^__PATH_AFTER__") }

  $path_after = $zsh_output | Select-String "^__PATH_AFTER__(.+)"
  if ($path_after) {
    $ENV:PATH = $path_after.Matches.Groups[1].Value
  }
}
nvm --version | Out-Null # Load your current nvm path

One caveat is that sourcing nvm.sh during profile load takes about 500ms on my MBP, so you could remove the last line if you don’t use node that often and manually run nvm if you want node on your path.

I’m with you, thanks for hammering home the point and for your support. NVM won’t work in my scenario.