nvm-sh / nvm

Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions
MIT License
78.9k stars 7.9k forks source link

NVM in fish #303

Open ecbrodie opened 10 years ago

ecbrodie commented 10 years ago

How do I install NVM for the fish shell?

djensen47 commented 10 years ago

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

ljharb commented 10 years ago

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.

alex7kom commented 10 years ago

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.

ljharb commented 10 years ago

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

alex7kom commented 10 years ago

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

creationix commented 10 years ago

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

ljharb commented 10 years ago

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

wires commented 10 years ago

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.

Globegitter commented 10 years ago

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.

doughsay commented 9 years ago

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?

ljharb commented 9 years ago

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.

passcod commented 9 years ago

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.

edc commented 9 years ago

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.

passcod commented 9 years ago

@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 9 years ago

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

derekstavis commented 9 years ago

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 :)

estebanprimost commented 8 years ago

@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
derekstavis commented 8 years ago

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

estebanprimost commented 8 years ago

@derekstavis Great! Thanks.

ljharb commented 8 years ago

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

tonatiuh commented 8 years ago

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

ljharb commented 8 years ago

that's a pretty effed up takedown :-/

passcod commented 8 years ago

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

derekstavis commented 8 years ago

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.

passcod commented 8 years ago

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.

derekstavis commented 8 years ago

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

shangsunset commented 8 years ago

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.

derekstavis commented 8 years ago

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

shangsunset commented 8 years ago

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

derekstavis commented 8 years ago

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

ghost commented 8 years ago

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

jethrokuan commented 8 years ago

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.

derekjkeller commented 8 years ago

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

ghost commented 8 years ago

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.

danielb2 commented 7 years ago

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

eugenet8k commented 7 years ago

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

osbre commented 4 years ago

With this package installed, nvm works fine:

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

elovin commented 2 years ago

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"

ljharb commented 2 years ago

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

elovin commented 2 years ago

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

Toesandfeet commented 1 year ago

I own this build with Robbie Jimenez.

alaslyalasel commented 1 year ago

alasly alasyl

vladimiremi commented 2 months ago

Thanks @Globegitter has worked me too :

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