"nvm" command not recognized in new command line windows/tabs despite presence of "NVM_DIR" variable in both ~/.bashrc and ~/.bash_profile

12,957

Solution 1

The instructions from brew after installing nvm are:

Add the following to $HOME/.bashrc, $HOME/.zshrc, or your shell's equivalent configuration file:

  source $(brew --prefix nvm)/nvm.sh

On OS X with default settings, you actually want $HOME/.profile (or ~/.profile). Just add the line above to the end of that file.

Running that line once will set up nvm in that shell session. But if you add it to your .profile file, it will be run at the beginning of every shell session.

Solution 2

If you don't want to edit the .bash_profile

You could also export the nvm() function to your current shell enviroment:

source $(brew --prefix nvm)/nvm.sh

to check that is working:

nvm -v

If you want to edit the .bash_profile

sudo vim ~/.bash_profile

Type i to insert, and add:

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

Press wq! to save and exit and finally restart the terminal application.

Never Give Up! ✌️

Share:
12,957
Richie Thomas
Author by

Richie Thomas

Currently: Software engineer @ thoughtbot. Formerly: Software Engineer @ Fastly Software Engineer @ Blue Apron Associate Consultant @ ThoughtWorks Software apprentice @ BrightTag (now called Signal). Graduate of Dev Bootcamp in Chicago (June 2013; go Otters!).

Updated on June 05, 2022

Comments

  • Richie Thomas
    Richie Thomas almost 2 years

    My colleague recently installed Node Version Manager on his Macbook using Homebrew, and ran the two commands suggested at the end of the install script:

    export NVM_DIR=~/.nvm
    source $(brew --prefix nvm)/nvm.sh
    

    Everything works fine in the terminal window in which the install took place, but if he opens up a new terminal window or even a new tab, he has to install NVM all over again.

    My level of command-line experience is relatively low (I don't know what I don't know), so I'm not sure where to start diagnosing the problem. Suggestions are welcome.