ChatGPT解决这个技术问题 Extra ChatGPT

How can the default node version be set using NVM?

I have installed nvm (ubuntu with zsh shell) with two node version: 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 version

$ nvm list
         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)


$ nvm v6

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


L
LJHarb

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

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

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

$ node -v
# v16.14.2

Addition: alias works as long as you're not installing new versions. After installing a new node the node will be your current node to use. So if you want to prevent this behavior you have to explicitly set up nvm alias default desiredVersionOfNode after each installation or just reopen a terminal.
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.
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/*
D
Dipesh Yadav

Lets say to want to make default version as 10.19.0.

nvm alias default v10.19.0

But it will give 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

nvm alias default 10.19.0 Without "v"
a
alltozall20381

This will set the default to be the most current version of node

nvm alias default node

and then you'll need to run

nvm use default

or exit and open a new tab


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.
P
Paul Verest

If you want to switch only for once use this

nvm use 12.x

Else if you want to switch the default node version then use

nvm use default 12.x 

or

nvm alias default 12.x

A
August Gong

You can also like this:

$ nvm alias default lts/fermium

Is there any way to target the latest version ? e.g. nvm alias default lts/latest ?
Try with nvm alias default lts/*
R
Rishabh B

Alert: This answer is for MacOS only

Let suppose you have 2 versions of nodeJS inside your nvm, namely v13.10.1 & v15.4.0

And, v15.4.0 is default

> nvm list
       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:

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)

Open new instance of terminal. Now check the node version here as...

node -v

You will get...

v13.10.1

nvm list will also show the new default version. nvm list

Just an info: The NodeJS versions taken as example above will have their different npm versions. You can cross-verify it in terminal by running npm -v


d
dolar

In 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 below link

https://git-scm.com/download/win

node-v16.XX.XX-x64.msi from below link

https://nodejs.org/dist/latest-v16.x/

M
Maxime

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

nvm alias default 'lts/*'

K
Kyle Chadha

For those testing this in VSCode terminal and still seeing the old version even after killing/restarting terminal -- VS code caches the old version somehow. Close/reopen your full VSCode window and you should see the correct version with node -v.


M
Maddu Swaroop

First check available versions

nvm list

Then set default version using

nvm alias default lts/**

https://i.stack.imgur.com/zXsPf.png


I
Ingun전인건

I tried the most-upvoted answer and didn’t work for me. The problem was that I had another node installed by brew which NVM recognizes as system-node. NVM prioritizes system-node over default alias. All I had to was to uninstall the system-node (brew uninstall node).


Same for me on WSL. I didn't have brew so I used ´sudo apt-get purge --auto-remove nodejs´
@LDW comment did the job for me on WSL as well.
H
Harrison Cramer

nvm alias default 16 (where "16" is the version you want to use) but if you're install node from https://nodejs.org/en/download/ before I would suggest you remove it first. For m1 or m1 pro chips, I suggest you follow this solution: https://www.youtube.com/watch?v=fULL8QiPEU4


o
okwyvic

This is what works for me.

nvm use default v16

This did not do anything for me

nvm alias default v16

P
Paul Weber

The current answers did not solve the problem for me, because I had node 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 had no node command anymore, until I used nvm use :(

I solved this issue by creating a symlink 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
  v12.20.1

B
Benos

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 everytime you change versions


Y
Yuto

change the default node version with nvm alias default 10.15.3 *

(replace mine version with your default version number)

you can check your default lists with nvm list


R
Rajbir

#Worked for me 100% Follow this for default node version:

nvm install 12.13.1 then, nvm alias default 12.13.1


B
Brandon Austin

Make sure to have the correct version of node installed globally. Your company might be using a different version.


t
tianjianchn

Since there are a lot of answers above 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 path in the $PATH environment variable, like /usr/local/nvm/versions/node/v14.1.0/bin. If there is one, nvm will not use default alias.

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

If you can't or just wanna keep it, then another solution is here:

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

# use default alias
nvm use default

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

P
Pushpendu Ghosh

nvm alias default <node version>

this will make the <node version> default.


The only thing this "new" answer does is to repeat what half of the other answers are already saying, including the accepted answer.