How to run "nvm" in "oh my zsh"?
In the system there is a nodejs
, installed through nvm
. The command is not running npm
.
Console is Oh my zsh
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
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
:
- Open your zsh config file
.zshrc
in nano with this command:nano ~/.zshrc
- Scroll down to where it shows
plugins=(git)
and addnvm
inside the parentheses to make it show asplugins=(git nvm)
(separate plugins with spaces) - Press
control
+O
(on macOS), thenenter
, to save, then presscontrol
+X
to exit - 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 entersource ~/.zshrc
, etc.)
Source: https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/nvm
This worked for me on Ubuntu 20.04.
Install or update nvm
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
Add in your ~/.zshrc
echo 'export NVM_DIR=~/.nvm' >> ~/.zshrc
echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"' >> ~/.zshrc
Load in the current shell environment
source ~/.zshrc
Check the nvm version
nvm -v
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
git clone https://github.com/nvm-sh/nvm.git ~/.nvm
-
cd ~/.nvm && git checkout v0.35.1
(current latest release) - Add
nvm
to your~/.zshrc
. Ex:plugins=(... nvm)
use homebrew to install nvm
-
brew install nvm
-
edit your system configuration
vim ~/.zshrc # or vim ~/.bashrc
export NVM_DIR=~/.nvm
esc > :wq
save file
-
reload the configuration
source $(brew --prefix nvm)/nvm.sh
-
view nvm version
$ nvm --version
# 0.36.0
enjoy it.