1926

I have installed nvm (Node.js with Z shell (executable zsh) (executable zsh) shell) with two Node.js versions: v6.11.5 and v9.0.0, and the default version in nvm is the v9.0.0.

Every time I need to change the Node.js version,

nvm list

Output:

         v6.11.5
->       v9.0.0
         system
default -> node (-> v9.0.0)
node -> stable (-> v9.0.0) (default)
stable -> 9.0 (-> v9.0.0) (default)

And:

nvm v6

How could I change the nvm version default to define v6.11.5?

1
  • 3
    for M1 mac nvm alias default xxx works but not nvm use default xxx Commented Jul 12, 2024 at 5:04

29 Answers 29

3822

(nvm maintainer here)

nvm alias default 6.11.5 if you want it pegged to that specific version.

You can also do nvm alias default 16 or nvm alias default node.

Either way, you'll want to upgrade to the latest version of nvm (v0.39.2 as of this writing)

# nvm set default node.js version 16.14.2
$ nvm alias default 16.14.2
$ nvm use

$ node -v
# v16.14.2
Sign up to request clarification or add additional context in comments.

18 Comments

Use nvm alias default node to make the "latest" the default.
Doing nvm alias default doesn't do anything. The tag "default" changes, but a new shell is still using the not-desired-version. I found that uninstalling not needed versions works.
I was using vs-code and it did not work initially, but when I restarted the vs-Code it worked.
nvm alias default node to use the latest version of Node installed on your computer. Remember to use nvm use node (or whatever Node version you want to use) after the first command to actually change the version. My example (I was using version 13 as default, but having 15 on machine and wanting to set default to latest version of Node): nvm alias default node nvm use node was like using (in my case): nvm alias default 15 nvm use 15
Might want to just use the latest long-term stable release. nvm alias default lts/*
For paranoid double-checking: I always open a new terminal windows/tab thereaftert, and truly verify with nvm list (looking at both --> (current version) and default --> (default version indeed) as well as node -v of course.
|
229

Let’s say we want to make the default version as 10.19.0.

nvm alias default v10.19.0

But it will give the following error

! WARNING: Version 'v10.19.0' does not exist.
default -> v10.19.0 (-> N/A)

In that case, you need to run two commands in the following order:

# Install the version that you would like
nvm install 10.19.0

# Set 10.19.0 (or another version) as default
nvm alias default 10.19.0

3 Comments

nvm alias default 10.19.0 Without "v"
it works both with or without the v.
don't forget to nvm use default as @alltozall20381 mentioned below or start a new shell.
217

This will set the default to be the most current version of Node.js:

nvm alias default node

And then you'll need to run

nvm use default

Or exit and open a new tab.

7 Comments

Asked for node 6, your solution will choose the last node stable version to use. So it will not use specific 6 version
Even though this is pretty useful, this doesn't answer the question at all.
This gave me a message "Your user’s .npmrc file (${HOME}/.npmrc) has a globalconfig and/or a prefix setting, which are incompatible with nvm. Run nvm use --delete-prefix v15.13.0 to unset it." I ended up running the suggested command and it worked.
Yours is the first answer that tells me to run nvm use default which is what I was missing.
This works for me, but only in the same shell. When I open a new one node -v still prints the previous version...
|
99

If you just want a major version as the default, this works:

nvm alias default 20

To check your current default:

nvm alias default

Output:

default -> 20 (-> v20.13.1)

Restart the terminal to apply it.

Comments

80

If you want to switch only for once, use this:

nvm use 12.x

Else, if you want to switch the default Node.js version, then use:

nvm use default 12.x

or

nvm alias default 12.x

Comments

79

I was trying to change the default version from a Visual Studio Code terminal on a Mac, but it didn’t work. I had to run this from the default system terminal:

nvm alias default v16.16.0

2 Comments

Just to clarify, you can also do "nvm alias default v16" and it'll stick to the v16 major version.
After running above command in macos terminal, had to restart my Vscode to see the changes applied in Vscode terminal.
59

For those testing this in the Visual Studio Code terminal and still seeing the old version even after killing/restarting terminal, Visual Studio Code caches the old version. Close/reopen your Visual Studio Code window, and you should see the correct version with node -v.

I got curious about why this is the case. The Visual Studio Code process provides new shells with the $PATH variable it received when it was started. Because nvm works by updating your $PATH, new shells in Visual Studio Code do not reflect those changes, and a full restart is required.

1 Comment

Thanks. This solved for me. I was trying to use the Reload Window command from the command palette, but it didn't work. I had to fully close VS Code and open it again.
55

This is what works for me.

nvm use default v16

This did not do anything for me:

nvm alias default v16

1 Comment

Notice that the v in v16 matters. Without that it didn't work for me (even though it does when I use nvm use 16).
34

You can also like this:

nvm alias default lts/fermium

4 Comments

Is there any way to target the latest version ? e.g. nvm alias default lts/latest ?
Try with nvm alias default lts/*
Note you will need to install lts/fermium first. Plus I find nvm alias default lts/* returns WARNING: Version 'lts/*' does not exist.
That format isn't supported by some shells. You can wrap in single quotes to get by this, e.g. nvm alias default 'lts/*'
26

If you want all projects and terminals to use the same Node.js version globally, nvm makes it simple to set a default version. Here's how:

  1. Install the latest version (if not already installed)

    Install the latest stable version of Node.js using nvm:

    nvm install node
    

    or

    nvm install [<version>]
    
  2. Set the global default version

    Run the following command to set the latest version as the global default for all environments:

    nvm alias default node
    

    or

    nvm alias default 23
    
  3. Verify the global default

    Close and reopen your terminal, then check the version to confirm:

    node -v
    

    This will show the default version being used across all sessions.

Why this works

By setting a default version with nvm alias default, every time you open a terminal, nvm will automatically load the specified version as the global default for all projects and scripts. You don’t need to manually switch versions anymore unless you explicitly want to.

Comments

24

Alert: This answer is for macOS only

Let’s suppose you have two versions of Node.js inside your nvm, namely v13.10.1 and v15.4.0.

And, v15.4.0 is default

nvm list

Output:

       v13.10.1
->      v15.4.0
         system
default -> 15.4.0 (-> v15.4.0)

And, you want to switch the default to v13.10.1

Follow these steps on your Mac terminal:

  1. Run the command:
nvm alias default 13.10.1

This will make the default point to v13.10.1 as...

default -> 13.10.1 (-> v13.10.1)
  1. Open new instance of terminal. Now check the node version here as...
node -v

You will get...

v13.10.1
  1. nvm list will also show the new default version.
nvm list

Just some information: The Node.js versions taken as example above will have their different npm versions. You can cross-verify it in terminal by running npm -v

2 Comments

Excellent step by step, thank you. Works on ubuntu 20.04
++ In fact, this answer would work totally well for Linux and Windows systems too. On Windows, the user may do these steps via Git bash
23

Enter image description here

If your Node.js version (node -v) is 16.17.0 or later, then run nvm alias default 20.9.0 to have node -v return 20.9.0.

2 Comments

I find your post so hard to read that I am not even sure whether you actually meant to answer (according to How to Answer). If so, please edit to make it more obvious. Otherwise please delete this.
Related: Why should I not upload images of code/data/errors?. It covers program output as well.
22

In a nutshell, steps to use NVM

For Mac

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.39.1/install.sh | bash
nvm install 16
nvm use 16
nvm alias default 16
npm install npm --global # Upgrade npm to the latest version

For Linux

sudo apt install curl git
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install nodejs

For Windows

Git's installer for Windows from the Windows install page.

File node-v16.XX.XX-x64.msi from Index of /dist/latest-v16.x/.

1 Comment

Re "For Linux": Would it work on Fedora or Arch Linux? What was it tested on?
19

I did something like this after running a nvm install --lts:

nvm alias default 'lts/*'

1 Comment

Good answer! nvm install --lts && nvm alias default lts/*
18

I tried the most-upvoted answer, and it didn’t work for me.

On Mac, the problem was that I had another Node.js instance installed by Homebrew (executable brew) which NVM recognizes as the system Node.js. NVM prioritizes the system Node.js over the default alias.

All I had to was to uninstall the system Node.js:

brew uninstall node

1 Comment

Same for me on WSL. I didn't have brew so I used ´sudo apt-get purge --auto-remove nodejs´
13

Since there are a lot of previous answers that talk about the default alias, and someone still can't get the right version in new terminal, my answer is here.

While you add source $NVM_DIR/nvm.sh in your shell rc file (like ~/.bashrc), it will first check whether there is a nvm-version Node.js path in the $PATH environment variable, like /usr/local/nvm/versions/node/v14.1.0/bin. If there is one, nvm will not use the default alias.

So firstly, you should check why there is Node.js path in $PATH. If you could get the reason (like run nvm use 16 explicitly in another rc file or script file), just remove it.

If you can't get reason or just want to keep it, then another solution is:

# That's your previous usage, keep it
source $NVM_DIR/nvm.sh

# FORCE to use the default alias
nvm use default

# Or if you prefer to forcedly use .nvmrc prior to default, then
test -f .nvmrc && nvm use || nvm use default

Comments

11

First check the available versions:

nvm list

Then set the default version using:

nvm alias default lts/**

Enter image description here

Comments

7

Use nvm alias default 16 (where "16" is the version you want to use), but if you're install Node.js from Download Node.js before, I would suggest you remove it first.

For Apple M1 or Apple M1 Pro chips, I suggest you follow this solution: How to uninstall Node.js from Mac M1

Comments

7

The current answers did not solve the problem for me, because I had Node.js installed in /usr/bin/node and /usr/local/bin/node, so the system always resolved these first, and ignored the nvm version.

I solved the issue by moving the existing versions to /usr/bin/node-system and /usr/local/bin/node-system.

Then I didn't have any node command anymore, until I used nvm use :(

I solved this issue by creating a symbolic link to the version that would be installed by nvm.

sudo mv /usr/local/bin/node /usr/local/bin/node-system
sudo mv /usr/bin/node /usr/bin/node-system
nvm use node
  # Now using node v12.20.1 (npm v6.14.10)

which node
  # /home/paul/.nvm/versions/node/v12.20.1/bin/node
sudo ln -s /home/paul/.nvm/versions/node/v12.20.1/bin/node /usr/bin/node

Then open a new shell

node -v

Output:

v12.20.1

Comments

6

Use this command to set a default Node.js version. For example, to set version 16 as the default, use:

nvm alias default 16

You can replace 16 with any other version, such as 18, 20, or 22, depending on your needs.

Comments

6

I solved this issue with the following command:

nvm use set as default xx.xx.x

Enter image description here

1 Comment

Related: Why should I not upload images of code/data/errors?. It covers program output as well.
5

Change the default version to use the latest LTS version:

nvm alias default lts/*

You manually upgrade the global version by doing nvm install lts/* --reinstall-packages-from=lts/* or a weekly cron job if you want to keep your version up-to-date.

The --reinstall-packages-from=lts/* is there to reinstall the global packages you had every time you change versions.

Comments

3

I had set the default version to use with nvm alias default <version>, but a new shell still didn't point to the version of node I wanted.

It turns out that:

  • nvm manipulates $PATH to set the version to use.
  • The .bash_profile file set by the system administrator reset $PATH to a fixed list of directories, rather than adding to the existing.
  • As this happened after .bashrc and thus nvm were run, nvm correctly picked my default, but the $PATH was then reset.

Two possible solutions:

  • Fix the $PATH manipulation, so that it doesn't clobber the existing $PATH
  • Or run nvm use default at the end of file .bash_profile, after the $PATH manipulation, so that nvm can readd the path to the correct version to $PATH

1 Comment

I ran into this today on linux. When I run nvm alias default, the correct version is shown. but node -v shows a version I previously installed. This seems like a dirty workaround, but I don't have much choice.
3

Whatever I tried, it did not use the version I specified, and running nvm current returned system.

Moving the following line to the end of my .zshrc file fixed it:

source ~/.nvm/nvm.sh

Comments

2

Change the default Node.js version with nvm alias default 10.15.3 (replace my version with your default version number).

You can check your default lists with nvm list.

Comments

1

For me the issue was, my nvm was not set every time I open a new terminal. Once I got rid of this issue, the default Node.js version was also being set fine.

After the nvm was initializing on startup of new terminal, the Node.js version was being initialized to the default set via nvm. Here.

Comments

1

FYI, it looks like tmux also caches the old version (like mentioned with Visual Studio Code in a previous answer). Restarting tmux then uses the new version for each window.

2 Comments

That was it! I had new version of node only in first pane, when I opened new one I got the old version. tmux kill-server helped.
Re "in a previous answer": In Kyle Chadha's answer?
1

In my situation of Windows 11, nvm 1.1.9, and using Git Bash:

These work for me (have to run as administrator):

Latest version

nvm use latest

Specific version

nvm use 18.10.0

These didn't work for me:

nvm use 18

returns:

node vv18.10.0 (64-bit) is not installed or cannot be found.

And

nvm use default 18

returns

Unrecognized version: "default"

And for

nvm alias ...

it seems the alias command is not supported in this nvm version.

Comments

-6

While NVM has its uses, I encourage you to consider an alternate option.

You can pin you project to a particular version of Node.js using the node package on Npm!

cd oldProject

npm i [email protected]

cd ../newProject

npm i [email protected]

Next time Npm runs node, it will use that version!

The node package accomplishes this by downloading the specified version of Node.js to node_modules/.bin/node. You you can run it directly, but it is easier to let Npm run it.

Any package.json#scripts will automatically use the specified version of node since node_modules/.bin is added to the path by Npm.

No more remembering which version of Node this package uses. No need to run anything new - just make sure npm i has been run.

{
  "scripts": {
    "node-version": "node --version"
  },
  "dependencies": {
    "node": "9.0.0"
  }
}

Note, you will need to npm install once before the correct node is used:

node --version
# v18.12.0
npm run node-version
# v18.12.0
npm install
npm run node-version
# v9.0.0
node --version
# v18.12.0
node_modules/.bin/node --version
# v9.0.0

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.