116

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

1

9 Answers 9

184

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

5
  • 1
    If anyone is using Vim for development, the lazy loading causes autocompletion using tsserver to fail because tsserver won't be found in PATH. Best to just use eager loading IMHO.
    – geoyws
    Oct 27, 2019 at 3:48
  • If I add this to ~/.zshrc, I get "command not found" when running nvm? How do I use this? Oct 21, 2020 at 10:53
  • make sure your .zshrc has been loaded: you can type "lazy" and press Tab to see if the ZSH auto-completion shows you lazynvm. if not, check your shell config or ZSH config to make sure it's loading the .zshrc
    – Ryan W
    Oct 23, 2020 at 14:04
  • If using oh-my-zsh, the nvm plugin already has lazy loading implemented, so skip to the next answer.
    – javs
    Feb 17, 2022 at 16:38
  • For anyone wondering [ -s "$NVM_DIR/nvm.sh" ] => -s FILE - True if the FILE exists and has nonzero size Nov 8, 2022 at 18:19
182

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

7
  • 1
    Thanks for the detailled answer. I think the only way to improve this answer is: Adding a Header (Moved from Bash to zsh) and add some linebreaks
    – duichwer
    Aug 29, 2019 at 14:10
  • 1
    Thank you for the suggestions @duichwer. I just made those edits.
    – Spencer
    Aug 30, 2019 at 3:13
  • 1
    This is the correct and proper answer, please ignore the others lol.
    – geoyws
    Oct 27, 2019 at 16:12
  • you are a savior Sep 20, 2021 at 5:41
  • we ran into this issue because we had oh-my-zsh on top of basic zsh. Thanks for the answer. Jun 20 at 17:35
41

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

Run the following commands in your terminal to add to 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
0
20

use homebrew to install nvm

  1. brew install nvm

  2. edit your system configuration

    vim ~/.zshrc     # or  vim ~/.bashrc
    export NVM_DIR=~/.nvm

esc > :wq
save file

  1. reload the configuration
    source $(brew --prefix nvm)/nvm.sh

  2. view nvm version

$ nvm --version
# 0.36.0

enjoy it.

3
  • 3
    source $(brew --prefix nvm)/nvm.sh was the missing piece for me. Thanks!
    – Aurelio
    Dec 2, 2020 at 13:43
  • Thanks, this solved my issue. Glad I tried this before doing any reinstallation. Jun 19, 2021 at 0:46
  • wth is :wq ????
    – MehmedB
    Apr 26 at 18:12
17

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)
1
  • This really helped me. Thanks.
    – Yunus Emre
    Jan 6, 2021 at 19:44
11

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.

3
  • 1
    As of this writing, adding nvm as a plugin did not work for me on a Mac (OS should not be a factor, though). I still get zsh: command not found: nvm. I'll try adding manually.
    – Mike S.
    Jun 20, 2020 at 12:54
  • I'm having the same issue. May I know what you mean by adding manually? Jun 19, 2021 at 0:30
  • To get zsh-nvm working, I had to find the section in the README how to install plugin. Steps involved: git clone https://github.com/lukechilds/zsh-nvm ~/.oh-my-zsh/custom/plugins/zsh-nvm and I had to edit the ~/.zshrc file and to add the plugin plugins=( git zsh-nvm ). Loaded new terminal and nvm list finally worked. Nov 18, 2022 at 22:29
8

With Linux (Ubuntu 20.04, 22.04, 22.10 and 23.04)

With your favorite editor, you edit ~/.zshrc

nano or vi ~/.zshrc

At the end of the file, you add :

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

And then you run :

source ~/.zshrc
7

Add this code to .zshrc on your user directory

export NVM_DIR="$HOME/.nvm"
  [ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"  # This loads nvm
  [ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion

Then run this code on your terminal:

source ~/.zshrc
2

I strongly suggest using christophemarois' approach to lazy loading nvm (node, npm and global packages) in order to avoid slow shell starting times:

# Add every binary that requires nvm, npm or node to run to an array of node globals
NODE_GLOBALS=(`find ~/.nvm/versions/node -maxdepth 3 -type l -wholename '*/bin/*' | xargs -n1 basename | sort | uniq`)
NODE_GLOBALS+=("node")
NODE_GLOBALS+=("nvm")

# Lazy-loading nvm + npm on node globals call
load_nvm () {
  export NVM_DIR=~/.nvm
  [ -s "$(brew --prefix nvm)/nvm.sh" ] && . "$(brew --prefix nvm)/nvm.sh"
}

# Making node global trigger the lazy loading
for cmd in "${NODE_GLOBALS[@]}"; do
  eval "${cmd}(){ unset -f ${NODE_GLOBALS}; load_nvm; ${cmd} \$@ }"
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.