nvm keeps "forgetting" node in new terminal session

184,830

Solution 1

Try nvm alias default. For example:

$ nvm alias default 0.12.7

This sets the default node version in your shell. Then verify that the change persists by closing the shell window, opening a new one, then: node --version

Solution 2

Alias to node itself to avoid updating the default alias along with node version updates later on.

nvm alias default node

Solution 3

In my case, another program had added PATH changes to .bashrc

If the other program changed the PATH after nvm's initialisation, then nvm's PATH changes would be forgotten, and we would get the system node on our PATH (or no node).

The solution was to move the nvm setup to the bottom of .bashrc

### BAD .bashrc ###

# NVM initialisation
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

# Some other program adding to the PATH:
export PATH="$ANT_ROOT:$PATH"

Solution:

### GOOD .bashrc ###

# Some other program adding to the PATH:
export PATH="$ANT_ROOT:$PATH"

# NVM initialisation
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

(This was with bash 4.2.46 on CentOS. It seems to me like a bug in bash, but I may be mistaken.)

Solution 4

To install the latest stable version:

nvm install stable

To set default to the stable version (instead of a specific version):

nvm alias default stable

To list installed versions:

nvm list

As of v6.2.0, it will look something like:

$ nvm list
         v4.4.2
->       v6.2.0
default -> stable (-> v6.2.0)
node -> stable (-> v6.2.0) (default)
stable -> 6.2 (-> v6.2.0) (default)
iojs -> N/A (default)

Solution 5

nvm does its job by changing the PATH variable, so you need to make sure you aren't somehow changing your PATH to something else after sourcing the nvm.sh script.

In my case, nvm.sh was being called in .bashrc but then the PATH variable was getting updated in .bash_profile which caused my session to find the system node before the nvm node.

Share:
184,830
frhd
Author by

frhd

Updated on July 15, 2022

Comments

  • frhd
    frhd almost 2 years

    Upon using a new terminal session in OS X, nvm forgets the node version and defaults to nothing:

    $ nvm ls:

             .nvm
         v0.11.12
         v0.11.13
    

    I have to keep hitting nvm use v.0.11.13 in every session:

             .nvm
         v0.11.12
    ->   v0.11.13
    

    I've tried both the brew install, as well as the official installation script.

    My .profile for the brew version:

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

    And for the install.sh script:

    $ curl https://raw.githubusercontent.com/creationix/nvm/v0.10.0/install.sh | bash

    #nvm
    export NVM_DIR="/Users/farhad/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
    

    Any clue to what I'm doing wrong?

  • Mild Fuzz
    Mild Fuzz over 8 years
    how can I inspect this? I suspect this is the case.
  • Tom
    Tom over 8 years
    This is actually better than aliasing to a specific version (the other answer). It is also the approach suggested at github.com/creationix/nvm#install-script .
  • vaughan
    vaughan over 8 years
    Just a note, this relies on including the v in the version when setting the remote. E.g. v4.2.4 instead of 4.2.4.
  • Jonah
    Jonah almost 8 years
    Was not working for me, and I didn't have the problem maxwell states. I didn't use brew but I upgraded/reinstalled my nvm and that fixed it.
  • Alejandro Silva
    Alejandro Silva almost 8 years
    this was my case thanks! i was setting the export on top on my .zshrc file, just move it to the bottom and the problem was solved
  • Kelly S. French
    Kelly S. French almost 8 years
    My version of nvm (1.1.0) does not recognize the alias parameter
  • Evan Mattson
    Evan Mattson over 7 years
    Also, depending on where the nvm init script is loaded in your profile, it's possible that a new shell doesn't load the expected version, even with the default aliased properly.
  • goodmanship
    goodmanship over 7 years
    If that doesn't work, make sure in your .bash_profile (or .bashrc or whatever) you don't have anything modifying PATH after source xx/nvm.sh
  • Eugene Tartakovsky
    Eugene Tartakovsky over 7 years
    That was my case. Solution was to move NVM ~/.bash_profile entry to the end of the file + set nvm alias default and nvm use to the version i needed.
  • LJHarb
    LJHarb about 7 years
    @KellyS.French (nvm maintainer here) nvm has never yet hit 1.0 - you may have installed it off of npm. Install only using the curl script at nvm.sh and you'll get the right version.
  • Kelly S. French
    Kelly S. French about 7 years
    @LJHarb I'll try that. It does make me wonder how my environment was translating my 'nvm' command. I installed nvm by downloading the Windows binary so is there a .bat or .ps1 equivalent of the curl script you mentioned?
  • LJHarb
    LJHarb about 7 years
    @KellyS.French ah! nvm does not support windows (except for BashOnWindows) - you may have nvm-windows, which is a different project.
  • sn0r
    sn0r almost 7 years
    Setting a path variable has nothing to do with the nvm version selection, surely?
  • XoXo
    XoXo over 6 years
    you can also specify a lts version in later version of nvm. e.g.: nvm alias default lts/boron
  • Jeremy
    Jeremy over 6 years
    I cannot get this to work on an Amazon linux image. I have tried the above and defining use, etc... I have to manually run: nvm use 8.8.1 everytime. this is a pain. NVM is too broken. I am just manually installing node. No issues this way.
  • Raja
    Raja about 6 years
    I would recommend using &&s instead of ;s in that command. If the which fails to find node, we don't want to proceed to chmod /bin/*!
  • clozach
    clozach about 6 years
    Yes! Not sure if it's 'cause I'm using fish shell, but the accepted answer didn't work for me. n works a treat!
  • YannickHelmut
    YannickHelmut almost 6 years
    I actually had to uninstall the version that was used by default since nothing else worked out. $ nvm uninstall 6.11.3 and then I switched to 6.11.5...
  • Redsandro
    Redsandro over 5 years
    This helped me! Default was stuck.
  • bobbytables
    bobbytables over 5 years
    Give this guy a medal! I had additional loading of other *rc files in my zshrc after the NVM_DIR export it drove me crazy. Thank you!
  • ajay
    ajay over 5 years
    Thanks a lot! This was the only solution which sets node path in bash as well and worked for me. I just used "nvm install node" to install latest node version rather than using "nvm install v7.8.0"
  • Admin
    Admin over 5 years
    This was the solution for me. Moved the following to the bottom of .bashrc: export NVM_DIR="$HOME/.nvm" then . "/usr/local/opt/nvm/nvm.sh"
  • Denis Howe
    Denis Howe over 5 years
    nvm.sh should modify your PATH after you set it (e.g. with the above). This let nvm put its version directory before /usr/local/bin where the "system" version of node lives.
  • Denis Howe
    Denis Howe over 5 years
    The behaviour is very confusing. The first nvm install X sets your nvm alias default to X, which is what you'll get in a new shell until you do another nvm use or nvm install. Even if you do nvm use or nvm install, that won't update nvm alias so you're stuck with the first default version unless you update the alias yourself.
  • png
    png about 5 years
    This worked great and the suggestion to use nvm list helps users visualize what is happening with their versioning. Thanks!
  • Carsten Führmann
    Carsten Führmann over 4 years
    This answer worked for me on the Windows Subsystem for Linux (WSL) with Ubuntu 18.04
  • WakeskaterX
    WakeskaterX about 4 years
    ffs this was such an easy answer, this has been bugging me for a while and thought it was going to be some annoying path thing to fix, thank you.
  • Trees
    Trees almost 4 years
    Thank you! This was driving me bonkers.
  • Trent
    Trent almost 4 years
    Instead of pinning Node to a version, you can specify 'lts/*' as the version identifier to use the latest LTS version.
  • Jason R Stevens CFA
    Jason R Stevens CFA over 3 years
    I don't think you need upped privileges via sudo in 4. I'll edit the answer to remove, as there appears to be no benefit to privilege escalation here, as the answer works within the home directory. If that's not the case, I am curious to hear why--thanks!
  • Ahmed Mahmoud
    Ahmed Mahmoud about 3 years
    That works perfectly fine, but why use stopped working?
  • vikramvi
    vikramvi about 3 years
    Please clarify why you didn't use "source ~/.nvm/nvm.sh" after export statement ?
  • Raja
    Raja almost 3 years
    @vikramvi We did, on the last line. \. is the same as source. The [ -s ... ] just checks that the file exists and is not empty. If you are asking why not move just the source line to the bottom, and leave the export NVM_DIR where it was originally, the only reason is to keep the NVM lines together for clarity. You could separate them if you wanted to.
  • Akash Kumar Seth
    Akash Kumar Seth almost 3 years
    Thanks. this solution is easiest if any one is facing issue on ZSH.
  • Buh Buh
    Buh Buh almost 3 years
    nvm alias default stable
  • Krishna Pravin
    Krishna Pravin over 2 years
    If setting default alias does not work, 1. Try moving the export NVM_DIR=.... in bash profile to the last 2. If that also does not work, uninstall nvm and reinstall it and install the node version you want Don't spend too much time finding a fix. reinstall is very quick
  • sails44
    sails44 over 2 years
    Yep, this worked for me. Otherwise I was seeing the default revert with each new terminal.
  • AliRehman7141
    AliRehman7141 over 2 years
    Thanks @arseniy
  • Rohan Shukla
    Rohan Shukla over 2 years
    Doesn't persist. I tried installing nvm, everything works, but when I remove that node version it says it's not installed, but when I check node version it comes up?
  • MuhsinFatih
    MuhsinFatih over 2 years
    I don't understand, why would an entirely unrelated path in the $PATH variable affect node version? Was your "other program" modifying the node path?
  • Raja
    Raja over 2 years
    @MuhsinFatih What seems to happen is when export PATH="$ANT_ROOT:$PATH" executes, for some reason bash uses an earlier copy of PATH, ignoring the directory that nvm added. The result is that we don't have nvm's directory on the PATH at all. It gets "forgotten".
  • andresp
    andresp about 2 years
    In fish with jorgebucaran/nvm.fish I get nvm: Unknown command or option: "alias" (see nvm -h). I guess there you need to use set --universal nvm_default_version $VERSION
  • Adriana Hernández
    Adriana Hernández about 2 years
    Thanks! In my case it worked by running this in vs code terminal: nvm use 16.14.2 and then restart VS Code. Beautiful!
  • B T
    B T about 2 years
    Thank you! None of the other solutions worked for me either. Maybe this is a recent nvm bug.
  • Wayne Mao
    Wayne Mao almost 2 years
    It can be simplify to nvm use default >> /dev/null 2>&1 I use zsh and I found that the accepted answer doesn't work. I think on zsh, this might be the best(only?) choice.