When using tmux nvm isn't being sourced

倖福魔咒の 提交于 2019-12-24 10:47:47

问题


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

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/47442647/when-using-tmux-nvm-isnt-being-sourced

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!