Skip to content

Instantly share code, notes, and snippets.

@eugenet8k
Last active February 23, 2024 03:10
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eugenet8k/535bf3c51d1fc7c31cb8784e55d4dae4 to your computer and use it in GitHub Desktop.
Save eugenet8k/535bf3c51d1fc7c31cb8784e55d4dae4 to your computer and use it in GitHub Desktop.
Support NVM in Fish Shell

Install Fish

https://fishshell.com

brew install fish

Set it as default macOS shell https://stackoverflow.com/questions/453236/how-to-set-my-default-shell-on-mac/20506404#20506404

Install Oh My Fish

https://github.com/oh-my-fish/oh-my-fish

curl -L https://get.oh-my.fish | fish

Install NVM plugin

omf install nvm

Calling nvm use automatically in a directory with a .nvmrc file

This is based on original zsh function. Put this into your ~/.config/fish/config.fish to call nvm use automatically whenever you enter a directory that contains an .nvmrc file with a string telling nvm which node to use:

function __check_nvm --on-variable PWD --description 'Do nvm stuff'
  set node_version (nvm version)

  if test -f .nvmrc
    set nvmrc_node_version (nvm version (cat .nvmrc))

    if [ $nvmrc_node_version = "N/A" ]
      nvm install
    else if [ $nvmrc_node_version != $node_version ]
      nvm use
    end
  else if [ $node_version != (nvm version default) ]
    echo Reverting to nvm default version
    nvm use default
  end
end

# To check current dir upon Fish session start
__check_nvm

Upper function tend to be pretty slow since it's triggered on every cd. There is an alternavite version that only works if there is .nvmrc in current dir (oh my, much faster!):

function __check_nvm --on-variable PWD --description 'Do nvm stuff'
  if test -f .nvmrc
    set node_version (nvm version)
    set nvmrc_node_version (nvm version (cat .nvmrc))

    if [ $nvmrc_node_version = "N/A" ]
      nvm install
    else if [ $nvmrc_node_version != $node_version ]
      nvm use
    end
  end
end

__check_nvm

Install NVM

https://github.com/creationix/nvm#installation

Update 2021

Latest editon that assumes the updated API of nvm utility:

function __check_nvm --on-variable PWD --description 'Do nvm stuff'
  if test -f .nvmrc
    set node_version (node -v)
    set nvmrc_node_version (nvm list | grep (cat .nvmrc))

    if set -q $nvmrc_node_version
      nvm install
    else if string match -q -- "*$node_version" $nvmrc_node_version
      # already current node version
    else
      nvm use
    end
  end
end
@kagia
Copy link

kagia commented Feb 17, 2018

We can still have the functionality without the performance hit:

function __check_nvm --on-variable PWD --description 'Detect node version'
  if test -f .nvmrc
    set node_version (nvm version)
    set nvmrc_node_version (nvm version (cat .nvmrc))

    if [ $nvmrc_node_version = "N/A" ]
      nvm install
    else if [ $nvmrc_node_version != $node_version ]
      nvm use
    end

    set -gx X_NVM_DIRTY_FLAG 1
  else if set -q X_NVM_DIRTY_FLAG; and [ (node --version) != (nvm version default) ]
    nvm use default
    set -e X_NVM_DIRTY_FLAG
  end
end

@tombl
Copy link

tombl commented Jan 15, 2019

I have created an updated version of this with my install method and a function I also ported from the zsh one that does more that this port, like recursively checking all parent directories too, so you can cd into a child directory of a project and keep it's node version.
You can find my fork here here.

@alphacornutum
Copy link

This is my current take on it:

function __check_nvm --on-variable PWD --description 'Do nvm stuff'
  if test -f .nvmrc
    set node_version (node -v)
    set node_version_target (cat .nvmrc)
    set nvmrc_node_version (nvm list | grep $node_version_target)

    if string match -q -- "*$node_version" $nvmrc_node_version
      # already current node version
    else if not set -q $nvmrc_node_version
      # install
      nvm install $node_version_target
    else
      nvm use $node_version_target
    end
  end
end
__check_nvm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment