nvm keeps "forgetting" node in new terminal session
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.
frhd
Updated on July 15, 2022Comments
-
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 over 8 yearshow can I inspect this? I suspect this is the case.
-
Tom over 8 yearsThis 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 over 8 yearsJust a note, this relies on including the
v
in the version when setting the remote. E.g.v4.2.4
instead of4.2.4
. -
Jonah almost 8 yearsWas 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 almost 8 yearsthis 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 almost 8 yearsMy version of nvm (1.1.0) does not recognize the alias parameter
-
Evan Mattson over 7 yearsAlso, 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 over 7 yearsIf that doesn't work, make sure in your
.bash_profile
(or.bashrc
or whatever) you don't have anything modifyingPATH
aftersource xx/nvm.sh
-
Eugene Tartakovsky over 7 yearsThat was my case. Solution was to move NVM ~/.bash_profile entry to the end of the file + set
nvm alias default
andnvm use
to the version i needed. -
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 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 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 almost 7 yearsSetting a path variable has nothing to do with the nvm version selection, surely?
-
XoXo over 6 yearsyou can also specify a
lts
version in later version ofnvm
. e.g.:nvm alias default lts/boron
-
Jeremy over 6 yearsI 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 about 6 yearsI would recommend using
&&
s instead of;
s in that command. If thewhich
fails to find node, we don't want to proceed to chmod/bin/*
! -
clozach about 6 yearsYes! 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 almost 6 yearsI 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 over 5 yearsThis helped me! Default was stuck.
-
bobbytables over 5 yearsGive 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 over 5 yearsThanks 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 over 5 yearsThis 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 over 5 yearsnvm.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 over 5 yearsThe 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 about 5 yearsThis worked great and the suggestion to use
nvm list
helps users visualize what is happening with their versioning. Thanks! -
Carsten Führmann over 4 yearsThis answer worked for me on the Windows Subsystem for Linux (WSL) with Ubuntu 18.04
-
WakeskaterX about 4 yearsffs 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 almost 4 yearsThank you! This was driving me bonkers.
-
Trent almost 4 yearsInstead of pinning Node to a version, you can specify
'lts/*'
as the version identifier to use the latest LTS version. -
Jason R Stevens CFA over 3 yearsI 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 about 3 yearsThat works perfectly fine, but why use stopped working?
-
vikramvi about 3 yearsPlease clarify why you didn't use "source ~/.nvm/nvm.sh" after export statement ?
-
Raja almost 3 years@vikramvi We did, on the last line.
\.
is the same assource
. 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 theexport 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 almost 3 yearsThanks. this solution is easiest if any one is facing issue on ZSH.
-
Buh Buh almost 3 years
nvm alias default stable
-
Krishna Pravin over 2 yearsIf 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 over 2 yearsYep, this worked for me. Otherwise I was seeing the default revert with each new terminal.
-
AliRehman7141 over 2 yearsThanks @arseniy
-
Rohan Shukla over 2 yearsDoesn'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 over 2 yearsI 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 over 2 years@MuhsinFatih What seems to happen is when
export PATH="$ANT_ROOT:$PATH"
executes, for some reason bash uses an earlier copy ofPATH
, ignoring the directory that nvm added. The result is that we don't have nvm's directory on thePATH
at all. It gets "forgotten". -
andresp about 2 yearsIn fish with jorgebucaran/nvm.fish I get
nvm: Unknown command or option: "alias" (see nvm -h)
. I guess there you need to useset --universal nvm_default_version $VERSION
-
Adriana Hernández about 2 yearsThanks! 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 about 2 yearsThank you! None of the other solutions worked for me either. Maybe this is a recent nvm bug.
-
Wayne Mao almost 2 yearsIt 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.