June 9, 2024

Node Version Manager(NVM)

Install (macOS)

  1. Open the Terminal and run

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

    This will download the NVM install script and install it on your computer

  2. Create a configuration file for the Zsh shell if you do not have one

    1
    touch ~/.zshrc

    Append the following code to the .zshrc file

    1
    2
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
  3. Execute the configuration file to take effect

    1
    source ~/.zshrc


Usage

Check nvm version

1
nvm -v

List node version available to install

1
nvm ls-remote

Sample Output

1
2
3
4
5
6
7
8
9
v0.1.14
v0.1.15
v0.1.16
...
v20.14.0 (Latest LTS: Iron)
v21.0.0
...
v22.1.0
v22.2.0

Install a specific version of the node

1
2
3
4
# v20.14.0
nvm install v20.14.0
# v14.21.3
nvm install v14.21.3

Validate installation

1
2
node -v
npm -v

Sample Output

1
2
3
4
5
v20.14.0
10.7.0

v14.21.3
6.14.18

Show installed node executable path

1
2
3
4
# Installed executable path for v20
nvm which 20
# Installed executable path for v14
nvm which 14

Sample Output

1
2
/Users/andy/.nvm/versions/node/v20.14.0/bin/node
/Users/andy/.nvm/versions/node/v14.21.3/bin/node

List node version installed

1
nvm ls

Sample Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
       v14.21.3
-> v20.14.0
default -> v14.21.3
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v20.14.0) (default)
stable -> 20.14 (-> v20.14.0) (default)
lts/* -> lts/iron (-> v20.14.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3
lts/gallium -> v16.20.2 (-> N/A)
lts/hydrogen -> v18.20.3 (-> N/A)
lts/iron -> v20.14.0

Switch node version

1
2
3
4
# Use node v14
nvm use 14
# Use node v20
nvm use 20

Sample Output

1
2
Now using node v14.21.3 (npm v6.14.18)
Now using node v20.14.0 (npm v10.7.0)


Reference

About this Post

This post is written by Andy, licensed under CC BY-NC 4.0.

#Nodejs#NVM