nvm-sh/nvm

NVM in fish

ecbrodie opened this issue ยท 46 comments

How do I install NVM for the fish shell?

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.

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.

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 does your port share the same env vars and directory of node versions and global modules that standard nvm does?

@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 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.

@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?

I tried it, works nice, thanks! ๐Ÿ‘ 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.

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.

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?

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.

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

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.

edc commented

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 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.

edc commented

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

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 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 Now we have a dedicated nvm package. Just omf install nvm. Awesome fish completions included! ๐Ÿป

@derekstavis Great! Thanks.

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

that's a pretty effed up takedown :-/

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

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.

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.

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

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.

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

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

Awesome @shangsunset, I hope you like the plugin. Feel free to file a bug if you find any issue ๐Ÿ‘

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 edc/bass#28

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.

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

Hi again and thanks to @ljharb for merging b0693f2.

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

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

osbre commented

With this package installed, nvm works fine:

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

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 that wouldnโ€™t leave nvm available inside fish to change the node version, which is the primary use case of nvm.

@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.

I own this build with Robbie Jimenez.

alasly alasyl

I have written a fish function which basically executes nvm in a bash login shell and sets the path accordingly:
https://gist.github.com/calle2010/b3f0054c1d4b72394d0fda7f22d47b38
Since it modifies the $fish_users_paths variable the node installation is also available in a new shell without executing anything. So far this works for me.

Add the following to the fish config and restart the terminal (or reload the config):

function nvm
    bash -c "source ~/.nvm/nvm.sh; nvm $argv"
end

just use fnm, it works fine on Fishshell.

source /home/meotimdihia/.config/fish/conf.d/fnm.fish

image