802

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?

bfontaine
  • 18,169
  • 13
  • 73
  • 107
frhd
  • 9,396
  • 5
  • 24
  • 41

29 Answers29

1566

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

dylants
  • 22,316
  • 3
  • 26
  • 22
  • 2
    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. – Jonah May 27 '16 at 02:28
  • My version of nvm (1.1.0) does not recognize the alias parameter – Kelly S. French Jun 02 '16 at 17:25
  • 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. – Evan Mattson Aug 31 '16 at 17:28
  • 55
    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` – goodmanship Jan 10 '17 at 01:25
  • 1
    @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 http://nvm.sh and you'll get the right version. – LJHarb Apr 05 '17 at 04:04
  • @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? – Kelly S. French Apr 05 '17 at 13:57
  • 1
    @KellyS.French ah! nvm does not support windows (except for BashOnWindows) - you may have nvm-windows, which is a different project. – LJHarb Apr 06 '17 at 18:59
  • 1
    you can also specify a `lts` version in later version of `nvm`. e.g.: `nvm alias default lts/boron` – XoXo Nov 19 '17 at 20:12
  • 1
    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. – Jeremy Dec 11 '17 at 20:40
  • 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... – YannickHelmut Jun 19 '18 at 17:54
  • 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. – Denis Howe Jan 18 '19 at 12:02
  • This answer worked for me on the Windows Subsystem for Linux (WSL) with Ubuntu 18.04 – Carsten Führmann Oct 28 '19 at 09:35
  • 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. – WakeskaterX May 22 '20 at 16:42
  • Instead of pinning Node to a version, you can specify `'lts/*'` as the version identifier to use the latest LTS version. – Trent Jul 21 '20 at 19:15
  • That works perfectly fine, but why use stopped working? – Ahmed Mahmoud Mar 15 '21 at 04:10
  • `nvm alias default stable` – Buh Buh Aug 12 '21 at 16:50
  • 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? – Rohan Shukla Feb 15 '22 at 11:09
  • 1
    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` – andresp Mar 01 '22 at 11:50
  • This no longer seems to work. – foxtrotuniform6969 Apr 15 '23 at 17:43
171

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

nvm alias default node
Tarun
  • 2,039
  • 1
  • 15
  • 5
  • 2
    This is actually better than aliasing to a specific version (the other answer). It is also the approach suggested at https://github.com/creationix/nvm#install-script . – Tom Dec 30 '15 at 20:46
  • 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 – Krishna Pravin Nov 16 '21 at 09:16
  • Seems this approach no longer works (2022-11-15). As per the docs, `node` is itself is an alias to the latest version on your system. https://github.com/nvm-sh/nvm#usage – aryzing Nov 15 '22 at 17:58
  • This should be the top answer, but I guess it never will. Voters don't always choose the right choice...Sad. – Nabil Kadimi Nov 24 '22 at 14:08
93

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.)

joeytwiddle
  • 29,306
  • 13
  • 121
  • 110
  • 3
    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! – bobbytables Sep 20 '18 at 20:18
  • 1
    Thank you! This was driving me bonkers. – Trees Jun 02 '20 at 22:13
  • Please clarify why you didn't use "source ~/.nvm/nvm.sh" after export statement ? – vikramvi May 24 '21 at 05:55
  • 1
    @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. – joeytwiddle May 26 '21 at 20:46
  • 1
    Yep, this worked for me. Otherwise I was seeing the default revert with each new terminal. – sails44 Jan 09 '22 at 15:30
  • 1
    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? – MuhsinFatih Feb 15 '22 at 23:41
  • @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". – joeytwiddle Feb 21 '22 at 21:12
  • Is there not a better way to fix the nvm addition to PATH by making sure it somehow persists in the PATH? Since other stuff might always update PATH later so this is super annoying to have to ensure nvm is always list in my dotfiles – rasen58 Aug 06 '22 at 17:37
  • oh my god. this helped me. it works. – Divij Jain Aug 16 '22 at 16:29
  • @rasen58 Does installing bash 5 fix the issue? – joeytwiddle Aug 20 '22 at 01:49
55

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)
ohho
  • 50,879
  • 75
  • 256
  • 383
  • This worked great and the suggestion to use `nvm list` helps users visualize what is happening with their versioning. Thanks! – png Mar 15 '19 at 22:29
31

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.

Jeff Mikels
  • 713
  • 1
  • 6
  • 14
  • 1
    how can I inspect this? I suspect this is the case. – Mild Fuzz Sep 25 '15 at 10:46
  • 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 – Alejandro Silva May 29 '16 at 05:12
  • 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. – Eugene Tartakovsky Jan 28 '17 at 16:54
  • 1
    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"` – asmiller Nov 18 '18 at 16:12
  • Same senario. I moved nvm script from `.bashrc` to `.bash_profile`, but that still didn't work. Then I set `nvm alias default` and move nvm script back to `.bashrc`. Problem solved. – Marvin Xu Oct 31 '22 at 10:11
28

Here is a simple instruction:

1) Install:

nvm install 8.10.0

2) Use once per terminal

nvm use 8.10.0

3) Set up as default for all terminals

nvm alias default 8.10.0

You may need to use root permissions to perform those actions.

And don't forget to check nvm documentation for more info.

Also note that you may need to specify node version for your IDE: enter image description here

Community
  • 1
  • 1
Arseniy-II
  • 8,323
  • 5
  • 24
  • 49
24

None of these solutions worked in my environment, nvm always seems to load the first installed version of node no matter what (unless you change it temporarily via nvm use).

The only way to change the default I have found is to:

  • Clear nvm cache: nvm cache clear
  • Set default to desired version: nvm alias default 12 (or whatever version)
  • Switch to desired version: nvm use 12
  • Uninstall all other versions:
    • nvm ls (to list installations)
    • nvm uninstall x (run for each installation that is not the default)
  • Reinstall other versions: nvm install x

You can use this script to automate this process (just change the first variable to your desired version) - it will re-install all versions you had previously automatically.

DEFAULT_NVM_VERSION=16
nvm cache clear
nvm install $DEFAULT_NVM_VERSION
nvm alias default $DEFAULT_NVM_VERSION
NVERS=$(nvm ls --no-alias | grep -v -- "->" | grep -o "v[0-9.]*")
while read ver; do nvm uninstall $ver; done <<< $NVERS
while read ver; do nvm install $ver; done <<< $NVERS
nvm use $DEFAULT_NVM_VERSION

Or as a one-liner:

DEFAULT_NVM_VERSION=16 && nvm cache clear && nvm install $DEFAULT_NVM_VERSION && nvm alias default $DEFAULT_NVM_VERSION && NVERS=$(nvm ls --no-alias | grep -v -- "->" | grep -o "v[0-9.]*") && while read ver; do nvm uninstall $ver; done <<< $NVERS && while read ver; do nvm install $ver; done <<< $NVERS && nvm use $DEFAULT_NVM_VERSION

New terminals should now respect the default version.

protango
  • 1,072
  • 12
  • 21
  • Thank you! None of the other solutions worked for me either. Maybe this is a recent nvm bug. – B T Apr 27 '22 at 23:42
  • Thanks but what is the point of putting it all on one line? The line that fixed it for me was `nvm alias default 16` – Phil Jul 30 '22 at 00:41
  • @Phil this solution is for when the `nvm alias default` command doesn’t work (it doesn’t work for a lot of folks here it seems) – protango Jul 31 '22 at 02:26
15

The top rated solutions didn't seem to work for me. My solution is below:

  1. Uninstall nvm completely using homebrew:brew uninstall nvm
  2. Reinstall brew install nvm
  3. In Terminal, follow the steps below(these are also listed when installing nvm via homebrew):

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

The steps outlined above will add NVM's working directory to your $HOME path, copy nvm-exec to NVM's working directory and add to $HOME/.bashrc, $HOME/.zshrc, or your shell's equivalent configuration file.(again taken from whats listed on an NVM install using homebrew)

ROOT
  • 11,363
  • 5
  • 30
  • 45
Joe
  • 1,850
  • 1
  • 13
  • 15
10

I'm using ZSH so I had to modify ~/.zshrc with the lines concerning NVM in that order:

[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
source ~/.nvm/nvm.sh
Matt Walterspieler
  • 2,073
  • 2
  • 21
  • 34
9

If you have tried everything still no luck you can try this :_

1 -> Uninstall NVM

rm -rf ~/.nvm

2 -> Remove npm dependencies by following this

3 -> Install NVM

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

4 -> Set ~/.bash_profile configuration

Run sudo nano ~/.bash_profile

Copy and paste following this

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

5 -> CONTROL + X save the changes

6 -> Run . ~/.bash_profile

7 -> Now you should have nvm installed on your machine, to install node run nvm install v7.8.0 this will be default node version or you can install any version of node

Community
  • 1
  • 1
Anand
  • 6,457
  • 3
  • 12
  • 26
  • 1
    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" – ajay Nov 18 '18 at 07:37
  • 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! – Jason R Stevens CFA Dec 22 '20 at 18:26
7

This question has mentioned for the OSX, but it happened to me in my linux OS. I tried using nvm alias default <version> but for each new terminal session the used node version was forgotten. so, here is the solution that i figured out.

make sure to set a default alias for node version,put the following code in .bashrc, and source .bashrc.

export NVM_DIR="/home/bonnie/.nvm"
## If the file exists and is not empty
if [ -s "$NVM_DIR/nvm.sh" ]; then
    ## Source it
    source "$NVM_DIR/nvm.sh"
fi
NODE_DEFAULT_VERSION=$(<"$NVM_DIR/alias/default")
export PATH="$NVM_DIR/versions/node/$NODE_DEFAULT_VERSION/bin":$PATH

descriptive solution link

Community
  • 1
  • 1
isnvi23h4
  • 1,910
  • 1
  • 27
  • 45
  • 1
    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`. – vaughan Jan 26 '16 at 05:16
5

Doing nvm install 10.14, for example, will nvm use that version for the current shell session but it will not always set it as the default for future sessions as you would expect. The node version you get in a new shell session is determined by nvm alias default. Confusingly, nvm install will only set the default alias if it is not already set. To get the expected behaviour, do this:

nvm alias default ''; nvm install 10.14

This will ensure that that version is downloaded, use it for the current session and set it as the default for future sessions.

Denis Howe
  • 2,092
  • 1
  • 23
  • 25
4

Linux/ubuntu

how to solve this you can see here

nvm use isn't meant to persist - it's only for the lifetime of the shell.

You can either do nvm alias default node if you want that to be the default when opening new shells or, you can make a .nvmrc file that will take precedence anywhere in the current directory, upwards to /. https://github.com/nvm-sh/nvm/issues/658

ubutnu-terminal

Al Mamun Khan
  • 551
  • 6
  • 7
3

run this after you installed any version,

n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local

This command is copying whatever version of node you have active via nvm into the /usr/local/ directory and setting the permissions so that all users can access them.

  • 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/*`! – joeytwiddle Feb 26 '18 at 03:38
3

I have found a new way here. Using n Interactively Manage Your Node.js helps.

Harshit Garg
  • 2,137
  • 21
  • 23
  • 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! – clozach Mar 28 '18 at 01:21
3

I was facing the same issue while using the integrated terminal in VS Code editor. Restarting VS Code after changing the node version using nvm fixed the issue for me.

RS1879
  • 81
  • 2
3
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

# place this after nvm initialization!
autoload -U add-zsh-hook

load-nvmrc() {
  local node_version="$(nvm version)"
  local nvmrc_path="$(nvm_find_nvmrc)"

  if [ -n "$nvmrc_path" ]; then
    local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")

    if [ "$nvmrc_node_version" = "N/A" ]; then
      nvm install
    elif [ "$nvmrc_node_version" != "$node_version" ]; then
      nvm use
    fi
  elif [ "$node_version" != "$(nvm version default)" ]; then
    echo "Reverting to nvm default version"
    nvm use default
  fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc
Kiry Meas
  • 1,200
  • 13
  • 26
  • 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. – Wayne Mao Jun 30 '22 at 05:51
3

For some reason in my .bashrc file I found this:

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

and had to remove --no-use flag, which I don't remember putting there in a first place... Just another thing to check.

3

On my end, I had to change both aliases STABLE and DEFAULT

nvm alias stable {node_version}

nvm alias default {node_version}
2

If you also have SDKMAN...

Somehow SDKMAN was conflicting with my NVM. If you're at your wits end with this and still can't figure it out, I just fixed it by ignoring the "THIS MUST BE AT THE END OF THE FILE..." from SDKMAN and putting the NVM lines after it.

#THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!
export SDKMAN_DIR="/Users/myname/.sdkman"
[[ -s "/Users/myname/.sdkman/bin/sdkman-init.sh" ]] && source "/Users/myname/.sdkman/bin/sdkman-init.sh"

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
itsDrew
  • 141
  • 1
  • 5
1

Also in case you had node installed before nvm check in your ~/.bash_profile to not have something like :

export PATH=/bin:/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:$PATH

If you do have it, comment/remove it and nvm should start handling the default node version.

Tiberiu Popescu
  • 4,486
  • 2
  • 26
  • 38
  • Setting a path variable has nothing to do with the nvm version selection, surely? – sn0r Jul 09 '17 at 14:05
  • 1
    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 Jan 18 '19 at 11:51
1
$ nvm alias default {NODE_VERSION}

when we use the above command, only update the node version but the npm still uses the old version.

Here is another solution for update the both node and npm, in my case i want to use node 8.9.4 and i have used the below command.

$ nvm use default 8.9.4

And the command returns the output.

Now using node v8.9.4 (npm v5.6.0)

Anish Agarwal
  • 1,793
  • 19
  • 19
1

As mentioned in the repository's issues section, nvm use is just for a lifetime of the shell. I have found this very useful, but sometimes it may put you in trouble actually when you are working on different codebases which need different versions of code. This is the link for the related discussion in GitHub

Emad Baqeri
  • 2,333
  • 2
  • 14
  • 29
1

For some reason, in my ~/.profile file I found that was selecting the version of node, overriding the alias default command of nvm

Just another thing to check.

William Ardila
  • 1,049
  • 13
  • 22
1

I tried everything above but nothing worked. How I fixed this error is setting the default alias to the desired version and then in .zshrc (or bash rc if is your case) at the bottom level:

nvm use default

Is not the best approach but it works for me.

0

On Ubuntu there is a potential issue if you are running a non-interactive shell, for example from a cronjob, or an init or upstart script.

Ubuntu's default .bashrc has this guard at the top which drops out early if the shell is non-interactive:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

So in order to get NVM loaded for non-interactive scripts, you can edit ~/.bashrc and move NVM's init to the top of the file, just above the guard.

Even safer is to copy the NVM init so it appears twice. That will address the concern mentioned in other answers, when other lines are modifying the PATH after NVM. NVM doesn't seem to mind being run twice, although it does slow down the shell startup a little.

joeytwiddle
  • 29,306
  • 13
  • 121
  • 110
0

In the nvm autoload script from the github I had to chage

local node_version="$(nvm version)" to local node_version="$(node -v)"

There was a local install of nvm on my system in my path so nvm version kept saying system no matter what

stack_pooper
  • 287
  • 1
  • 2
  • 12
0

1.- Install via homebrew

2.- Because I am using zsh terminal, in ~/.zshrc add this lines, if you are using bash you will need to put that lines in ~/.bash_profile

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

Ref: The issue that is causing this is related to the fact that the folder NPM is trying to access/write to is not owned by the user that is executing the command

The run the commands stated below and You should be good to go!

sudo chown -R `whoami` ~/.npm
sudo chown -R `whoami` /usr/local/lib/node_modules

Try running npm install -g ... or npm uninstall -g ... without sudo and it should run without errors.

Hadi Masoumi
  • 1,153
  • 9
  • 13