How to get Fish shell and NVM both installed with Homebrew to work together?

7

2

I am trying to manage my packages for OSX using Homebrew as much a I can. I have installed both the Fish Shell and NVM using Homebrew but cannot get Fish to recognize my NVM installation. NVM commands are run fine when run with Bash, probably because when you install NVM using Homebrew it had me add some extra lines to my .bashrc file.

After installing NVM with Homebrew it says to:

Add NVM's working directory to your $HOME path (if it doesn't exist):

mkdir ~/.nvm

Add the following to $HOME/.bashrc, $HOME/.zshrc, or your shell's
equivalent configuration file:

export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh

Also it appears the Homebrew install script has added this line to my ~/.profile

source /usr/local/opt/nvm/nvm.sh

I hope that it would be possible to use this same technique with Fish to also gain access to the NVM installation, but I cannot seem to get this to work.

I have found this technique: https://github.com/passcod/nvm-fish-wrapper But I would rather not have to wrap NVM.

Can anyone help?

Sam

Posted 2015-04-23T15:16:27.433

Reputation: 81

You have to either wrap nvm or create /usr/local/opt/nvm/nvm.fish. fish is a different language than sh, so you have to do something to get nvm to emit fish syntax – glenn jackman – 2015-04-23T16:00:28.360

Ahhh I see that makes sense, I completely missed that. I will just use the fish wrapper then or just install NVM not using homebrew. Thank you @glennjackman ! – Sam – 2015-04-23T16:24:45.847

Answers

6

You can use Bass. Clone the git repository

git clone https://github.com/edc/bass.git 

Then cd in the cloned directory and type

make install
bass source ~/.nvm/nvm.sh ';' nvm use stable

Now you should be able to use node inside fish shell.

Sanjay Karki

Posted 2015-04-23T15:16:27.433

Reputation: 176

~/.nvm/nvm.sh is not the right path for a homebrew installation. It should be (brew --prefix nvm)/nvm.sh or /usr/local/opt/nvm/nvm.sh – d0x – 2018-11-21T18:48:03.130

22

If you're using oh my fish you can install the nvm plugin (after installing NVM with homebrew).

$ omf install nvm

Then set the NVM_DIR and all should be fine.

$ set -gx NVM_DIR (brew --prefix nvm)

George

Posted 2015-04-23T15:16:27.433

Reputation: 424

not sure why the downvote, this is what I am currently using … – George – 2016-01-21T06:16:13.107

Every other option on the internet was like step1... step2... step45... Thanks for a quick simple solution – codenamejames – 2018-05-14T15:28:03.810

2

I've created the following function inside ~/.config/fish/functions/nvm.fish:

function _nvm
    bass source (brew --prefix nvm)/nvm.sh --no-use ';' nvm $argv
end

function nvm    
    if test -e $NVM_CURRENT
        echo Setting up nvm..

        _nvm use default --silent

        set -x -g NVM_CURRENT (_nvm current)

        printf "Using Node %s\n" $NVM_CURRENT
    end

    _nvm $argv
end

After having several issues with other plugins, this just works.

iamsaitam

Posted 2015-04-23T15:16:27.433

Reputation: 21

Works like sharm! I replaced (brew --prefix nvm)/nvm.sh by the direct path /usr/local/opt/nvm/nvm.sh to improve the initial setup time by 3/4. – d0x – 2018-11-21T18:46:35.517

I also ended up doing the same, when I noticed it was taking 2+ seconds to get a new session going. – iamsaitam – 2019-06-15T14:16:11.183