使用 NVM 對 Node.js 進行版本管理 for CentOS 7/8 - MIS 腳印
MIS 腳印 logo

MIS 腳印

記錄 IT 學習的軌跡

使用 NVM 對 Node.js 進行版本管理 for CentOS 7/8

NVM (Node Version Manager) 是一套用來管理 Node.js 的版本,可使用它任意的切換 Node.js 的各種版本。

Node.js

安裝

下載並安裝:

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.1/install.sh | bash
=> Downloading nvm as script to '/home/jacky/.nvm'

=> Appending nvm source string to /home/jacky/.bashrc
=> Appending bash_completion source string to /home/jacky/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:

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

重新登入:

logout

查看版本:

nvm --version
0.35.1

常用指令

查看所有指令:

nvm --help

查看遠端有哪些 Node.js 版本可以安裝:

nvm ls-remote

安裝指定的 Node.js 版本,例如 v8.12.0 (會自動切到該版本):

nvm install v8.12.0
Downloading and installing node v8.12.0...
Downloading https://nodejs.org/dist/v8.12.0/node-v8.12.0-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v8.12.0 (npm v6.4.1)
Creating default alias: default -> v8.12.0
node --version
v8.12.0

再安裝另一個 Node.js 版本 (會自動切到該版本):

nvm install v8.9.4
Downloading and installing node v8.9.4...
Downloading https://nodejs.org/dist/v8.9.4/node-v8.9.4-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v8.9.4 (npm v5.6.0)
node --version
v8.9.4

切換 Node.js 版本:

nvm use v8.12.0
Now using node v8.12.0 (npm v6.4.1)

設定預設使用 Node.js 版本:

nvm alias default v8.12.0
default -> v8.12.0

查看當前使用 Node.js 版本:

nvm current
v8.12.0

查看本地安裝哪些 Node.js 版本的詳細資訊,以及使用狀態:

nvm ls
         v8.9.4
->      v8.12.0     # 目前使用版本
default -> v8.12.0  # 重新登入使用的預設版本
node -> stable (-> v8.12.0) (default)
stable -> 8.12 (-> v8.12.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/erbium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.2 (-> N/A)
lts/dubnium -> v10.17.0 (-> N/A)
lts/erbium -> v12.13.1 (-> N/A)

移除已安裝的 Node.js 版本:

nvm uninstall v8.9.4
Uninstalled node v8.9.4

參考

在〈使用 NVM 對 Node.js 進行版本管理 for CentOS 7/8〉中有 1 則留言

發表迴響