NVM常用命令记录
一、安装NVM
[root@localhost nginx]# yum install -y curl
[root@localhost nginx]# yum install -y wget# 使用curl或wget安装
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash[root@localhost nginx]# wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
# 检查安装是否成功
[root@localhost nginx]# nvm --version
0.39.7
二、查看远程可安装版本和本地已经安装的版本
查看所有可安装的 LTS(长期支持) 版本
如果你只想查看 LTS(长期支持) 版本(推荐用于生产环境),可以使用:
[root@localhost nginx]# nvm ls-remote --ltsv4.2.0 (LTS: Argon)v4.2.1 (LTS: Argon)v4.2.2 (LTS: Argon)
。。。。v22.13.1 (LTS: Jod)v22.14.0 (LTS: Jod)v22.15.0 (LTS: Jod)v22.15.1 (LTS: Jod)v22.16.0 (Latest LTS: Jod)
[root@localhost nginx]## 查看当前已安装的 Node.js 版本
Hydrogen 代表Node.js v18.x 系列
Iron 代表Node.js v20.x 系列
Jod 代表Node.js v22.x 系列
三、安装和卸载Node
[root@localhost nginx]# nvm install 16
Downloading and installing node v16.20.2...
Downloading https://nodejs.org/dist/v16.20.2/node-v16.20.2-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v16.20.2 (npm v8.19.4)
[root@localhost nginx]#
[root@localhost nginx]#
[root@localhost nginx]# node -v
v16.20.2[root@localhost nginx]# nvm uninstall 20
Uninstalled node v20.19.2
CentOS 7 默认 glibc 版本是 2.17,远低于 Node.js v18+ 所需的 GLIBC_2.27(对应 glibc 2.27+)。
由于 CentOS 7 自带的 glibc 和 libstdc++ 版本太低,直接升级这些系统库是非常危险的,因为它们是系统的核心组件,升级可能导致系统不稳定甚至崩溃。
解决方案:
1.使用 NodeSource RPM 包安装的 Node.js 18 ,但是不能被NVM管理。
2.使用 Docker 容器 运行 Node.js v18
3.升级到 CentOS 8 / Rocky Linux 8 / AlmaLinux 8,然后直接安装 Node.js v18
四、切换node版本和验证
[root@localhost nginx]# nvm use 16
Now using node v16.20.2 (npm v8.19.4)
[root@localhost nginx]# nvm lsv14.21.3
-> v16.20.2
default -> 18 (-> N/A)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v16.20.2) (default)
stable -> 16.20 (-> v16.20.2) (default)
lts/* -> lts/jod (-> N/A)
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
lts/hydrogen -> v18.20.8 (-> N/A)
lts/iron -> v20.19.2 (-> N/A)
lts/jod -> v22.16.0 (-> N/A)
[root@localhost nginx]# # 切换默认版本
[root@localhost nginx]# nvm alias default 16
default -> 16 (-> v16.20.2)
[root@localhost nginx]# nvm lsv14.21.3
-> v16.20.2
default -> 16 (-> v16.20.2)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v16.20.2) (default)
stable -> 16.20 (-> v16.20.2) (default)
lts/* -> lts/jod (-> N/A)
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
lts/hydrogen -> v18.20.8 (-> N/A)
lts/iron -> v20.19.2 (-> N/A)
lts/jod -> v22.16.0 (-> N/A)
[root@localhost nginx]##验证当前node版本
[root@localhost nginx]# node -v
v16.20.2
[root@localhost nginx]# npm -v
8.19.4