How to run “nvm” in “oh my zsh”?

≯℡__Kan透↙ 提交于 2020-01-30 14:15:36

问题


In the system there is a nodejs, installed through nvm. The command is not running npm. Console is Oh my zsh


回答1:


You can use zsh-nvm or enable it yourself by adding following lines to your ~/.zshrc

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

Extra:

For faster shell initialization, I use lazynvm which only loads node when needed

lazynvm() {
  unset -f nvm node npm
  export NVM_DIR=~/.nvm
  [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
}

nvm() {
  lazynvm 
  nvm $@
}

node() {
  lazynvm
  node $@
}

npm() {
  lazynvm
  npm $@
}

Reference: Lazy load nvm for faster shell start




回答2:


Switching from Bash to Oh-My-Zsh

If you already have nvm installed and you're switching from bash to oh-my-zsh you can simply open up your .zshrc file and add the nvm plugin that is included with oh-my-zsh:

  1. Open your zsh config file.zshrc in nano with this command: nano ~/.zshrc
  2. Scroll down to where it shows plugins=(git) and add nvm inside the parentheses to make it show as plugins=(git nvm) (separate plugins with spaces)
  3. Press control + O (on macOS), then enter, to save, then press control + X to exit
  4. Then open a new terminal window/tab and enter nvm ls to confirm it works. Note that you must open a new window/tab for your shell to use the newly updated .zshrc config (or enter source ~/.zshrc, etc.)

Source: https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/nvm




回答3:


I discovered that there is a nvm plug-in shipping with oh-my-zsh (that's different from lukechilds plugin). After short inspection, I think it adds the necessary modifications to .zshrc when loading, so simply adding nvm to the plugins list in .zshrc should work as well (and it does for me).

I did not find any more details on that default nvm plugin via google so I don't know whether this is the "go-to" solution.




回答4:


A much easier solution is to use the nvm plugin that is shipped by default:

It also automatically sources nvm, so you don't need to do it manually in your .zshrc

  1. git clone https://github.com/nvm-sh/nvm.git ~/.nvm
  2. cd ~/.nvm && git checkout v0.35.1 (current latest release)
  3. Add nvm to your ~/.zshrc. Ex: plugins=(... nvm)


来源:https://stackoverflow.com/questions/47009776/how-to-run-nvm-in-oh-my-zsh

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