4

I used brew to install nvm on macOS, then I used nvm to install node 8.9.1 and it works fine, until I load tmux, then I get the following messages:

nvm is not compatible with the npm config "prefix" option: currently set to "/usr/local"
Run `npm config delete prefix` or `nvm use --delete-prefix v8.9.1 --silent` to unset it.

After some troubleshooting I noticed that when I use tmux it is using a different npm.

Not using tmux:

~ which npm
/Users/mario/.nvm/versions/node/v8.9.1/bin/npm
~ npm config get prefix
/Users/mario/.nvm/versions/node/v8.9.1
~ echo $NVM_DIR
/Users/mario/.nvm

Using tmux:

~ which npm
/usr/local/bin/npm
~ npm config get prefix
/usr/local
~ echo $NVM_DIR
/Users/mario/.nvm

As per the installation note in brew, I added the following to my .zshrc

export NVM_DIR="$HOME/.nvm"
. "/usr/local/opt/nvm/nvm.sh"

Additionally, if I manually source /usr/local/opt/nvm/nvm.sh within tmux it works as expected.

Using tmux:

~ which npm
/usr/local/bin/npm
~ . /usr/local/opt/nvm/nvm.sh
~ which npm                       
/Users/mario/.nvm/versions/node/v8.9.1/bin/npm
~ npm config get prefix
/Users/mario/.nvm/versions/node/v8.9.1

Can anyone provide any insight into what could be causing this? I'm happy to provide additional info as necessary.

Versions:

  • macOS 10.13.1
  • zsh 5.4.2
  • tmux 2.6
  • nvm 0.33.6
  • node 8.9.1
  • npm 5.5.1
Mario
  • 1,999
  • 2
  • 15
  • 28
  • 1
    Prior to using nvm, had you installed Node.js using another method such as brew? Also, are you certain your `.zshrc` is being sourced by tmux? Perhaps check your PATH inside and outside of tmux. May also be helpful to post your `~/.tmux.conf` – Fissure King Nov 29 '17 at 13:48
  • @FissureKing I'm certain .zshrc is being sourced, but I think you might be right about another node being installed. I'm using a Brewfile and two packages seemed to have required node and may be causing the conflict. Post your comment as an answer, if cleaning up the brew installed node dependency fixes the issue, I'd love to mark your answer as correct. – Mario Nov 30 '17 at 15:33
  • sure, I'll write that up. However, while removing the offending package may produce the correct result, i.e. `npm config get prefix` working, it does not explain *why* tmux and normal shell access are providing different results for `which npm`. I suspect this may be due to `path_helper` prepending default values to your PATH. See https://superuser.com/questions/544989/does-tmux-sort-the-path-variable – Fissure King Nov 30 '17 at 16:02

2 Answers2

5

You should use your nvm.sh under your NVM_DIR

Following is my nvm related config in .zshrc

export NVM_DIR=~/.nvm
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
nvm use default

If you feel it's slow every time you open a new Tmux due to nvm initialization, you can use this lazynvm technic

Ryan Wu
  • 5,963
  • 2
  • 36
  • 47
2

You have a Node homebrew package installed outside of and in addition to nvm. nvm is correctly prepending your PATH to resolve the correct versions of Node and npm, hence your working result:

~ which npm
/Users/mario/.nvm/versions/node/v8.9.1/bin/npm
~ npm config get prefix
/Users/mario/.nvm/versions/node/v8.9.1
~ echo $NVM_DIR
/Users/mario/.nvm

However, tmux is causing your PATH to be modified such that the undesired, non-nvm versions are resolved. path_helper is the likely culprit here.

You can either remove the offending homebrew package or ensure the correct versions are resolved by disabling or tweaking the result of path_helper.

The former solution may open another can of worms as you've indicated it is installed via brewfile, so let's look at the latter.

One potential solution is to manually unset your PATH before path_helper runs. Another solution may be to modify /etc/paths to remove /usr/local/bin.

Fissure King
  • 1,250
  • 7
  • 17
  • 1
    Ultimately, I uninstalled the two packages (heroku and yarn) and node from brew, as well as all traces of node/nvm/npm. Then I reinstalled nvm in brew and now everything seems to work as expected (no warnings in tmux). Since those packages are dependant on node, I figured it's just easier to install them through npm. – Mario Nov 30 '17 at 18:54