NVM doesn't stick to alias default with ZSH (oh-my-zsh)
Asked Answered
B

8

30

I had NVM installed already, then I discovered oh-my-zsh and installed that. It seems to have an nvm plugin, which I enabled in .zshrc.

Also I put this in my .zprofile

export NVM_DIR="/Users/me/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm

Now I can use nvm, but whenever I set alias default to a different version, nvm switches back the next time I open the shell.

$ iojs -v
v1.2.0
$ which iojs
/Users/me/.nvm/versions/io.js/v1.2.0/bin/iojs
$ nvm use 1.5.1
Now using io.js v1.5.1
$ nvm alias default iojs v1.5.1
default -> iojs (-> iojs-v1.5.1)
$ which iojs
/Users/me/.nvm/versions/io.js/v1.5.1/bin/iojs

Then after opening a new shell:

$ which iojs
/Users/me/.nvm/versions/io.js/v1.2.0/bin/iojs

Could my old bash install somehow mess things up? I'm on OSX btw but I guess it doesn't make a difference.

--------- edit ------ More output as requested

➜  ~  nvm version
iojs-v1.2.0
➜  ~  ls $NVM_DIR/alias
default
➜  ~  cat $NVM_DIR/alias/default
iojs
➜  ~  nvm alias iojs
iojs -> iojs-v1.5 (-> iojs-v1.5.1) (default)
➜  ~  nvm alias $(cat $NVM_DIR/alias/default)
iojs -> iojs-v1.5 (-> iojs-v1.5.1) (default)

I just found out why it's reverting to 1.2.0 I think. During installation of oh-my-zsh it seems to have taken the active PATH from my Bash shell and copied it to the .zshrc file including the active nvm path at the time:

/Users/me/.nvm/versions/io.js/v1.2.0/bin

But after removing that from PATH, now my zsh can't find any Node binary after launching a new shell. So still the question is I quess, why isn't the NVM setting remembered? I can still set it in the active shell like before, it just doesn't stick.

I'm now thinking there might be something fundamentally wrong with my oh-my-zsh intstallation. I have the git plugin enabled for example in zshrc but the command "gst" is not a valid alias.

plugins=(git, gitflow, nvm, brew, tmux)

➜  ~  gst
zsh: command not found: gst
Bronchi answered 16/3, 2015 at 8:49 Comment(4)
"whenever I set alias default to a different version" Could you explain in detail what this means? (What command did you run?)Perrine
Ok I thought is was clear for nvm users but I've added the commandsBronchi
Please attach output of nvm version, ls $NVM_DIR/alias, cat $NVM_DIR/alias/default, nvm alias iojs, and nvm alias $(cat $NVM_DIR/alias/default) after opening a new shell.Perrine
Okay, I agree that everything looks normal. My aliases look the same but I have no problem loading the right default. I would suggest first turn off the nvm plugin and test again (I'm not with my computer right now so I can't see the plugin's source code, but I personally don't have it enabled.) After that, try reinstalling oh-my-zsh and nvm.Perrine
H
29

Add below to the end of .zshrc

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

Please note that must be in the end, i don't know why

Homochromous answered 9/11, 2015 at 7:57 Comment(4)
worked for me. Though for ubuntu I used source ~/.nvm/nvm.shBlackman
Thanks for the info. Just so others know; this doesn't need to be at the end of my config in order to work.Ozoniferous
Please note that must be in the end, i don't know why This tip saves my day!Condign
This is the exact content of my .zshrc file and it's still not working. Keeps resetting my default version on every new terminal window.Ad
B
11

I have it working now. Reinstalled oh-my-zsh.

I am not sure what has changed. It seems I do need to keep the nvm plugin enabled for it to work.

Without the plugin I tried putting the nvm startup command in .zprofile

export NVM_DIR="/Users/me/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm

Although the file is loaded on creating a new shell, it didn't seem to enable NVM properly.

Also I noticed that if I make a mistake in the zsh plugin configuration in .zshrc, there is no error and the plugins simply won't work. So this is something to be careful about I guess. For example I used comma's by accident and then the plugins break without warning:

plugins=(git, gitflow, nvm)
Bronchi answered 22/3, 2015 at 21:43 Comment(0)
G
5

I am using OSX 10.11.6 with Hyper and Oh My Zsh. I had to uninstall nvm that was installed through homebrew, reinstall it through the curl command, and then add this line to the very bottom for it to work correctly. I am sure that is something with all of my plugins/setting/blah but this is what made it work for me:

source "$NVM_DIR/nvm.sh"

at the very end of my the ~/.zshrc file

Galanti answered 19/1, 2017 at 19:35 Comment(0)
E
4

As noted by other users, adding this to my ~/.zshrc file solved the problem.

# NVM Stuff
export NVM_DIR=~/.nvm
source $NVM_DIR/nvm.sh

Just wanted to emphasize that it was not working till I placed it close to the end of the file. Probably something in the middle messes up the configuration and my nvm was using a separate context for each folder.

Placing in the end solved the problem for me.

Etem answered 29/9, 2021 at 18:12 Comment(0)
P
2

If you have this line inside your ~/.zshrc file

export PATH="/usr/local/opt/node@8/bin:$PATH"

remove that line or comment out in the file.

Protuberancy answered 23/7, 2019 at 8:59 Comment(0)
D
2

As stated by brew when you install nvm, "Add the following to ~/.bash_profile or your desired shell configuration file:"

export NVM_DIR="$HOME/.nvm"
[ -s "$(brew --prefix)/opt/nvm/nvm.sh" ] && . "$(brew --prefix)/opt/nvm/nvm.sh" # This loads nvm
[ -s "$(brew --prefix)/opt/nvm/etc/bash_completion" ] && . "$(brew --prefix)/opt/nvm/etc/bash_completion" # This loads nvm bash_completion

Even though .zprofile is the equivalent of .bash_profile, for some reason, the aliases are disregarded if you add the lines to .zprofile. So add them to .zshrc instead.

See https://formulae.brew.sh/formula/nvm which has the latest version of these lines.

If that doesn't work... some people seem to be having success adding something to their PATH: https://github.com/nvm-sh/nvm/issues/1703#issuecomment-356221842.

Dewhurst answered 21/10, 2019 at 12:35 Comment(0)
M
2

With Linux (Ubuntu 20.04 - 23.04)

With your favorite editor, you edit ~/.zshrc

nano ~/.zshrc

At the end of the file your add:

# NVM
export NVM_DIR=~/.nvm
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"

And then you run:

source ~/.zshrc
Maestoso answered 23/6, 2021 at 6:24 Comment(0)
P
1

This worked for me when I added to the ~/.zprofile

export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
Pyrone answered 14/10, 2015 at 2:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.