nvm icon indicating copy to clipboard operation
nvm copied to clipboard

NVM in fish

Open ecbrodie opened this issue 10 years ago • 42 comments

How do I install NVM for the fish shell?

ecbrodie avatar Oct 10 '13 04:10 ecbrodie

May I request that this issue be reformatted in the form of a bug?

  • NVM does not install if you use the fish shell.
  • NVM does not work in the fish shell shell shell if previously installed in the bash shell. In fact, nvm isn't even in the path.

djensen47 avatar Oct 14 '13 16:10 djensen47

I attempted to test this out - . nvm.sh didn't work in the fish shell, even with the shebang for /bin/sh or /bin/bash.

I'm not sure if a script can be portable across bash/sh/dash/ksh and still work in fish. For example, $? to indicate success doesn't work in fish, it's $status, which none of the other shells have.

Most worrying, all the other shells' function syntax doesn't work in fish. I suspect we'd need a brand new nvm.fish to make this work properly.

ljharb avatar Apr 20 '14 05:04 ljharb

Not a self-promotion, but I did port nvm to fish, please check https://github.com/Alex7Kom/nvm-fish I rewrote it entirely since fish is very different. If nvm.fish fits in the main nvm project, I'll be happy to contribute it.

alex7kom avatar Apr 20 '14 05:04 alex7kom

@Alex7Kom does your port share the same env vars and directory of node versions and global modules that standard nvm does?

ljharb avatar Apr 20 '14 06:04 ljharb

@ljharb I tried to make it as close to standard nvm as possible, so it uses the same naming everywhere, and it should use the same directory of node versions if put in the same directory as standard nvm. However, I didn't update it for 4 months, so I'll need some time to achieve parity with nvm. I hope I understood you right.

alex7kom avatar Apr 20 '14 06:04 alex7kom

@Alex7Kom This is great! I wanted to implement nvm for fish, but never found the time to learn fish well enough to do it. You are right that a complete rewrite was needed.

/me goes off to install fish and test it.

creationix avatar Apr 21 '14 16:04 creationix

@Alex7Kom Honestly, if you can commit to keeping your fish port up to date such that it can be a seamless drop-in for standard nvm, and i could simultaneously use installed nodes in one tab with a bash shell and another with a fish shell; they read the same .nvmrc files; and if it could use git-tagged versions; etc - what I'd prefer is for the install script to be able to intelligently install your fork rather than incorporate it into this repo.

Thoughts?

ljharb avatar Apr 22 '14 06:04 ljharb

I tried it, works nice, thanks! :+1: Just a thought, python virtualenv provides bin/activate for bash-ish shells and bin/activate.fish for fish. If nvm could do something similar that would be nice. Also note that a .fish script could still use sh to delegate some work (installing etc) to the nvm bash version.

wires avatar May 28 '14 15:05 wires

https://github.com/Alex7Kom/nvm-fish has worked great for me. Would love to see the main project support fish out of the box at some point.

Globegitter avatar Aug 25 '14 00:08 Globegitter

Sorry to dredge up an old issue, but with all the recent hype about io.js, and with the latest nvm now supporting installing io.js, this has become a bigger issue for fish users again. The latest version of the nvm-fish fork is woefully outdated compared to current nvm, and so therefore can't be used to install io.js. Has any more thought been put into this?

doughsay avatar Jan 29 '15 17:01 doughsay

There's a PR open to get it to work, but fish being wildly different than most shells, some of the changes required concern me. I'm still open to the possibility.

ljharb avatar Jan 29 '15 18:01 ljharb

I've written a wrapper that lets fish people use nvm seamlessly: https://github.com/passcod/nvm-fish-wrapper :fish:

It works by running nvm in bash within a fish function, and doing some magic to pass the (relevant parts of the) environment through and export it again once nvm returns. Doing so incurs a noticeable delay (about 1 second) before nvm is run, but I think that's acceptable. Because it's just a wrapper, it will always work (unlike the nvm-fish port), requiring no maintenance while nvm can be updated at will, unless nvm changes significantly. Additionally, it uses the same nvm as other shells, so nodes (and ios) are shared (again, unlike the nvm-fish port). And it's less than two pagefuls of code ;P

It is similar in thought to #579 but less intrusive and requires no changes in nvm.

passcod avatar Feb 07 '15 21:02 passcod

I also wrote a general wrapper to bring almost any bash utility to fish shell: https://github.com/edc/bass. It works flawlessly with nvm using syntax like bass source ~/.nvm/nvm.sh \; nvm --version. For convenience, one can create an alias Fish function:

function nvm
      bass source ~/.nvm/nvm.sh ';' nvm $argv
  end

and then just use nvm --version, nvm ls, etc.

edc avatar Jul 13 '15 23:07 edc

@edc This is really cool! Does it work as well on linux and on OS X? If so I'll post a notice on nvm-fish-wrapper to use bass instead.

passcod avatar Jul 13 '15 23:07 passcod

@passcod I only tested it on OS X. Should work on Linux. It probably does not work on Windows.

edc avatar Jul 13 '15 23:07 edc

If you use Oh My Fish framework you can use foreign-env to run nvm just like bass does. In fact, the plugin is a rewrite of bass in pure fish :)

derekstavis avatar Sep 08 '15 14:09 derekstavis

@derekstavis That's a great tip, thanks. With bass I have some troubles, but with fish functions + foreign-env it works!:

# ~/.config/fish/functions/nvm.fish
# $NVM_DIR=your nvm install path
function nvm
    fenv source $NVM_DIR/nvm.sh \; nvm $argv
end

estebanprimost avatar Oct 15 '15 12:10 estebanprimost

@estebanprimost Now we have a dedicated nvm package. Just omf install nvm. Awesome fish completions included! :beers:

derekstavis avatar Oct 15 '15 12:10 derekstavis

@derekstavis Great! Thanks.

estebanprimost avatar Oct 15 '15 13:10 estebanprimost

@derekstavis it'd be helpful if you could make a PR that mentions that in the docs, just like there's a Windows option.

ljharb avatar Oct 15 '15 17:10 ljharb

OMF isn't available anymore https://github.com/github/dmca/blob/master/2016-02-04-Wahoo-2.md :S

tonatiuh avatar Mar 02 '16 18:03 tonatiuh

that's a pretty effed up takedown :-/

ljharb avatar Mar 02 '16 18:03 ljharb

Man, again? I thought they resolved this. The omf project seems to be plagued with copyright problems.

passcod avatar Mar 02 '16 18:03 passcod

Man, again? I thought they resolved this.

We also thought this was solved, but then we got striked again by dirty competition. Github should be freeing our repository in next few days, as the requester gave up on fighting.

The omf project seems to be plagued with copyright problems.

In fact we (omf project) never infringed copyright. But it's how DMCA requests work. You don't need to prove anything and Github takes down your repo to keep safe harbour protection.

derekstavis avatar Mar 02 '16 19:03 derekstavis

Yes, apologies. I really meant to say "DMCA requests" but left it as is because I didn't remember what had happened and left to research it. Then I got bored of reading about douchenozzles, but let's not hijack this thread further.

Outside people looking to use omf: find someone who already has an install and ask them for a tarball or git clone or something. Git is distributed, yay.

passcod avatar Mar 02 '16 19:03 passcod

Sorry if I sounded gross @passcod, I was affected by such a hard time in Oh My Fish. But well.. Now we are back!!

derekstavis avatar Mar 02 '16 20:03 derekstavis

Fish shell noob here. while function nvm bass source ~/.nvm/nvm.sh ';' nvm $argv end makes nvm work in fish, installed node and npm wont work properly until i execute a nvm command. How to make them available on shell startup? Thanks.

shangsunset avatar Apr 14 '16 21:04 shangsunset

Hey @shangsunset, install nvm wrapper via Oh My Fish and forget about issues. It will also allow you to use completions. Give a look at https://github.com/derekstavis/plugin-nvm

derekstavis avatar Apr 14 '16 21:04 derekstavis

@derekjkeller yea. i was using fisherman at first. ended up switching to oh-my-fish and your plugin-nvm works perfectly :)

shangsunset avatar Apr 14 '16 22:04 shangsunset

Awesome @shangsunset, I hope you like the plugin. Feel free to file a bug if you find any issue :+1:

derekstavis avatar Apr 14 '16 23:04 derekstavis

Someone reading this thread in the future will be seriously misdirected, so let me fix that.

The reason @shangsunset was having trouble with nvm has nothing to do with fisherman or omf, but incorrect nvm usage.

got bass?

In fish, there are a few ways to run nvm, IMO the best way to go about this is using bass, which lets you run (not just nvm, but also other) non-fish env-altering scripts and have your fish env/vars altered accordingly.

You don't even need fisherman / omf to do any of this.

how??

Create a dummy nvm function (save it to ~/.config/fish/functions/nvm.fish)

function nvm
    bass source ~/.nvm/nvm.sh --no-use ';' nvm $argv
end

Run this function at the start of your shell session _every time_ (or when you cd into a directory with a .nvmrc). If you don't do this, you won't be able to run node or npm since they will _not_ be in your $PATH.

okay, but i don't want to run nvm every time

If you don't want to run nvm at the start of the shell / or when cd'ing into a path with .nvmrc, then just add this to your ~/.config/fish/config.fish:

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

Related https://github.com/edc/bass/issues/28

ghost avatar Apr 16 '16 05:04 ghost

Minor correction: a .nvmrc in the directory does not guarantee that node or npm is added into the path upon entering the directory. nvm use reads from it if no node version is specified in the command.

While it is good that you got it working, knowing what goes on under the hood is also essential in debugging problems you might face in the future.

jethrokuan avatar Apr 16 '16 05:04 jethrokuan

@shangsunset wrong derek referenced in your comment earlier, but he obviously saw it.

derekjkeller avatar Apr 19 '16 20:04 derekjkeller

Hi again and thanks to @ljharb for merging https://github.com/creationix/nvm/commit/b0693f2d6a1a11bd5b95d412bdb17d69346ae680.

Running nvm in fish can be laborious, so I wrote an nvm_-like_ node version manager in fish: fin.

ghost avatar Apr 27 '16 07:04 ghost

I just use

#!/bin/bash

export NVM_SYMLINK_CURRENT=true

if [[ -s $HOME/.nvm/nvm.sh ]] ;
  then
    source $HOME/.nvm/nvm.sh;
  else
    echo "No nvm found in `$HOME/.nvm/nvm.sh`";
fi

nvm $@

And then add /usr/local/sbin $HOME/.nvm/current/bin to my path. This is why I implemented the NVM_SYMLINK_CURRENT variable for nvm in the first place

danielb2 avatar Jan 27 '17 16:01 danielb2

NVM has an example of a change directory hook to automatically call nvm use: https://github.com/creationix/nvm#calling-nvm-use-automatically-in-a-directory-with-a-nvmrc-file

If you want the same in Fish, use this adopted function: https://gist.github.com/eugenet8k/535bf3c51d1fc7c31cb8784e55d4dae4#calling-nvm-use-automatically-in-a-directory-with-a-nvmrc-file

eugenet8k avatar Aug 25 '17 18:08 eugenet8k

With this package installed, nvm works fine:

https://github.com/FabioAntunes/fish-nvm

osbre avatar Feb 11 '20 22:02 osbre

~~An alternative is to setup nvm in zsh/bash and then switch to fish (e.g. on mac): /bin/zsh -c "source ~/.zshrc && /usr/local/bin/fish -i"~~

elovin avatar Nov 18 '21 10:11 elovin

@elovin that wouldn’t leave nvm available inside fish to change the node version, which is the primary use case of nvm.

ljharb avatar Nov 18 '21 17:11 ljharb

@ljharb hm I'm not sure if I do something different in the setup then because I can switch between node versions just fine

Update: You are right, I also use the workaround with bass, for some reason I still have to call zsh first though.

elovin avatar Dec 01 '21 16:12 elovin