在 CentOS 上使用 yum
安装 Node.js 非常简单。可以按照以下步骤进行:
1. 添加 NodeSource 仓库
NodeSource 提供了 Node.js 的官方仓库。首先,添加你需要的 Node.js 版本仓库,例如 Node.js 16.x:
curl -sL https://rpm.nodesource.com/setup_16.x | sudo bash -
2. 安装 Node.js
接下来,使用 yum
安装 Node.js:
sudo yum install -y nodejs
3. 验证安装
安装完成后,检查 Node.js 和 npm 版本以确认安装成功:
# node -v
v16.20.2
# npm -v
8.19.4
4. 更新 npm(可选)
如果需要更新 npm 到最新版本,可以使用以下命令:
sudo npm install -g npm@latest