Search code examples
nvmfish

Can't run nvm of Fish shell on Mac


I have installed Fish shell via Brew.

According to the instructions given on the terminal while installing Fish, I added:

  export NVM_DIR="$HOME/.nvm"
  [ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh"  # This loads nvm
  [ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm"  # This loads nvm bash_completion

to

 ~/.config/fish/config.fish

and to

~/.zprofile

On normal terminal I can run nvm however on fish it says fish: Unknown command: nvm

If I do source ~/.config/fish/config.fish I get:

/opt/homebrew/Cellar/nvm/0.39.3/libexec/nvm.sh (line 386): Unexpected ')' found, expecting '}'
    *[!/]*/)
           ^
from sourcing file /opt/homebrew/Cellar/nvm/0.39.3/libexec/nvm.sh
    called on line 3 of file /opt/homebrew/opt/nvm/nvm.sh
from sourcing file /opt/homebrew/opt/nvm/nvm.sh
    called on line 6 of file ~/.config/fish/config.fish
from sourcing file ~/.config/fish/config.fish
.: Error while reading file '/opt/homebrew/Cellar/nvm/0.39.3/libexec/nvm.sh'
/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm (line 5): Missing end to balance this if statement
if ! command -v nvm &> /dev/null; then
^^
from sourcing file /opt/homebrew/opt/nvm/etc/bash_completion.d/nvm
    called on line 7 of file ~/.config/fish/config.fish
from sourcing file ~/.config/fish/config.fish
.: Error while reading file '/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm'

What can I do to run nvm on Fish?


Solution

  • What the error message tells you is that nvm is not a fish script.

    nvm is written as a posix-compatible shell script and fish is intentionally not posix-compatible.

    This bit here:

     \. "/opt/homebrew/opt/nvm/nvm.sh" 
    

    Tells the shell to "source" nvm.sh, meaning to run it in the current shell. That won't work because fish isn't a posix shell.

    You'll have to use either another version manager like nvm.fish or use a posix-to-fish shim like bass and do something like its nvm instructions:

    bass source ~/.nvm/nvm.sh --no-use ';' nvm use iojs